-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfence.js
37 lines (31 loc) · 811 Bytes
/
fence.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
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
let isDrawing = false;
let lastX = 0;
let lastY = 0;
const circleRadius = 2;
canvas.addEventListener('mousedown', function(event) {
isDrawing = true;
lastX = event.offsetX;
lastY = event.offsetY;
});
canvas.addEventListener('mousemove', function(event) {
if (isDrawing) {
const x = event.offsetX;
const y = event.offsetY;
// Draw a line from the last position to the current position
ctx.beginPath();
ctx.moveTo(lastX, lastY);
ctx.lineTo(x, y);
ctx.stroke();
// Draw a circle at the current position
ctx.beginPath();
ctx.arc(x, y, circleRadius, 0, 2 * Math.PI);
ctx.fill();
lastX = x;
lastY = y;
}
});
canvas.addEventListener('mouseup', function(event) {
isDrawing = false;
});