forked from javierbyte/img2css
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.jsx
More file actions
executable file
·248 lines (210 loc) · 6.51 KB
/
app.jsx
File metadata and controls
executable file
·248 lines (210 loc) · 6.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import React, { useState } from "react";
import tinycolor from "tinycolor2";
import _ from "lodash";
import { HeaderH1, HeaderH2, Text, Space, Box, A, Container, Dropzone, Ul, Li } from "./jbx.jsx";
import Styled from "styled-components";
const Textarea = Styled.textarea({
"font-family": "monaco, monospace",
border: "none",
width: "100%",
height: "256px",
"font-size": "13px",
"line-height": "1.309",
padding: "16px 18px",
background: "#ecf0f1",
color: "#34495e",
":focus": {
outline: "none",
},
});
import ImageUtils from "base64-image-utils";
const base64ImageToRGBMatrix = ImageUtils.base64ImageToRGBMatrix;
function compressColor(rgb) {
const hex = tinycolor(rgb).toHexString();
switch (
hex // based on CSS3 supported color names http://www.w3.org/TR/css3-color/
) {
case "#c0c0c0":
return "silver";
case "#808080":
return "gray";
case "#800000":
return "maroon";
case "#ff0000":
return "red";
case "#800080":
return "purple";
case "#008000":
return "green";
case "#808000":
return "olive";
case "#000080":
return "navy";
case "#008080":
return "teal";
}
return hex[1] === hex[2] && hex[3] === hex[4] && hex[5] === hex[6] ? "#" + hex[1] + hex[3] + hex[5] : hex;
}
function resizeImage(base64Str, maxMass = 128 * 128) {
return new Promise((resolve) => {
let img = new Image();
img.src = base64Str;
img.onload = () => {
let canvas = document.createElement("canvas");
const originalWidth = img.width;
const originalHeight = img.height;
let width = img.width;
let height = img.height;
while (width * height > maxMass) {
width = width / Math.sqrt(2, 2);
height = height / Math.sqrt(2, 2);
}
width = Math.round(width);
height = Math.round(height);
canvas.width = width;
canvas.height = height;
let ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, width, height);
resolve([canvas.toDataURL(), { originalWidth, originalHeight }]);
};
});
}
function App() {
const [rgbMatrix, rgbMatrixSet] = useState(null);
const [loadingImage, loadingImageSet] = useState(false);
function onFileSelected(event) {
event.stopPropagation();
event.preventDefault();
const dt = event.dataTransfer;
const files = dt ? dt.files : event.target.files;
const file = files[0];
const fr = new window.FileReader();
loadingImageSet(true);
fr.onload = async (data) => {
const base64src = data.currentTarget.result;
const [base64] = await resizeImage(base64src);
base64ImageToRGBMatrix(base64, (err, data) => {
if (err) return console.error(err);
rgbMatrixSet(data);
loadingImageSet(false);
});
};
fr.readAsDataURL(file);
}
function onDragOver(event) {
event.stopPropagation();
event.preventDefault();
}
let scale = 1;
const masterShadow = _.map(rgbMatrix, (row, rowIndexSrc) => {
return _.map(row, (col, colIndexSrc) => {
const colIndex = colIndexSrc * scale;
const rowIndex = rowIndexSrc * scale;
const color = compressColor(`rgb(${col.r},${col.g},${col.b})`);
const scaleCompensation = scale !== 1 ? ` 0 ${scale / 2}px` : ``;
return `${color} ${colIndex ? colIndex + "px" : 0} ${rowIndex ? rowIndex + "px" : 0}${scaleCompensation}`;
}).join(",");
}).join(",");
const handleFocus = (event) => {
event.preventDefault();
event.stopPropagation();
event.target.select();
};
return (
<Container>
<Box padding={2}>
<Space h={1} />
<HeaderH1
style={{
fontWeight: 900,
display: "inline-block",
width: "auto",
padding: "6px",
backgroundColor: "var(--accent-color)",
}}>
img2css
</HeaderH1>
<Space h={1} />
<Text>This is a tool that can convert any image into a pure css image.</Text>
<Space h={2} />
<Dropzone onDrop={onFileSelected} onDragOver={onDragOver} onDragEnter={onDragOver}>
{loadingImage ? (
<Text>Processing...</Text>
) : (
<div>
<Text>Drop an image here</Text>
<Text>or click to select</Text>
</div>
)}
<input
type="file"
onChange={onFileSelected}
multiple
accept="image/*"
aria-label="Drop an image here, or click to select"
/>
</Dropzone>
<Space h={2} />
<Text>
I also made a per-pixel animation experiment, see <A href="https://javier.xyz/morphin/">morphin</A>.
</Text>
{rgbMatrix && (
<div>
<Space h={2} />
<HeaderH2>The result</HeaderH2>
<Space h={1} />
<Text>This is your pure css (and single div) image! Enjoy!</Text>
<Space h={1} />
<div
style={{
height: 1,
width: 1,
boxShadow: masterShadow,
marginBottom: rgbMatrix.length * scale,
marginRight: rgbMatrix[0].length * scale,
}}
/>
<Space h={1} />
<Textarea
onFocus={handleFocus}
onChange={() => {}}
className="code"
value={`<div style="margin-right: ${rgbMatrix[0].length * scale}px; margin-bottom: ${
rgbMatrix.length * scale
}px; height: 1px; width: 1px; box-shadow: ${masterShadow}"></div>`}
/>
<Space h={1} />
<Text>Size: {masterShadow.length.toLocaleString()}b</Text>
</div>
)}
<Space h={2} />
<Text>More unrelated experiments</Text>
<Space h={0.5} />
<Ul>
<Li>
<Text>
Play with 3D and shaws, <A href="https://sombras.app/">sombras.app</A>.
</Text>
</Li>
<Li>
<Text>
Find the visual center of your images / logos,{" "}
<A href="https://javier.xyz/visual-center/">visual-center</A>.
</Text>
</Li>
<Li>
<Text>
JS AI Battle Game, <A href="https://clashjs.com/">clashjs</A>.
</Text>
</Li>
</Ul>
<Space h={2} />
<Text>
Made by <A href="https://javier.xyz">javierbyte</A>.
</Text>
<Space h={3} />
</Box>
</Container>
);
}
export default App;