-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pixel.js
198 lines (162 loc) · 6.82 KB
/
Pixel.js
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
const kronecker = function(target, mask) {
const result = [[Math.ceil(target[0] / mask[0]), Math.ceil(target[1] / mask[1])],
[((target[0] - 1) % mask[0]) + 1, ((target[1] - 1) % mask[1]) + 1]];
var quadrant = result[0].join("");
switch (quadrant) {
case "11": quadrant = "00"; break;
case "12": quadrant = "01"; break;
case "21": quadrant = "10"; break;
case "22": quadrant = "11"; break;
}
if (mask[0] == 1)
return quadrant;
return (quadrant + kronecker(result[1], [mask[0] / 2, mask[1] / 2]));
}
const dotProduct = function(U, V) {
return U.x * V.x + U.y * V.y;
};
const isInside = function(vertex, square) {
const AB = { x: square.b.x - square.a.x, y: square.b.y - square.a.y };
const AM = { x: vertex.x - square.a.x, y: vertex.y - square.a.y };
const BC = { x: square.c.x - square.b.x, y: square.c.y - square.b.y };
const BM = { x: vertex.x - square.b.x, y: vertex.y - square.b.y };
return (0 <= dotProduct(AB, AM) && dotProduct(AB, AM) <= dotProduct(AB, AB))
&& (0 <= dotProduct(BC, BM) && dotProduct(BC, BM) <= dotProduct(BC, BC));
};
const isInsideSquare = function(vertex, square, isize) {
return isInside(vertex, square)
|| isInside({x: vertex.x + isize.width, y: vertex.y}, square)
|| isInside({x: vertex.x, y: vertex.y + isize.height}, square)
|| isInside({x: vertex.x + isize.width, y: vertex.y + isize.height}, square);
};
const toBinary = function(decimal, padding) {
return decimal.toString(2).padStart(padding, 0);
};
const getId = function (source) {
return /[01]+$/.exec(source)[0];
};
const canvas = {
get boundary() { return document.getElementById("canvas").getBoundingClientRect() },
vertices: {
get a() { return { x: 0, y: 0 }; },
get b() { return { x: 0, y: canvas.boundary.width }; },
get c() { return { x: canvas.boundary.height, y: canvas.boundary.width }; },
get d() { return { x: canvas.boundary.height, y: 0 }; }
},
url: "http://127.0.0.1:5000/image/",
width: 0,
height: 0,
images: [],
level: 0
};
const mouse = {
x: 0,
y: 0,
prevX: 0,
prevY: 0
};
const draw = function(ctx, csize, isize, url, level, images, coords) {
ctx.clearRect(0, 0, csize.width, csize.height);
const seedCoord = images[0] ? {x : coords.x - coords.prevX, y: coords.y - coords.prevY} : coords;
const seedImage = images[0] ? images.find((i) => isInsideSquare({x: seedCoord.x + i.x, y: seedCoord.y + i.y}, canvas.vertices, isize)) || {pos: [1,1], x: 0, y: 0} : {pos: [1,1], x: 0, y: 0};
return draw_(ctx, isize, url, level, images.map((i) => {
return Object.assign(i, {drawn: false});
}), seedImage.pos, {x: seedCoord.x + seedImage.x, y: seedCoord.y + seedImage.y});
}
const draw_ = function(ctx, isize, url, level, images, pos, coords) {
if (level != canvas.level || pos.some((i) => i < 1 || i > (2 ** (level + 1)))) {
return images;
}
const targetImage = kronecker(pos, [2 ** (level + 1), 2 ** (level + 1)]);
const idPath = targetImage.substring(0, targetImage.length - 2).match(/.{1,2}/g).join('/');
const id = "00/" + idPath + "/" + targetImage + ".jpg";
const index = images.findIndex((i) => (url + id) == i.image.src);
if (!isInsideSquare(coords, canvas.vertices, isize)) {
if (index != -1) {
images.splice(index);
}
return images;
} else {
const target = images[index];
if (target) {
if (target.drawn) {
return images;
} else {
if (target.image.naturalWidth) {
ctx.drawImage(target.image, coords.x, coords.y);
} else {
target.image.onload = () => ctx.drawImage(target.image, coords.x, coords.y);
}
images.push({
image: target.image,
x: coords.x,
y: coords.y,
drawn: true,
pos: pos
});
images.splice(index);
}
} else {
const image = new Image();
image.src = url + id;
image.onload = () => ctx.drawImage(image, coords.x, coords.y);
images.push({
image: image,
x: coords.x,
y: coords.y,
drawn: true,
pos: pos
});
}
images = draw_(ctx, isize, url, level, images, [pos[0] - 1, pos[1]], { x: coords.x, y: coords.y - isize.height });
images = draw_(ctx, isize, url, level, images, [pos[0], pos[1] + 1], { x: coords.x + isize.width, y: coords.y });
images = draw_(ctx, isize, url, level, images, [pos[0] + 1, pos[1]], { x: coords.x, y: coords.y + isize.height });
images = draw_(ctx, isize, url, level, images, [pos[0], pos[1] - 1], { x: coords.x - isize.width, y: coords.y });
return images;
}
};
const init = function(c) {
const image = new Image();
const targetImage = "0".repeat((canvas.level + 1) * 2);
image.src = canvas.url + targetImage.match(/.{1,2}/g).join('/').concat("/") + targetImage + ".jpg";
image.onload = () => {
canvas.width = image.width;
canvas.height = image.height;
canvas.images = draw(c.getContext("2d"), canvas.boundary, canvas,
canvas.url, canvas.level, canvas.images, {x: 0, y: 0});
}
}
const main = function() {
const cbox = document.getElementById("canvas-box");
const c = document.getElementById("canvas");
c.setAttribute("width", window.getComputedStyle(cbox).getPropertyValue("width"));
c.setAttribute("height", window.getComputedStyle(cbox).getPropertyValue("height"));
document.addEventListener("mousedown", (e) => {
if (c === document.activeElement && e.button === 0) {
mouse.prevX = e.clientX;
mouse.prevY = e.clientY;
}
});
document.addEventListener("mouseup", (e) => {
if (c === document.activeElement && e.button === 0) {
mouse.x = e.clientX;
mouse.y = e.clientY;
canvas.images = draw(c.getContext("2d"), canvas.boundary, canvas,
canvas.url, canvas.level, canvas.images, mouse);
}
});
document.addEventListener("wheel", (e) => {
if (c === document.activeElement) {
if (e.deltaY > 0) {
canvas.level += 1;
} else {
if (canvas.level > 0) {
canvas.level += -1;
}
}
document.getElementById("level").innerText = canvas.level;
init(c);
}
});
init(c);
};