-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscribbly.js
385 lines (312 loc) · 10.9 KB
/
scribbly.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
/*!
* Scribbly.js
* @author Ayan_Dey
* @version 0.7.0
*/
(function (root, factory) {
if ( typeof define === "function" && define.amd ) {
define([], factory(root));
} else if ( typeof exports === "object" ) {
module.exports = factory(root);
} else {
root.Scribbly = factory(root);
}
})(typeof global !== "undefined" ? global : this.window || this.global, function (root) {
"use strict";
/**
* Variables
*/
let board,
ctx,
opts,
isDrawing = false,
isDragging = false,
startX,
startY,
curTool,
curStrokeSize,
curStrokeColor;
// Constructor
function Scribbly(options) {
// Default settings
let defaults = {
canvas: "",
canvasBg: "#FFFFFF",
lineThickness: 2,
lineColor: "#000000",
toolbar: true
};
// extend config
opts = extend(defaults, options || {} );
// initialize plugin
this.init();
}
/**
* Public Methods
*/
Scribbly.prototype.init = function () {
// Get the canvas ready to draw
board = document.getElementById(opts.canvas);
ctx = board.getContext("2d");
// Set background colour of canvas
board.style.backgroundColor = opts.canvasBg;
// Default stroke size
curStrokeSize = opts.lineThickness;
// Default brush color
curStrokeColor = opts.lineColor;
if(opts.toolbar) {
buildToolbar();
let clearBtn = document.getElementById("clearBtn");
let eraseBtn = document.getElementById("eraseBtn");
let markerBtn = document.getElementById("markerBtn");
let strokeSizeSlider = document.getElementById("strokeSizeSlider");
let colorPalette = document.getElementById("colorPalette");
let saveBtn = document.getElementById("saveBtn");
clearBtn.addEventListener("click", function () {
Scribbly.prototype.setTool("clear");
}, false);
eraseBtn.addEventListener("click", function () {
Scribbly.prototype.setTool("eraser", curStrokeSize);
}, false);
markerBtn.addEventListener("click", function () {
Scribbly.prototype.setTool("marker", curStrokeSize, curStrokeColor);
}, false);
strokeSizeSlider.addEventListener("input", function () {
curStrokeSize = this.value;
}, false);
colorPalette.addEventListener("input", function () {
curStrokeColor = this.value;
}, false);
saveBtn.addEventListener("click", function () {
Scribbly.prototype.save();
})
}
// Add mouse event listeners to canvas element
board.addEventListener("mousedown", press, false);
board.addEventListener("mousemove", drag, false);
board.addEventListener("mouseup", release);
board.addEventListener("mouseout", cancel, false);
// Add touch event listeners to canvas element
board.addEventListener("touchstart", press, false);
board.addEventListener("touchmove", drag, false);
board.addEventListener("touchend", release, false);
board.addEventListener("touchcancel", cancel, false);
};
// Set a specific tool to use on canvas
Scribbly.prototype.setTool = function(tool, size, color) {
if(tool === "clear") {
Scribbly.prototype.clear();
}
else if(tool === "eraser") {
curTool = tool;
curStrokeSize = size;
}
else if(tool === "marker") {
curTool = tool;
curStrokeSize = size;
curStrokeColor = color;
}
};
// Clear the canvas
Scribbly.prototype.clear = function() {
ctx.clearRect(0, 0, board.width, board.height);
curTool = "marker";
};
/**
* Save the image
* Currently save the image by downloading it
* ToDo Decide whether this function should be a part of setTool method
*/
Scribbly.prototype.save = function (filename) {
if(!filename) {
filename = "My Scribble";
}
//get the current ImageData for the canvas.
let data = ctx.getImageData(0, 0, board.width, board.height);
//store the current globalCompositeOperation
let compositeOperation = ctx.globalCompositeOperation;
//set to draw behind current content
ctx.globalCompositeOperation = "destination-over";
//set background color
ctx.fillStyle = opts.canvasBg;
//draw background / rect on entire canvas
ctx.fillRect(0, 0, board.width, board.height);
//get the image data from the canvas
let imageData = board.toDataURL("image/png");
//clear the canvas
ctx.clearRect (0, 0, board.width, board.height);
//restore it with original / cached ImageData
ctx.putImageData(data, 0,0);
//reset the globalCompositeOperation to what it was
ctx.globalCompositeOperation = compositeOperation;
let image = new Image();
image.src = imageData;
let link = document.createElement('a');
link.setAttribute("href", image.src);
link.setAttribute("download", filename+".png");
link.style.display = "none";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};
/**
* Private Methods
*/
/**
* Merge two or more objects. Returns a new object.
*/
const extend = function () {
// Variables
let extended = {};
let deep = false;
let i = 0;
let length = arguments.length;
// Check if a deep merge
if ( Object.prototype.toString.call( arguments[0] ) === "[object Boolean]" ) {
deep = arguments[0];
i++;
}
// Merge the object into the extended object
let merge = function (obj) {
for ( let prop in obj ) {
if ( Object.prototype.hasOwnProperty.call( obj, prop ) ) {
// If deep merge and property is an object, merge properties
if ( deep && Object.prototype.toString.call(obj[prop]) === "[object Object]" ) {
extended[prop] = extend( true, extended[prop], obj[prop] );
} else {
extended[prop] = obj[prop];
}
}
}
};
// Loop through each object and conduct a merge
for ( ; i < length; i++ ) {
let obj = arguments[i];
merge(obj);
}
return extended;
};
const buildToolbar = function () {
let rect = board.getBoundingClientRect();
let toolbarWrapper = document.createElement("div");
let clearBtn = document.createElement("div");
let eraseBtn = document.createElement("div");
let markerBtn = document.createElement("div");
let strokeSizeSlider = document.createElement("input");
let colorPalette = document.createElement("input");
let saveBtn = document.createElement("div");
toolbarWrapper.setAttribute("id", "toolbarWrapper");
clearBtn.setAttribute("id", "clearBtn");
eraseBtn.setAttribute("id", "eraseBtn");
markerBtn.setAttribute("id", "markerBtn");
strokeSizeSlider.setAttribute("id", "strokeSizeSlider");
colorPalette.setAttribute("id", "colorPalette");
saveBtn.setAttribute("id", "saveBtn");
colorPalette.setAttribute("type", "color");
colorPalette.setAttribute("value", opts.lineColor);
strokeSizeSlider.setAttribute("type", "range");
strokeSizeSlider.setAttribute("min", "1");
strokeSizeSlider.setAttribute("max", "50");
strokeSizeSlider.setAttribute("value", opts.lineThickness);
clearBtn.setAttribute("class", "btn");
eraseBtn.setAttribute("class", "btn");
markerBtn.setAttribute("class", "btn");
saveBtn.setAttribute("class", "btn");
clearBtn.textContent = "Clear";
eraseBtn.textContent = "Eraser";
markerBtn.textContent = "Marker";
saveBtn.textContent = "Save";
toolbarWrapper.style.width = rect.width + "px";
toolbarWrapper.style.left = rect.left + 'px';
toolbarWrapper.appendChild(clearBtn);
toolbarWrapper.appendChild(eraseBtn);
toolbarWrapper.appendChild(markerBtn);
toolbarWrapper.appendChild(strokeSizeSlider);
toolbarWrapper.appendChild(colorPalette);
toolbarWrapper.appendChild(saveBtn);
document.body.appendChild(toolbarWrapper);
};
// Get the coordinates of the mouse click
const getMousePos = function(evt) {
let rect = board.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
};
};
// Get the coordinates of the tap
const getTouchPos = function(evt) {
let rect = board.getBoundingClientRect();
return {
x: evt.touches[0].clientX - rect.left,
y: evt.touches[0].clientY - rect.top
};
};
// Mouse press/ touchstart event
const press = function(e) {
let pos;
isDrawing = true;
if(e.type === "touchstart") {
pos = getTouchPos(e);
}
else {
pos = getMousePos(e);
}
startX = pos.x;
startY = pos.y;
draw(startX, startY);
};
// Mouse/ touch drag event
const drag = function(e) {
if(isDrawing) {
let pos;
isDragging = true;
if(e.type === "touchmove") {
pos = getTouchPos(e);
}
else {
pos = getMousePos(e);
}
draw(pos.x, pos.y);
}
};
// Mouse release/ touchend event
const release = function(e) {
isDrawing = false;
isDragging = false;
startX = null;
startY = null;
};
// When mouse or touch goes out of the canvas
const cancel = function(e) {
isDrawing = false;
isDragging = false;
};
const draw = function(x, y) {
ctx.lineJoin = "round";
ctx.lineWidth = curStrokeSize;
ctx.strokeStyle = curStrokeColor;
if(curTool === "eraser") {
ctx.strokeStyle = opts.canvasBg;
}
else if(curTool === "marker") {
ctx.strokeStyle = curStrokeColor;
}
ctx.beginPath();
if(isDragging) {
ctx.moveTo(startX, startY);
startX = x;
startY = y;
}
else {
ctx.moveTo(x, y);
}
ctx.lineTo(x, y);
ctx.closePath();
ctx.stroke();
};
/**
* Public APIs
*/
return Scribbly;
});