-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode7_test_draw.html
72 lines (67 loc) · 2.5 KB
/
Code7_test_draw.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Canvas Drawing</title>
</head>
<body>
<canvas id="myCanvas" width="1500" height="1500"></canvas>
<script>
const canvas = document.getElementById('myCanvas');
const context = canvas.getContext('2d');
const record = [];
let isDrawing = false;
let lastX = 0;
let lastY = 0;
canvas.addEventListener('mousedown', (event) => {
isDrawing = true;
[lastX, lastY] = [event.offsetX, event.offsetY];
record.push({ type: 'mousedown', x: lastX, y: lastY });
});
canvas.addEventListener('mousemove', (event) => {
if (!isDrawing) return;
const currentX = event.offsetX;
const currentY = event.offsetY;
context.beginPath();
context.moveTo(lastX, lastY);
context.lineTo(currentX, currentY);
context.lineWidth = 1;
context.stroke();
[lastX, lastY] = [currentX, currentY];
record.push({ type: 'mousemove', x: currentX, y: currentY });
drawRandomLines(currentX, currentY);
});
canvas.addEventListener('mouseup', () => {
isDrawing = false;
record.push({ type: 'mouseup' });
});
function drawRandomLines(x, y) {
const radius = 30;
const validPoints = [];
for (let i = 0; i < record.length; i++) {
const point = record[i];
if (point.type === 'mousemove' && distance(point.x, point.y, x, y) < radius) {
validPoints.push(point);
}
}
if (validPoints.length > 0) {
for (let i = 0; i < 20; i++) {
const randomPoint = validPoints[Math.floor(Math.random() * validPoints.length)];
const randomX = randomPoint.x;
const randomY = randomPoint.y;
context.beginPath();
context.moveTo(x, y);
context.lineTo(randomX, randomY);
context.lineWidth = 1;
context.stroke();
}
}
}
function distance(x1, y1, x2, y2) {
const xDistance = x2 - x1;
const yDistance = y2 - y1;
return Math.sqrt(Math.pow(xDistance, 2) + Math.pow(yDistance, 2));
}
</script>
</body>
</html>