-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
243 lines (199 loc) · 6.13 KB
/
Copy pathindex.js
File metadata and controls
243 lines (199 loc) · 6.13 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
// https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Pixel_manipulation_with_canvas
const isLocal = window.location.href.indexOf('github.io') === -1;
const img = new Image();
img.crossOrigin = "anonymous";
img.src = isLocal
? "https://jdc-cunningham.github.io/oled-pixels-to-mpython/pexels_living-room.jpeg"
: "./pexels_living-room.jpeg";
const canvas = document.getElementById("pixels");
const canvasWidth = canvas.offsetWidth;
const canvasHeight = canvas.offsetHeight;
const ctx = canvas.getContext("2d");
const microPythonOutput = document.getElementById('micropython-output');
canvas.width = canvasWidth;
canvas.height = canvasHeight;
img.addEventListener("load", () => {
ctx.drawImage(img, 0, 0);
img.style.display = "none";
});
const reset = () => {
ctx.drawImage(img, 0, 0);
img.style.display = "none";
microPythonOutput.innerText = "";
}
const colors = {
white: "white",
yellow: "yellow",
cyan: "cyan",
green: "green",
pink: "pink",
red: "red",
blue: "blue",
black: "black"
};
const colorsRGB = {
white: [255, 255, 255],
yellow: [255, 255, 0],
cyan: [0, 255, 255],
green: [0, 128, 0],
pink: [255, 192, 203],
red: [255, 0, 0],
blue: [0, 0, 255],
black: [0, 0, 0]
};
const colorsHex = {
white: "0xFFFFFF",
yellow: "0xFFFF00",
cyan: "0x00FFFF",
green: "0x008000",
pink: "0xFFC0CB",
red: "0xFF0000",
blue: "0x0000FF",
black: "0x000000"
};
const baseColor = [195, 195, 195]; // 0xc3c3c3
let pixelSize = 10; // 10 min based on monocle draw library
let activeColor = colors.white;
const hoverPixel = document.getElementById('hover-pixel');
const selectedPositions = [];
const selectedPositionsCat = []; // concatenate for search
const imgPixels = {};
function pick(event, callback) {
const bounding = canvas.getBoundingClientRect();
const x = event.clientX - bounding.left;
const y = event.clientY - bounding.top;
const mousePagePos = [event.clientX, event.clientY];
const snapPosOffset = [
x % 10,
y % 10
];
const snapX = snapPosOffset[0];
const snapY = snapPosOffset[1];
const pixelGroup = ctx.getImageData(x - snapX, y - snapY, 10, 10);
const data = pixelGroup.data;
const rgba = [data[0], data[1], data[2], data[3] / 255];
hoverPixel.style.transform = `translateX(${mousePagePos[0] - snapX + 1}px) translateY(${mousePagePos[1] - snapY + 1}px)`;
hoverPixel.classList = 'active';
// make pixel active/inactive
if (event.type === "click") {
const coordStr = `${x - snapX}-${y - snapY}`;
const coord = selectedPositionsCat.indexOf(coordStr);
if (
coord === -1
) {
imgPixels[coordStr] = rgba;
for (let i = 0; i < data.length; i += 4) {
data[i + 0] = colorsRGB[activeColor][0];
data[i + 1] = colorsRGB[activeColor][1];
data[i + 2] = colorsRGB[activeColor][2];
}
selectedPositions.push(
[x - snapX, y - snapY, activeColor],
);
selectedPositionsCat.push(
coordStr,
);
ctx.putImageData(pixelGroup, x - snapX, y - snapY);
} else {
for (let i = 0; i < data.length; i += 4) {
data[i + 0] = imgPixels[coordStr][0];
data[i + 1] = imgPixels[coordStr][1];
data[i + 2] = imgPixels[coordStr][2];
}
selectedPositionsCat.splice(coord, 1);
ctx.putImageData(pixelGroup, x - snapX, y - snapY);
}
}
return rgba;
}
canvas.addEventListener("mousemove", (event) => pick(event, (e) => {
console.log(e)
}));
let mouseoutTimer;
canvas.addEventListener("mouseout", (event) => {
clearTimeout(mouseoutTimer);
mouseoutTimer = setTimeout(() => {
hoverPixel.classList = '';
}, 250); // fire after pick
});
canvas.addEventListener("click", (event) => pick(event, (e) => {
console.log(e)
}));
// event listeners
const resetBtn = document.getElementById('reset');
const generateBtn = document.getElementById('generate');
const colorBtns = document.querySelectorAll('.color');
resetBtn.addEventListener('click', reset);
colorBtns.forEach(color => {
color.addEventListener('click', (e) => {
const pickedColor = e.target.id;
const colorWord = pickedColor.split('-')[1];
if (colorWord !== activeColor) {
document.getElementById(`color-${activeColor}`).classList = 'color';
hoverPixel.style.backgroundColor = colorWord;
}
color.classList = 'color active';
activeColor = colorWord;
});
});
// mpython generation algo
// sort ascending
// box in by row
//
// if same color, same line
// break (base color) or new color, new line
// https://stackoverflow.com/questions/16096872/how-to-sort-2-dimensional-array-by-column-value
// omg "if you want to sort by second col" convenient
const sortCoordinates = (coords) => {
coords.sort(compareSecondColumn);
function compareSecondColumn(a, b) {
if (a[1] === b[1]) {
return 0;
}
else {
return (a[1] < b[1]) ? -1 : 1;
}
}
}
const microPythonCmds = [
'import display'
];
const generateMicroPython = () => {
let prevX = 0;
let prevY = 0;
let lineWidth = 10; // forms horizontal line
selectedPositions.forEach(coord => {
let thisX = coord[0];
let thisY = coord[1];
if (prevX === 0 && prevY === 0) { // first run
microPythonCmds.push(`display.hline(${thisX - lineWidth}, ${thisY}, ${lineWidth}, ${colorsHex[coord[2]]})`);
} else {
if (thisY > prevY) { // new line
microPythonCmds.push(`display.hline(${thisX - lineWidth}, ${thisY}, ${lineWidth}, ${colorsHex[coord[2]]})`);
lineWidth = 10;
} else {
if (thisX - 9 === prevX) {
lineWidth += 10;
} else {
microPythonCmds.push(`display.hline(${thisX - lineWidth}, ${thisY}, ${lineWidth}, ${colorsHex[coord[2]]})`); // have to store and match color
lineWidth = 10;
}
}
}
prevX = thisX;
prevY = thisY;
});
}
generateBtn.addEventListener('click', () => {
sortCoordinates(selectedPositions);
generateMicroPython();
microPythonCmds.push('display.show()');
microPythonCmds.forEach(cmd => {
microPythonOutput.innerText += cmd + '\n';
});
});
// improvements
// hold drag vs. clicking every pixel
// fill support
// custom color input
// make algo use hline and vline