canvas-random-dots.html 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Canvas Random Dots</title>
  8. </head>
  9. <body>
  10. </body>
  11. <script>
  12. 'use strict';
  13. function main() {
  14. const ctx = document.createElement('canvas').getContext('2d');
  15. document.body.appendChild(ctx.canvas);
  16. ctx.canvas.width = 256;
  17. ctx.canvas.height = 256;
  18. ctx.fillStyle = '#FFF';
  19. ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
  20. function randInt(min, max) {
  21. if (max === undefined) {
  22. max = min;
  23. min = 0;
  24. }
  25. return Math.random() * (max - min) + min | 0;
  26. }
  27. function drawRandomDot() {
  28. ctx.fillStyle = `#${randInt(0x1000000).toString(16).padStart(6, '0')}`;
  29. ctx.beginPath();
  30. const x = randInt(256);
  31. const y = randInt(256);
  32. const radius = randInt(10, 64);
  33. ctx.arc(x, y, radius, 0, Math.PI * 2);
  34. ctx.fill();
  35. }
  36. function render() {
  37. drawRandomDot();
  38. requestAnimationFrame(render);
  39. }
  40. requestAnimationFrame(render);
  41. }
  42. main();
  43. </script>
  44. </html>