|
| 1 | +// get canvas and get a context for said canvas |
| 2 | +var canvas = document.getElementById("canvas"); |
| 3 | +var ctx = canvas.getContext("2d"); |
| 4 | + |
| 5 | +// frames per second to render |
| 6 | +const FPS = 200; |
| 7 | + |
| 8 | +// constant size of dots that does not change |
| 9 | +const DOTSIZE = 3; |
| 10 | +const DOTCOUNT = 500; |
| 11 | +const DOTDISTANCE = 100; |
| 12 | + |
| 13 | +// { x: number, y: number, vx: number, vy: number } |
| 14 | +var dots = []; |
| 15 | + |
| 16 | +/** |
| 17 | + * return a random number between 0 and max |
| 18 | + * |
| 19 | + * @param {number} max |
| 20 | + */ |
| 21 | +function randomPosNumber(max) { |
| 22 | + return Math.floor(Math.random() * max); |
| 23 | +} |
| 24 | + |
| 25 | +function randomVelNumber() { |
| 26 | + const random = Math.random(); |
| 27 | + if (random > 0.5) { |
| 28 | + return Math.random() * -1; |
| 29 | + } else { |
| 30 | + return Math.random() * 0.5; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * resize the canvas |
| 36 | + */ |
| 37 | +function setCanvasSize() { |
| 38 | + canvas.width = window.innerWidth; |
| 39 | + ctx.canvas.width = window.innerWidth; |
| 40 | + canvas.height = window.innerHeight; |
| 41 | + ctx.canvas.height = window.innerHeight; |
| 42 | +} |
| 43 | + |
| 44 | +function animate() { |
| 45 | + setTimeout(function () { |
| 46 | + requestAnimationFrame(animate); |
| 47 | + ctx.fillStyle = "white"; |
| 48 | + ctx.strokeStyle = "white"; |
| 49 | + ctx.lineWidth = 0.5; |
| 50 | + |
| 51 | + // if dots havent been initialized yet, initialize |
| 52 | + if (dots.length == 0) { |
| 53 | + for (let i = 0; i < DOTCOUNT; i++) { |
| 54 | + dots.push({ |
| 55 | + x: randomPosNumber(ctx.canvas.width), |
| 56 | + y: randomPosNumber(ctx.canvas.width), |
| 57 | + vx: randomVelNumber(), |
| 58 | + vy: randomVelNumber(), |
| 59 | + }); |
| 60 | + } |
| 61 | + } else { |
| 62 | + // animate |
| 63 | + ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); |
| 64 | + console.log("animate"); |
| 65 | + dots.forEach((dot) => { |
| 66 | + dot.x += dot.vx; |
| 67 | + dot.y += dot.vy; |
| 68 | + |
| 69 | + if (dot.x > ctx.canvas.width - DOTSIZE || dot.x < 0) { |
| 70 | + dot.vx *= -1; |
| 71 | + dot.x += dot.vx; |
| 72 | + } |
| 73 | + |
| 74 | + if (dot.y > ctx.canvas.height - DOTSIZE || dot.y < 0) { |
| 75 | + dot.vy *= -1; |
| 76 | + dot.y += dot.vy; |
| 77 | + } |
| 78 | + ctx.fillRect(dot.x, dot.y, DOTSIZE, DOTSIZE); |
| 79 | + }); |
| 80 | + |
| 81 | + for (let i = 0; i < DOTCOUNT; i++) { |
| 82 | + for (let j = 0; j < DOTCOUNT; j++) { |
| 83 | + let dot1 = dots[i]; |
| 84 | + let dot2 = dots[j]; |
| 85 | + if ( |
| 86 | + Math.sqrt( |
| 87 | + Math.pow(dot2.x - dot1.x, 2) + Math.pow(dot2.y - dot1.y, 2) * 1.0 |
| 88 | + ) < DOTDISTANCE |
| 89 | + ) { |
| 90 | + ctx.beginPath(); // start a path |
| 91 | + ctx.moveTo(dot1.x, dot1.y); |
| 92 | + ctx.lineTo(dot2.x, dot2.y); |
| 93 | + ctx.stroke(); // fill line |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + }, 1000 / FPS); |
| 99 | +} |
| 100 | + |
| 101 | +// whenever window is resized, change the canvas width and height to be full screen |
| 102 | +window.addEventListener("resize", (_e) => setCanvasSize()); |
| 103 | +setCanvasSize(); |
| 104 | + |
| 105 | +requestAnimationFrame(animate); |
0 commit comments