forked from jakubfiala/atrament
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatrament.js
More file actions
300 lines (248 loc) · 7.24 KB
/
Copy pathatrament.js
File metadata and controls
300 lines (248 loc) · 7.24 KB
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
//make a class for Point
class Point {
constructor(x, y) {
if (arguments.length < 2) throw new Error('not enough coordinates for Point.');
this._x = x;
this._y = y;
}
get x() {
return this._x;
}
get y() {
return this._y;
}
set x(x) {
this._x = x;
}
set y(y) {
this._y = y;
}
set(x, y) {
if (arguments.length < 2) throw new Error('not enough coordinates for Point.set');
this._x = x;
this._y = y;
}
}
//make a class for the mouse data
class Mouse extends Point {
constructor() {
super(0, 0);
this._down = false;
this._px = 0;
this._py = 0;
}
get down() {
return this._down;
}
set down(d) {
this._down = d;
}
get x() {
return this._x;
}
get y() {
return this._y;
}
set x(x) {
this._x = x;
}
set y(y) {
this._y = y;
}
get px() {
return this._px;
}
get py() {
return this._py;
}
set px(px) {
this._px = px;
}
set py(py) {
this._py = py;
}
}
class Atrament {
constructor(selector, width, height, color) {
if (!document) throw new Error('no DOM found');
//get canvas element
if (selector instanceof Node && selector.tagName === 'CANVAS') this.canvas = selector;
else if (typeof selector === 'string') this.canvas = document.querySelector(selector);
else throw new Error(`can\'t look for canvas based on \'${selector}\'`);
if (!this.canvas) throw new Error('canvas not found');
//set external canvas params
this.canvas.width = width ? width : 500;
this.canvas.height = height ? height : 500;
this.canvas.style.cursor = 'crosshair';
//create a mouse object
this.mouse = new Mouse();
//mousemove handler
let mouseMove = (mousePosition) => {
mousePosition.preventDefault();
//get position
let mx, my;
if (mousePosition.changedTouches) { // touchscreens
mx = mousePosition.changedTouches[0].pageX - mousePosition.target.offsetLeft;
my = mousePosition.changedTouches[0].pageY - mousePosition.target.offsetTop;
}
else if (mousePosition.layerX || mousePosition.layerX == 0) { // Firefox
mx = mousePosition.layerX;
my = mousePosition.layerY;
}
else if (mousePosition.offsetX || mousePosition.offsetX == 0) { // Opera
mx = mousePosition.offsetX;
my = mousePosition.offsetY;
};
//draw if we should draw
if (this.mouse.down) {
this.draw(mx, my);
}
//if not, just update position
else {
this.mouse.x = mx;
this.mouse.y = my;
}
}
//mousedown handler
let mouseDown = (mousePosition) => {
mousePosition.preventDefault();
//update position just in case
mouseMove(mousePosition);
//remember it
this.mouse.px = this.mouse.x;
this.mouse.py = this.mouse.y;
//begin drawing
this.mouse.down = true;
this.context.beginPath();
this.context.moveTo(this.mouse.px, this.mouse.py);
}
let mouseUp = (mousePosition) => {
mousePosition.preventDefault();
this.mouse.down = false;
//stop drawing
this.context.closePath();
}
//attach listeners
this.canvas.addEventListener('mousemove', mouseMove);
this.canvas.addEventListener('mousedown', mouseDown);
this.canvas.addEventListener('mouseup', mouseUp);
this.canvas.addEventListener('touchstart', mouseDown);
this.canvas.addEventListener('touchend', mouseUp);
this.canvas.addEventListener('touchmove', mouseMove);
//set internal canvas params
this.context = this.canvas.getContext('2d');
this.context.globalCompositeOperation = 'source-over';
this.context.globalAlpha = 1;
this.context.strokeStyle = color ? color : 'black';
this.context.lineCap = 'round';
this.context.lineJoin = 'round';
//set drawing params
this.SMOOTHING_INIT = 0.85;
this.WEIGHT_SPREAD = 10;
this._smoothing = this.SMOOTHING_INIT;
this._maxWeight = 12;
this._thickness = 2;
this._targetThickness = 2;
this._weight = 2;
}
static lineDistance(x1, y1, x2, y2) {
//calculate euclidean distance between (x1, y1) and (x2, y2)
var xs = 0;
var ys = 0;
xs = x2 - x1;
xs = xs * xs;
ys = y2 - y1;
ys = ys * ys;
return Math.sqrt( xs + ys );
}
draw(mX, mY) {
let mouse = this.mouse;
let context = this.context;
//calculate distance from previous point
let raw_dist = Atrament.lineDistance(mX, mY, mouse.px, mouse.py)
//now, here we scale the initial smoothing factor by the raw distance
//this means that when the mouse moves fast, there is more smoothing
//and when we're drawing small detailed stuff, we have more control
//also we hard clip at 1
let smoothingFactor = Math.min(0.87, this._smoothing + (raw_dist - 60) / 3000);
//calculate smoothed coordinates
mouse.x = mX - (mX - mouse.px) * smoothingFactor;
mouse.y = mY - (mY - mouse.py) * smoothingFactor;
//recalculate distance from previous point, this time relative to the smoothed coords
let dist = Atrament.lineDistance(mouse.x, mouse.y, mouse.px, mouse.py)
//calculate target thickness based on the new distance
this._targetThickness = (dist - 1) / (50 - 1) * (this._maxWeight - this._weight) + this._weight;
//approach the target gradually
if (this._thickness > this._targetThickness) {
this._thickness -= 0.5;
}
else if (this._thickness < this._targetThickness) {
this._thickness += 0.5;
}
//set line width
context.lineWidth = this._thickness;
//draw using quad interpolation
context.quadraticCurveTo(mouse.px, mouse.py, mouse.x, mouse.y);
context.stroke();
//remember
mouse.px = mouse.x;
mouse.py = mouse.y;
}
get color() {
return this.context.strokeStyle;
}
set color(c) {
if (typeof c !== 'string') throw new Error('wrong argument type');
this.context.strokeStyle = c;
}
get weight() {
return this._weight;
}
set weight(w) {
if (typeof w !== 'number') throw new Error('wrong argument type');
this._weight = w;
this._thickness = w;
this._targetThickness = w;
this._maxWeight = w + this.WEIGHT_SPREAD;
}
get mode() {
return this.context.globalCompositeOperation === 'destination-out' ? 'erase' : 'draw';
}
set mode(m) {
if (typeof m !== 'string') throw new Error('wrong argument type');
this.context.globalCompositeOperation = m === 'erase' ? 'destination-out' : 'source-over';
}
get smoothing() {
return this._smoothing === this.SMOOTHING_INIT;
}
set smoothing(s) {
if (typeof s !== 'boolean') throw new Error('wrong argument type');
this._smoothing = s ? this.SMOOTHING_INIT : 0;
}
set opacity(o) {
if (typeof o !== 'number') throw new Error('wrong argument type');
//now, we need to scale this, because our drawing method means we don't just get uniform transparency all over the drawn line.
//so we scale it down a lot, meaning that it'll look nicely semi-transparent
//unless opacity is 1, then we should go full on to 1
if (o >= 1) this.context.globalAlpha = 1;
else this.context.globalAlpha = o/10;
}
clear() {
//make sure we're in the right compositing mode, and erase everything
if (this.context.globalCompositeOperation === 'destination-out') {
this.mode = 'draw';
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
this.mode = 'erase';
}
else {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
}
toImage() {
return this.canvas.toDataURL();
}
}
//for people who like functional programming
function atrament(selector, width, height, color) {
return new Atrament(selector, width, height, color);
}