-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsketch.js
executable file
·57 lines (45 loc) · 1.59 KB
/
sketch.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
const canvas = document.createElement('canvas');
const body = document.body;
const html = document.documentElement;
const scale = window.devicePixelRatio;
let windowWidth = window.innerWidth;
let windowHeight = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight);
console.log(windowWidth)
canvas.width = windowWidth * scale;
canvas.height = windowHeight * scale;
Object.assign(canvas.style, {
position:"absolute",
zIndex: 1000,
// pointerEvents: 'none',
// height: windowHeight + '!important',
width: windowWidth + 'px',
top: '0',
left: '0'
});
body.appendChild(canvas);
const ctx = canvas.getContext('2d');
ctx.lineJoin = 'round';
ctx.lineCap = 'round';
ctx.scale(scale, scale)
ctx.translate(0.5, 0.5);
let isDrawing = false;
let lastX = 0;
let lastY = 0;
let direction = true;
function draw(e) {
if (!isDrawing) return;
ctx.strokeStyle = drawConfig.color;
ctx.lineWidth = drawConfig.weight;
ctx.beginPath();
ctx.moveTo(lastX, lastY);
ctx.lineTo(e.offsetX, e.offsetY);
ctx.stroke();
[lastX, lastY] = [e.offsetX, e.offsetY];
}
canvas.addEventListener('mousedown', (e) => {
isDrawing = true;
[lastX, lastY] = [e.offsetX, e.offsetY];
});
canvas.addEventListener('mousemove', draw);
canvas.addEventListener('mouseup', () => isDrawing = false);
canvas.addEventListener('mouseout', () => isDrawing = false);