forked from tsand/responsive-sketchpad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponsive-sketchpad.js
219 lines (176 loc) · 5.55 KB
/
responsive-sketchpad.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
$.fn.sketchpad = function(options) {
// Canvas info
var canvas = this;
var ctx = $(this)[0].getContext('2d');
// Default aspect ratio
var aspectRatio = 1;
// For storing strokes
var strokes = [];
// Whether or not currently drawing
var sketching = false;
// Default Context
var lineColor = 'black';
var lineSize = 20;
var lineCap = 'round';
var lineJoin = 'round';
var lineMiterLimit = 10;
// Array for storing strokes that were undone
var undo = [];
// Resize canvas with window
$(window).resize(function(e) {
var width = canvas.parent().width();
var height = width / aspectRatio;
setSize(width, height);
redraw();
});
// Return the mouse/touch location
function getCursor(element, event) {
var cur = {x: 0, y: 0};
if (event.type.indexOf('touch') !== -1) {
cur.x = event.originalEvent.touches[0].pageX;
cur.y = event.originalEvent.touches[0].pageY;
} else {
cur.x = event.pageX ;
cur.y = event.pageY;
}
return {
x: (cur.x - $(element).offset().left) / $(element).width(),
y: (cur.y - $(element).offset().top) / $(element).height()
}
}
// Set the canvas size
function setSize(w, h) {
canvas.width(w);
canvas.height(h);
canvas[0].setAttribute('width', w);
canvas[0].setAttribute('height', h);
}
// On mouse down, create new stroke, push start location
var startEvent = 'mousedown touchstart ';
canvas.on(startEvent, function(e) {
if (e.type == 'touchstart') {
e.preventDefault();
} else {
e.originalEvent.preventDefault();
}
sketching = true;
undo = []; // Clear undo strokes
strokes.push({
stroke: [],
color: lineColor,
size: lineSize / $(this).width(),
cap: lineCap,
join: lineJoin,
miterLimit: lineMiterLimit
});
var canvas = getCursor(this, e);
strokes[strokes.length - 1].stroke.push({
x: canvas.x,
y: canvas.y
});
redraw();
});
// On mouse move, record movements
var moveEvent = 'mousemove touchmove ';
canvas.on(moveEvent, function(e) {
var canvas = getCursor(this, e);
if (sketching) {
strokes[strokes.length - 1].stroke.push({
x: canvas.x,
y: canvas.y
});
redraw();
}
});
// On mouse up, end stroke
var endEvent = 'mouseup mouseleave touchend ';
canvas.on(endEvent, function(e) {
sketching = false;
});
function redraw() {
var width = $(canvas).width();
var height = $(canvas).height();
ctx.clearRect(0, 0, width, height); // Clear Canvas
for (var i = 0; i < strokes.length; i++) {
var stroke = strokes[i].stroke;
ctx.beginPath();
for (var j = 0; j < stroke.length - 1; j++) {
ctx.moveTo(stroke[j].x * width, stroke[j].y * height);
ctx.lineTo(stroke[j + 1].x * width, stroke[j + 1].y * height);
}
ctx.closePath();
ctx.strokeStyle = strokes[i].color;
ctx.lineWidth = strokes[i].size * width;
ctx.lineJoin = strokes[i].join;
ctx.lineCap = strokes[i].cap;
ctx.miterLimit = strokes[i].miterLimit;
ctx.stroke()
}
}
function init() {
if (options.data) {
aspectRatio = typeof options.data.aspectRatio !== 'undefined' ? options.data.aspectRatio : aspectRatio;
strokes = typeof options.data.strokes !== 'undefined' ? options.data.strokes : [];
} else {
aspectRatio = typeof options.aspectRatio !== 'undefined' ? options.aspectRatio : aspectRatio;
}
var canvasColor = typeof options.canvasColor !== 'undefined' ? options.canvasColor : '#fff';
canvas.css('background-color', canvasColor);
var locked = typeof options.locked !== 'undefined' ? options.locked : false;
if (locked) {
canvas.unbind(startEvent + moveEvent + endEvent);
} else {
canvas.css('cursor', 'crosshair');
}
// Set canvas size
var width = canvas.parent().width();
var height = width / aspectRatio;
setSize(width, height);
redraw();
}
init();
this.json = function() {
return JSON.stringify({
aspectRatio: aspectRatio,
strokes: strokes
});
};
this.jsonLoad = function(json) {
var array = JSON.parse(json);
aspectRatio = array.aspectRatio;
strokes = array.strokes;
redraw()
};
this.getImage = function() {
return '<img src="' + canvas[0].toDataURL("image/png") + '"/>';
};
this.getLineColor = function() {
return lineColor;
};
this.setLineColor = function(color) {
lineColor = color;
};
this.getLineSize = function() {
return lineSize;
};
this.setLineSize = function(size) {
lineSize = size;
};
this.undo = function() {
if (strokes.length > 0) {
undo.push(strokes.pop());
redraw();
}
};
this.redo = function() {
if (undo.length > 0) {
strokes.push(undo.pop());
redraw();
}
};
this.clear = function() {
strokes = [];
redraw();
};
return this;
};