-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdraw.html
100 lines (85 loc) · 2.38 KB
/
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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pretty pretty starfield</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
#target {
background: black;
height: 100vh;
width: 100vw;
z-index: -1;
position: fixed;
}
button {
background: transparent;
border: 1px solid transparent;
border-radius: 5px;
box-shadow: 0 0 5px 2px #0f0;
color: #0f0;
margin: 1em;
padding: 0.3em;
cursor: pointer;
}
#counter {
cursor: all-scroll;
position: absolute;
top: 0;
right: 0;
}
</style>
</head>
<body>
<canvas id="target"></canvas>
<div id="wrapper">
<button id="counter"></button>
</div>
<script type="text/javascript">
// Are you KIDDING me with this type casting?
/** @type {HTMLCanvasElement} */
const canvas = document.querySelector('#target');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
/** @type {CanvasRenderingContext2d} */
const ctx = canvas.getContext('2d');
const centerX = Math.ceil(canvas.width / 2);
const centerY = Math.ceil(canvas.height / 2);
let counter = 0;
let oldx = centerX;
let oldy = centerY;
const degrees = Math.PI * 2;
ctx.moveTo(oldx, oldy);
ctx.lineWidth = 6;
const draw = (x,y) => {
// ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = 'hsl('+counter+', 100%, 50%)';
ctx.beginPath();
ctx.moveTo(oldx,oldy);
ctx.lineTo(x,y);
ctx.stroke();
oldx = x;
oldy = y;
counter++;
document.querySelector('#counter').innerHTML = counter;
};
const b = document.querySelector('body');
b.addEventListener('touchmove', (e) => {
counter += 10; // touchmoves are slooooow
const touches = e.changedTouches;
for (let i = 0; i < touches.length; i++) {
draw(touches[i].pageX, touches[i].pageY);
}
});
b.addEventListener('mousemove', (e) => {
draw(e.offsetX, e.offsetY);
});
</script>
</body>
</html>