forked from 1j01/jspaint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtool-options.js
316 lines (290 loc) · 8.63 KB
/
tool-options.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
const ChooserCanvas = (
url,
invert,
width,
height,
sourceX,
sourceY,
sourceWidth,
sourceHeight,
destX,
destY,
destWidth,
destHeight,
reuse_canvas,
) => {
const c = reuse_canvas(width, height);
let img = ChooserCanvas.cache[url];
if(!img){
img = ChooserCanvas.cache[url] = E("img");
img.onerror = ()=> {
delete ChooserCanvas.cache[url];
};
img.src = url;
}
const render = () => {
try {
c.ctx.drawImage(
img,
sourceX, sourceY, sourceWidth, sourceHeight,
destX, destY, destWidth, destHeight
);
// eslint-disable-next-line no-empty
} catch(error) {}
if(invert){
invert_rgb(c.ctx);
}
};
$(img).on("load", render);
render();
return c;
};
ChooserCanvas.cache = {};
const $Choose = (things, display, choose, is_chosen) => {
const $chooser = $(E("div")).addClass("chooser").attr("touch-action", "none");
const choose_thing = (thing) => {
if(is_chosen(thing)){
return;
}
choose(thing);
$G.trigger("option-changed");
};
$chooser.on("update", () => {
if (!$chooser.is(":visible")) {
return;
}
$chooser.empty();
for(let i=0; i<things.length; i++){
(thing => {
const $option_container = $(E("div")).addClass("chooser-option").appendTo($chooser);
$option_container.data("thing", thing);
const reuse_canvas = (width, height)=> {
let option_canvas = $option_container.find("canvas")[0];
if (option_canvas) {
if (option_canvas.width !== width) { option_canvas.width = width; }
if (option_canvas.height !== height) { option_canvas.height = height; }
} else {
option_canvas = make_canvas(width, height);
$option_container.append(option_canvas);
}
return option_canvas;
};
const update = () => {
const selected_color = get_theme() === "modern.css" ? "#0178d7" : "#000080"; // @TODO: get from a CSS variable
$option_container.css({
backgroundColor: is_chosen(thing) ? selected_color : ""
});
display(thing, is_chosen(thing), reuse_canvas);
};
update();
$G.on("redraw-tool-options", update);
$G.on("option-changed", update);
})(things[i]);
}
const onpointerover_while_pointer_down = (event)=> {
const option_container = event.target.closest(".chooser-option");
if (option_container) {
choose_thing($(option_container).data("thing"));
}
};
const ontouchmove_while_pointer_down = (event)=> {
const touch = event.originalEvent.changedTouches[0];
const target = document.elementFromPoint(touch.clientX, touch.clientY);
const option_container = target.closest(".chooser-option");
if (option_container) {
choose_thing($(option_container).data("thing"));
}
event.preventDefault();
};
$chooser.on("pointerdown click", (event)=> {
const option_container = event.target.closest(".chooser-option");
if (option_container) {
choose_thing($(option_container).data("thing"));
}
if (event.type === "pointerdown") {
// glide thru tool options
$chooser.on("pointerover", onpointerover_while_pointer_down);
$chooser.on("touchmove", ontouchmove_while_pointer_down);
}
});
$G.on("pointerup pointercancel", ()=> {
$chooser.off("pointerover", onpointerover_while_pointer_down);
$chooser.off("touchmove", ontouchmove_while_pointer_down);
});
});
return $chooser;
};
const $ChooseShapeStyle = () => {
const $chooser = $Choose(
[
{stroke: true, fill: false},
{stroke: true, fill: true},
{stroke: false, fill: true}
],
({stroke, fill}, is_chosen, reuse_canvas) => {
const sscanvas = reuse_canvas(39, 21);
const ssctx = sscanvas.ctx;
// border px inwards amount
let b = 5;
ssctx.fillStyle = is_chosen ? "#fff" : "#000";
if(stroke){
// just using a solid rectangle for the stroke
// so as not to have to deal with the pixel grid with strokes
ssctx.fillRect(b, b, sscanvas.width-b*2, sscanvas.height-b*2);
}
// go inward a pixel for the fill
b += 1;
ssctx.fillStyle = "#777";
if(fill){
ssctx.fillRect(b, b, sscanvas.width-b*2, sscanvas.height-b*2);
}else{
ssctx.clearRect(b, b, sscanvas.width-b*2, sscanvas.height-b*2);
}
return sscanvas;
},
({stroke, fill}) => {
$chooser.stroke = stroke;
$chooser.fill = fill;
},
({stroke, fill}) => $chooser.stroke === stroke && $chooser.fill === fill
).addClass("choose-shape-style");
$chooser.fill = false;
$chooser.stroke = true;
return $chooser;
};
const $choose_brush = $Choose(
(() => {
const brush_shapes = ["circle", "square", "reverse_diagonal", "diagonal"];
const circular_brush_sizes = [7, 4, 1];
const brush_sizes = [8, 5, 2];
const things = [];
brush_shapes.forEach((brush_shape)=> {
const sizes = brush_shape === "circle" ? circular_brush_sizes : brush_sizes;
sizes.forEach((brush_size)=> {
things.push({
shape: brush_shape,
size: brush_size,
});
});
});
return things;
})(),
(o, is_chosen, reuse_canvas) => {
const cbcanvas = reuse_canvas(10, 10);
const shape = o.shape;
const size = o.size;
stamp_brush_canvas(cbcanvas.ctx, 5, 5, shape, size);
if (is_chosen) {
invert_rgb(cbcanvas.ctx);
}
return cbcanvas;
}, ({shape, size}) => {
brush_shape = shape;
brush_size = size;
}, ({shape, size}) => brush_shape === shape && brush_size === size
).addClass("choose-brush");
const $choose_eraser_size = $Choose(
[4, 6, 8, 10],
(size, is_chosen, reuse_canvas) => {
const cecanvas = reuse_canvas(39, 16);
cecanvas.ctx.fillStyle = is_chosen ? "#fff" : "#000";
render_brush(cecanvas.ctx, "square", size);
return cecanvas;
},
size => {
eraser_size = size;
},
size => eraser_size === size
).addClass("choose-eraser");
const $choose_stroke_size = $Choose(
[1, 2, 3, 4, 5],
(size, is_chosen, reuse_canvas) => {
const w = 39, h = 12, b = 5;
const cscanvas = reuse_canvas(w, h);
const center_y = (h - size) / 2;
cscanvas.ctx.fillStyle = is_chosen ? "#fff" : "#000";
cscanvas.ctx.fillRect(b, ~~center_y, w - b*2, size);
return cscanvas;
},
size => {
stroke_size = size;
},
size => stroke_size === size
).addClass("choose-stroke-size");
const magnifications = [1, 2, 6, 8, 10];
const $choose_magnification = $Choose(
magnifications,
(scale, is_chosen, reuse_canvas) => {
const i = magnifications.indexOf(scale);
const secret = scale === 10; // 10x is secret
const chooser_canvas = ChooserCanvas(
"images/options-magnification.png",
is_chosen, // invert if chosen
39, (secret ? 2 : 13), // width, height of destination canvas
i*23, 0, 23, 9, // x, y, width, height from source image
8, 2, 23, 9, // x, y, width, height on destination
reuse_canvas,
);
if(secret){
$(chooser_canvas).addClass("secret-option");
}
return chooser_canvas;
},
scale => {
set_magnification(scale);
},
scale => scale === magnification
).addClass("choose-magnification")
.css({position: "relative"}); // positioning context for above `position: "absolute"` canvas
$choose_magnification.on("update", () => {
$choose_magnification
.find(".secret-option")
.parent()
.css({position: "absolute", bottom: "-2px", left: 0, opacity: 0});
});
const airbrush_sizes = [9, 16, 24];
const $choose_airbrush_size = $Choose(
airbrush_sizes,
(size, is_chosen, reuse_canvas) => {
const image_width = 72; // width of source image
const i = airbrush_sizes.indexOf(size); // 0 or 1 or 2
const l = airbrush_sizes.length; // 3
const is_bottom = (i === 2);
const shrink = 4 * !is_bottom;
const w = image_width / l - shrink * 2;
const h = 23;
const source_x = image_width / l * i + shrink;
return ChooserCanvas(
"images/options-airbrush-size.png",
is_chosen, // invert if chosen
w, h, // width, height of created destination canvas
source_x, 0, w, h, // x, y, width, height from source image
0, 0, w, h, // x, y, width, height on created destination canvas
reuse_canvas,
);
},
size => {
airbrush_size = size;
},
size => size === airbrush_size
).addClass("choose-airbrush-size");
const $choose_transparent_mode = $Choose(
[false, true],
(option, _is_chosen, reuse_canvas) => {
const sw = 35, sh = 23; // width, height from source image
const b = 2; // margin by which the source image is inset on the destination
const theme_folder = `images/${get_theme().replace(/\.css/, "")}`;
return ChooserCanvas(
`${theme_folder}/options-transparency.png`,
false, // never invert it
b+sw+b, b+sh+b, // width, height of created destination canvas
0, option ? 22 : 0, sw, sh, // x, y, width, height from source image
b, b, sw, sh, // x, y, width, height on created destination canvas
reuse_canvas,
);
},
option => {
tool_transparent_mode = option;
},
option => option === tool_transparent_mode
).addClass("choose-transparent-mode");