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
|
<!DOCTYPE html>
<html>
<head>
<title>Canvas, timer, random and event demo</title>
</head>
<body>
<canvas id=c width=400 height=300></canvas>
<script>
c = document.getElementById("c");
ctx = c.getContext("2d");
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, c.width, c.height);
pressed = false;
mouseX = 0;
mouseY = 0;
c.addEventListener("mousedown", function(e) {
// mousedown
pressed = true;
mouseX = e.offsetX;
mouseY = e.offsetY;
});
c.addEventListener("mouseup", function() {
// mouseup
pressed = false;
});
c.addEventListener("mousemove", function(e) {
// mousemove
mouseX = e.offsetX;
mouseY = e.offsetY;
});
function update() {
if (pressed) {
var r = Math.random() * 255;
var g = Math.random() * 255;
var b = Math.random() * 255;
var color = "rgb(" + ~~r + "," + ~~g + "," + ~~b + ")";
ctx.fillStyle = color;
const spread = 35;
var x = mouseX + (Math.random() * spread) - (spread / 2);
var y = mouseY + (Math.random() * spread) - (spread / 2);
var size = Math.random() * 8;
ctx.fillRect(x, y, size, size);
}
}
setInterval(update, 20);
</script>
</body>
</html>
|