-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpen.js
More file actions
50 lines (44 loc) · 1.3 KB
/
pen.js
File metadata and controls
50 lines (44 loc) · 1.3 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
const Pen = (ctx) => {
let pen_is_down = false;
let pen_col = '#000f';
let pen_size = 2;
let prev_x, prev_y,
curr_x, curr_y;
const draw = () => {
ctx.beginPath();
ctx.moveTo(prev_x, prev_y);
ctx.lineTo(curr_x, curr_y);
ctx.strokeStyle = pen_col;
ctx.lineWidth = pen_size;
ctx.lineCap = "round";
ctx.stroke();
}
const up = () => { pen_is_down = false; }
const down = () => {
pen_is_down = true;
[prev_x, prev_y] = [curr_x, curr_y];
draw();
}
const dot = () => { down(); up(); }
const isDown = () => pen_is_down;
const goto = (x, y) => {
[curr_x, curr_y] = [x, y];
if (pen_is_down) draw();
[prev_x, prev_y] = [x, y];
}
const move = (dx, dy) => {
curr_x += dx; curr_y += dy;
if (pen_is_down) draw();
[prev_x, prev_y] = [curr_x, curr_y];
}
const pos = () => ({ x: curr_x, y: curr_y });
const color = (c) => {
if (typeof c == 'number')
pen_col = `#${c.toString(16).padStart(6, '0').padEnd(8, 'f')}`;
else pen_col = c;
}
const size = (size) => { pen_size = size; }
return {
up, down, dot, goto, move, pos, color, size, isDown
}
};