-
-
Notifications
You must be signed in to change notification settings - Fork 35.7k
/
Copy pathcanvas-random-dots.html
62 lines (43 loc) · 1.13 KB
/
canvas-random-dots.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
<!-- Licensed under a BSD license. See license.html for license -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>Canvas Random Dots</title>
</head>
<body>
</body>
<script type="module">
function main() {
const ctx = document.createElement( 'canvas' ).getContext( '2d' );
document.body.appendChild( ctx.canvas );
ctx.canvas.width = 256;
ctx.canvas.height = 256;
ctx.fillStyle = '#FFF';
ctx.fillRect( 0, 0, ctx.canvas.width, ctx.canvas.height );
function randInt( min, max ) {
if ( max === undefined ) {
max = min;
min = 0;
}
return Math.random() * ( max - min ) + min | 0;
}
function drawRandomDot() {
ctx.fillStyle = `#${randInt( 0x1000000 ).toString( 16 ).padStart( 6, '0' )}`;
ctx.beginPath();
const x = randInt( 256 );
const y = randInt( 256 );
const radius = randInt( 10, 64 );
ctx.arc( x, y, radius, 0, Math.PI * 2 );
ctx.fill();
}
function render() {
drawRandomDot();
requestAnimationFrame( render );
}
requestAnimationFrame( render );
}
main();
</script>
</html>