-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
428 lines (321 loc) · 10 KB
/
script.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
window.addEventListener('load', main);
async function main(){
//global variables--------------------------------------------
//consts
const width = 500;
const height = 500;
const byteSize = height * width * 4;
//variables
let clicked = false;
var points = [];
var currentBrushR;
var currentBrushAlpha;
var eraseMode;
var currentLayer = 0;
var scaleConstant = 1.0;
var currentTool; //0=paintbrush 1=eraser
var sizeConstant = 100
var sourceX, sourceY, sourceLeft, sourceTop;
var lastPosX = -1;
var lastPosY = -1;
//core pointers to shared memory - be careful with these
var instance;
var memory;
var pointer;
//element bindings--------------------------------------------
//canvas binds
const canvas = document.getElementById('c');
var rect = canvas.getBoundingClientRect();
var ctx = canvas.getContext('2d');
//area outside of canvas
const windowShim = document.getElementById('windowShim');
//main draw area, contains shim and canvas
const drawArea = document.getElementById('drawArea');
//color selection control
const colorPicker = document.getElementById('brushC');
//brush selection control
const circleButton = document.getElementById("circlebrush");
const squareButton = document.getElementById("squarebrush");
const cursorButton = document.getElementById("cursorbrush");
const eraseModeCheckbox = document.getElementById("eraseMode");
//size control
const sizeSlider = document.getElementById('sizeSlider');
const sizeStatus = document.getElementById('size');
//alpha control
const alphaSlider = document.getElementById('alphaSlider');
const alphaStatus = document.getElementById('alpha');
//layer control ui
const addLayer = document.getElementById('addLayer');
const hideLayer = document.getElementById('hideLayer');
const layersPicker = document.getElementById('layers');
const layerStatusElement = document.getElementById('layerStatus');
const clearBtn = document.getElementById('clear');
//reset button
const resetBtn = document.getElementById('reset');
//save button
const saveBtn = document.getElementById('save');
//event listeners--------------------------------------------
//shortcut keys
document.addEventListener('keydown', handleShortcut);
//window change events
window,addEventListener('resize', updateBoundingRect);
//layer handlers
addLayer.addEventListener('click', addLayerHandler);
hideLayer.addEventListener('click', hideLayerHandler);
layersPicker.addEventListener('change', changeSelectedLayer);
clearBtn.addEventListener('click', handleClearLayer);
//main draw handlers
canvas.addEventListener('mousemove', drawOnCanvas);
canvas.addEventListener('mouseup', handleMouseUp);
canvas.addEventListener('mousedown', handleMouseDown);
canvas.addEventListener('mouseleave', handlePathEnd);
//tool handlers
circleButton.addEventListener('change', readRadio);
squareButton.addEventListener('change', readRadio);
eraseModeCheckbox.addEventListener('change', readEraseMode);
//recalculate bounds on scroll
drawArea.addEventListener('scroll', updateBoundingRect);
//brush controls
sizeSlider.addEventListener('change', updateSize);
alphaSlider.addEventListener('change', updateAlpha);
colorPicker.addEventListener('change', updateColor);
//reset global state
resetBtn.addEventListener('click', handleFullReset);
//save
saveBtn.addEventListener('click', runExport);
//core functions--------------------------------------------
async function entry(){
//init system
const b2p = function bytesToPages(bytes) { return Math.ceil(bytes / 64_000); }
memory = new WebAssembly.Memory({initial: 1000});
let resp = await fetch("buffer.wasm");
let bytes = await resp.arrayBuffer();
const lI = await WebAssembly.instantiate(bytes, {
env: { memory }
});
instance = lI.instance;
console.log(instance);
//allocate canvas and init
pointer = instance.exports.allocate(width, height);
instance.exports.initSystem();
readInputs();
}
entry(); //run core function on init!
function readInputs(){
//place canvas buffer on screen
updateCanvas();
//update color picker
updateColor();
//read current brush
readRadio();
//check erase mode
readEraseMode();
//check size control
updateSize();
//check alpha
updateAlpha();
//update list of layers
updateLayersHandler();
}
function updateColor(){
const hexValue = colorPicker.value.substring(1);
const aRgbHex = hexValue.match(/.{1,2}/g);
const r = parseInt(aRgbHex[0], 16);
const g = parseInt(aRgbHex[1], 16); //rgb
const b = parseInt(aRgbHex[2], 16);
instance.exports.setColor(r, g, b);
}
function readRadio(){
if(circleButton.checked){
updateColor();
currentTool = 0;
}
if(squareButton.checked){
updateColor();
currentTool = 1;
}
if(cursorButton.checked){
//TODO: make tool do nothing, equiv of "put down brush"
}
instance.exports.setBrushProperties(currentBrushR, currentTool, currentBrushAlpha, eraseMode);
}
function readEraseMode(){
if(eraseModeCheckbox.checked){
eraseMode = 1;
} else {
eraseMode = 0;
}
//do this as a hack, to init a full refresh of all properties. Will have to refactor later
readRadio();
}
function updateSize(){
currentBrushR = sizeSlider.value;
sizeStatus.innerHTML = currentBrushR;
instance.exports.setBrushProperties(currentBrushR, currentTool, currentBrushAlpha, eraseMode);
}
function updateAlpha(){
currentBrushAlpha = alphaSlider.value;
alphaStatus.innerHTML = currentBrushAlpha;
instance.exports.setBrushProperties(currentBrushR, currentTool, currentBrushAlpha, eraseMode);
}
//layer control
function getLayerStatus(){
const layerArrayAddress = instance.exports.layerArrayAddress();
const layerStatus = new Uint8Array(memory.buffer, layerArrayAddress, 10);
return layerStatus;
}
function changeSelectedLayer(){
currentLayer = layers.value;
instance.exports.selectActiveLayer(currentLayer);
updateLayerVisibility();
}
function addLayerHandler(){
instance.exports.addLayer();
updateLayersHandler();
updateLayerVisibility();
}
function hideLayerHandler(){
instance.exports.toggleLayerVisibility(currentLayer);
instance.exports.blendLayers();
updateCanvas();
updateLayerVisibility();
}
function updateLayerVisibility(){
const layerStatus = getLayerStatus();
if(layerStatus[currentLayer] == 2){
layerStatusElement.innerHTML = 'layer is not hidden';
} else if (layerStatus[currentLayer] == 1){
layerStatusElement.innerHTML = 'layer is hidden';
} else {
layerStatusElement.innerHTML = 'oops'
}
}
function updateLayersHandler(){
const layerStatus = getLayerStatus();
//clear layers field
layersPicker.innerHTML = ' ';
for(let i = 0; i < layerStatus.length; i++){
if(layerStatus[i] == 0) continue;
//create new option value
const layerOption = document.createElement('option');
layerOption.innerHTML = `layer ${i}`;
layerOption.value = i;
layersPicker.appendChild(layerOption);
}
//reselect
layers.value = currentLayer;
}
function handleShortcut(e){
if(e.keyCode == 187 && e.shiftKey){
zoomIn();
}
if(e.keyCode == 189 && e.shiftKey){
zoomOut();
}
}
function zoomIn(){
scaleConstant += 0.1;
canvas.style.transform = `scale(${scaleConstant})`;
sizeConstant += 100
windowShim.style.padding= `${sizeConstant}px`;
drawArea.scroll(sizeConstant + (lastPosX - 250), sizeConstant + (lastPosX - 250));
rect = canvas.getBoundingClientRect();
}
function zoomOut(){
scaleConstant -= 0.1;
canvas.style.transform = `scale(${scaleConstant})`;
sizeConstant -= 100
windowShim.style.padding= `${sizeConstant}px`;
drawArea.scroll(sizeConstant - (lastPosX - 250), sizeConstant - (lastPosX - 250));
rect = canvas.getBoundingClientRect();
}
function handleMouseDown(e){
let x = (event.clientX - rect.left) / scaleConstant;
let y = (event.clientY - rect.top) / scaleConstant;
sourceX = e.clientX;
sourceY = e.clientY;
sourceLeft = drawArea.scrollLeft;
sourceTop = drawArea.scrollTop;
clicked = true;
if(!cursorButton.checked){
instance.exports.startPath(x, y);
}
}
function handleMouseUp(e){
if(!cursorButton.checked){
updateCanvas();
instance.exports.endPath();
}
clicked = false;
}
function drawOnCanvas(event){
let x = (event.clientX - rect.left) / scaleConstant;
let y = (event.clientY - rect.top) / scaleConstant;
lastPosX = x;
lastPosY = y;
if(!cursorButton.checked){
instance.exports.setOverlay(x, y);
if(clicked){
points.push([x, y]);
if(points.length > 1){ //flosting avg
var ax = 0;
var ay = 0;
for(let i = 0; i < points.length; i++){
ax += points[i][0];
ay += points[i][1];
}
ax /= points.length;
ay /= points.length;
instance.exports.addPoint(x, y);
points = [];
}
}
updateCanvas()
} else {
if(clicked){
drawArea.scroll(sourceLeft - (event.clientX - sourceX), sourceTop - (event.clientY - sourceY))
}
}
}
function handlePathEnd(e){
if(clicked){
clicked = false;
instance.exports.endPath();
}
instance.exports.clearOverlay();
updateCanvas();
}
function handleClearLayer(){
instance.exports.clearCurrentLayer();
updateCanvas();
}
function handleFullReset(){
//free old memory
instance.exports.dealloc()
//alloc canvas buffer
pointer = instance.exports.allocate(width, height);
//initalize rest of system
instance.exports.initSystem();
//update canvas
updateCanvas();
//read inputs and update system to match
readInputs();
}
function runExport(){
const dataURL = canvas.toDataURL("image/png");
const a = document.createElement('a');
a.href = dataURL;
a.download = "hello.png";
a.click();
}
//helper functions--------------------------------------------
function updateBoundingRect(){
rect = canvas.getBoundingClientRect();
}
function updateCanvas(){
instance.exports.blendLayers();
const usub = new Uint8ClampedArray(memory.buffer, pointer, byteSize);
const img = new ImageData(usub, width, height);
ctx.putImageData(img, 0, 0);
}
}