-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathartboard-1.1.2.js
501 lines (472 loc) · 16 KB
/
artboard-1.1.2.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*!
ArtboardJS v1.1.2
(c) 2017 Depth Development. http://depthdev.com
License: MIT
!*/
(function(window, document) {
'use strict';
window.Artboard = function() {
const canvas = arguments[0];
const options = typeof arguments[1] === 'object' ? arguments[1] : false;
// Reject if not supported
if (!canvas.getContext) {
const errorCallback = arguments[arguments.length - 1];
if (typeof errorCallback === 'function') {
errorCallback();
}
return false;
}
// Setup the canvas
let multiTouch = true;
let rainbow = true;
let canvasTopToPageTop = 0;
let canvasLeftToPageLeft = 0;
let x = [];
let y = [];
let radius = 10;
const piSq = Math.PI * 2;
let degree = 0;
let done = null;
// Setup the context
const ctx = canvas.getContext('2d');
ctx.fillStyle = '#000000';
ctx.shadowColor = '#000000';
ctx.shadowOffsetX = 0;
ctx.shadowOffsetY = 0;
ctx.shadowBlur = 10;
// Other
let draw = null; // Placeholder since draw gets referenced before it would otherwise be defined
let isMouseDown = false;
// History
const history = {
timeline: [],
createEntry() {
history.timeline.push([]);
},
undoCount: 0,
save() {
if (history.timeline.length) {
window.localStorage.artboard = JSON.stringify({width:canvas.width,height:canvas.height,timeline:history.timeline});
return true;
} else {
return false;
}
},
saveAs(str, devCallback) {
if (history.timeline.length && /^[a-zA-Z0-9_$]+$/.test(str)) {
window.localStorage[`artboard_${str}`] = JSON.stringify({width:canvas.width,height:canvas.height,timeline:history.timeline});
return devCallback ? devCallback(true) : true;
} else {
return devCallback ? devCallback(false) : false;
}
},
saved() {
const a = [];
for (const p in window.localStorage) {
if (/^artboard_/.test(p)) {
a.push(p.replace(/^artboard_/,''));
}
}
return a;
},
backup() {
return {width:canvas.width,height:canvas.height,timeline:history.timeline};
},
restore(data, devCallback) {
if (data && !(data instanceof Event)) {
// If type of is string, then developer/user is requesting it by name
if (typeof data === 'string') {
if (window.localStorage[`artboard_${data}`]) {
const parsedData = JSON.parse(window.localStorage[`artboard_${data}`]);
canvas.width = parsedData.width;
canvas.height = parsedData.height;
history.timeline = parsedData.timeline;
history.undoCount = 0;
history.change();
return devCallback ? devCallback(true) : true;
} else {
return devCallback ? devCallback(false) : false;
}
}
// Otherwise; it should be object literal passed in from the developer
else {
try {
canvas.width = data.width;
canvas.height = data.height;
history.timeline = data.timeline;
history.undoCount = 0;
history.change();
return devCallback ? devCallback(true) : true;
} catch(e) {
return devCallback ? devCallback(false) : false;
}
}
}
// If no name or object is passed in, see if the artboard is saved generically
else if (window.localStorage.artboard) {
const parsedData = JSON.parse(window.localStorage.artboard);
canvas.width = parsedData.width;
canvas.height = parsedData.height;
history.timeline = parsedData.timeline;
history.undoCount = 0;
history.change();
return devCallback ? devCallback(true) : true;
}
// Otherwise, return `false`
else {
return devCallback ? devCallback(false) : false;
}
},
deleteSaved() {
delete window.localStorage.artboard;
for (const p in window.localStorage) {
if (/^artboard_/.test(p)) {
delete window.localStorage[p];
}
}
},
rewrite() {
//If re-writing over history
if (history.undoCount) {
history.timeline.splice(history.timeline.length - 1 - history.undoCount, history.undoCount);
history.undoCount = 0;
}
},
log() {
history.timeline[history.timeline.length - 1].push({
multiTouch,
rainbow,
color: ctx.fillStyle,
x,
y,
radius,
degree,
glow: ctx.shadowBlur
});
},
settings() {
return {
multiTouch,
rainbow,
color: ctx.fillStyle,
radius,
degree,
glow: ctx.shadowBlur
}
},
change() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
let i = 0;
let ii = 0;
const l = history.timeline.length - history.undoCount;
let ll = history.timeline[i].length;
for (;i<l;i++) {
ii = 0;
ll = history.timeline[i].length;
for (;ii<ll;ii++) {
multiTouch = history.timeline[i][ii].multiTouch;
rainbow = history.timeline[i][ii].rainbow;
ctx.fillStyle = history.timeline[i][ii].color;
x = history.timeline[i][ii].x;
y = history.timeline[i][ii].y;
radius = history.timeline[i][ii].radius;
degree = history.timeline[i][ii].degree;
ctx.shadowBlur = history.timeline[i][ii].glow;
ctx.shadowColor = ctx.fillStyle;
draw(true);
}
}
done();
},
undo() {
if (history.undoCount !== history.timeline.length) {
history.undoCount++;
history.change();
}
},
redo() {
if (history.undoCount > 0) {
history.undoCount--;
history.change();
}
},
stopPlaying: false,
isPlaying: false,
play() {
if (!history.timeline.length) {
return;
}
history.isPlaying = true;
ctx.clearRect(0, 0, canvas.width, canvas.height);
let i = 0;
let ii = 0;
const l = history.timeline.length;
let ll = history.timeline[i].length;
function playing() {
multiTouch = history.timeline[i][ii].multiTouch;
rainbow = history.timeline[i][ii].rainbow;
ctx.fillStyle = history.timeline[i][ii].color;
x = history.timeline[i][ii].x;
y = history.timeline[i][ii].y;
radius = history.timeline[i][ii].radius;
degree = history.timeline[i][ii].degree;
ctx.shadowBlur = history.timeline[i][ii].glow;
ctx.shadowColor = ctx.fillStyle;
draw(true);
ii++;
if (history.stopPlaying) {
history.stopPlaying = false;
return;
} else if (ii === ll) {
if (i + 1 !== l) {
i++;
ii = 0;
ll = history.timeline[i].length;
window.requestAnimationFrame(playing);
} else {
history.stopPlaying = false;
history.isPlaying = false;
done();
}
} else {
window.requestAnimationFrame(playing);
}
}
playing();
},
stop() {
history.stopPlaying = true;
window.setTimeout(() => {
history.isPlaying = false;
history.stopPlaying = false;
history.change();
// done(); Don't need this because history.change(); contains the done() function
}, 100);
},
size() {
return JSON.stringify({width:canvas.width,height:canvas.height,timeline:history.timeline}).length;
}
};
// Draw
draw = (noLog) => {
if (rainbow) {
ctx.fillStyle = 'hsl(' + degree + ',100%,50%)';
ctx.shadowColor = 'hsl(' + degree + ',100%,50%)';
degree++;
}
let i = 0;
const l = x.length;
for (;i<l;i++) {
ctx.beginPath();
ctx.arc(x[i], y[i], radius, 0, piSq, false);
ctx.fill();
if (!multiTouch) {
break;
}
}
if (!noLog) {
history.log();
}
};
// Start
function start(e) {
e.preventDefault();
e.stopPropagation();
// Get canvas distance from page top and left
canvasTopToPageTop = 0;
canvasLeftToPageLeft = 0;
let elem = canvas;
while (elem) {
canvasTopToPageTop += elem.offsetTop;
canvasLeftToPageLeft += elem.offsetLeft;
elem = elem.offsetParent;
}
}
// Touches
function touches(touchesArray) {
x = [];
y = [];
let i = 0;
const l = touchesArray.length;
for (;i<l;i++) {
x.push(touchesArray[i].pageX - canvasLeftToPageLeft);
y.push(touchesArray[i].pageY - canvasTopToPageTop);
}
}
// Touch Start
function touchStart(e) {
e.preventDefault();
e.stopPropagation();
if (history.isPlaying) { return; }
start(e);
touches(e.touches);
history.createEntry();
draw();
history.rewrite();
}
// Touch Move
function touchMove(e) {
e.preventDefault();
e.stopPropagation();
if (history.isPlaying) { return; }
touches(e.touches);
draw();
}
// Mouse Down
function mouseDown(e) {
e.preventDefault();
e.stopPropagation();
if (history.isPlaying) { return; }
isMouseDown = true;
start(e);
x = [e.pageX - canvasLeftToPageLeft];
y = [e.pageY - canvasTopToPageTop];
history.createEntry();
draw();
history.rewrite();
}
// Mouse Move
function mouseMove(e) {
e.preventDefault();
e.stopPropagation();
if (history.isPlaying) { return; }
if (!isMouseDown) { return; }
x = [e.pageX - canvasLeftToPageLeft];
y = [e.pageY - canvasTopToPageTop];
draw();
}
// Mouse Up
function mouseUp(e) {
if (!isMouseDown) { return; }
isMouseDown = false;
e.preventDefault();
e.stopPropagation();
}
// Reset Hard
function resetHard(devObj) {
if (history.isPlaying) { return; }
const o = devObj || {};
canvas.width = o.width || document.documentElement.clientWidth;
canvas.height = o.height || canvas.width * (9/16);
ctx.clearRect(0, 0, canvas.width, canvas.height);
multiTouch = o.multiTouch === false ? false : true;
rainbow = o.rainbow === false ? false : true;
ctx.fillStyle = o.color || '#000000';
ctx.shadowColor = ctx.fillStyle;
ctx.shadowBlur = o.glow >= 0 ? o.glow : 10;
degree = 0;
radius = o.radius >= 0 ? o.radius : 10;
done = o.done || function(){};
history.timeline = [];
}
// Reset
function reset(devObj) {
if (history.isPlaying) {
return;
}
// Get current settings
const currentSettings = {
multiTouch,
rainbow,
color: ctx.fillStyle,
glow: ctx.shadowBlur,
degree,
radius,
done
};
const o = devObj || {};
canvas.width = o.width || canvas.width;
canvas.height = o.height || canvas.height;
ctx.clearRect(0, 0, canvas.width, canvas.height);
history.timeline = [];
// Replace wiped out settings
multiTouch = currentSettings.multiTouch;
rainbow = currentSettings.rainbow;
ctx.fillStyle = currentSettings.color;
ctx.shadowColor = ctx.fillStyle;
ctx.shadowBlur = currentSettings.glow;
degree = currentSettings.degree;
radius = currentSettings.radius;
done = currentSettings.done;
}
// Clear
function clear() {
if (history.isPlaying) {
return;
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
history.timeline = [];
}
// Adjust
function adjust(devObj) {
if (history.isPlaying) {
return;
}
const o = devObj || {};
multiTouch = o.multiTouch !== undefined ? o.multiTouch : multiTouch;
rainbow = o.rainbow !== undefined ? o.rainbow : rainbow;
ctx.fillStyle = o.color || ctx.fillStyle;
ctx.shadowColor = ctx.fillStyle;
ctx.shadowBlur = o.glow >= 0 ? o.glow : ctx.shadowBlur;
radius = o.radius >= 0 ? o.radius : radius;
done = o.done || done;
}
// Image
function image(str) {
this.a = document.createElement('a');
this.a.href = canvas.toDataURL('image/png');
this.a.download = str && typeof str === 'string' ? str : 'ArtboardJS';
this.a.click();
delete this.a;
}
// Event listeners
canvas.addEventListener('touchstart', touchStart);
canvas.addEventListener('touchmove', touchMove);
canvas.addEventListener('mousedown', mouseDown);
canvas.addEventListener('mousemove', mouseMove);
document.addEventListener('mouseup', mouseUp);
// Destroy
function destroy() {
history.timeline = [];
canvas.removeEventListener('touchstart', touchStart);
canvas.removeEventListener('touchmove', touchMove);
canvas.removeEventListener('mousedown', mouseDown);
canvas.removeEventListener('mousemove', mouseMove);
document.removeEventListener('mouseup', mouseUp);
}
// Init
resetHard(options);
// Return
return {
adjust, // Adjust effects only
backup: history.backup, // Returns an object literal backup to the developer
clear, // Clears out the canvas and history, but keeps all settings and the size
deleteSaved: history.deleteSaved, // Deletes all saved artboards
destroy, // Remove all listeners including window and document
hardReset: resetHard, // Resets everything to the defaults unless and equivalent adjust object properties and/or reset object properties override them
image, // Creates a PNG image of the canvas, renames it to an optional string provided, and downloads it
play: history.play, // Clears the canvas and re-paints the users whole picture exactly as drawn with animation
redo: history.redo, // Redo one or more moves and returns the current state
reset, // Resets the canvas and can re-adjust the width/height
restore: history.restore, // Restores a saved artboard if one exists in localStorage; returns a provided callback with the value `true` or just `true`, otherwise it returns a provided callback with the value of `false` or just `false`
save: history.save, // Saves the current artboard to localStorage to be restored later with the restore method
saveAs: history.saveAs, // Saves the current named artboard to localStorage to be restored later with the restore method
saved: history.saved, // Returns an array of the user-defined named artboards
settings: history.settings, // Returns an object with the current settings (most useful for the UI after an undo/redo)
size: history.size, // Returns the size of the current artboard in bytes in it's saved format (which is somewhat larger than the JavaScript multi-dimensional array in memory)
stop: history.stop, // Stops the current animation playback
undo: history.undo // Undo one or more moves and returns the current state
};
/*
Adjustable properties:
- width
- height
- multiTouch
- rainbow
- color
- glow
- radius
- done
*/
}
})(window,document);