Advertisement
AtEchoOff

HTML Canvas Activity Starter Code

Apr 15th, 2025
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. ----------------------------index.html------------------------------
  2.  
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <title>Click to Color!</title>
  7. </head>
  8. <body>
  9. <h2>Click anywhere on the canvas to draw colorful dots!</h2>
  10.  
  11. <canvas id="myCanvas" width="500" height="400" style="border:1px solid #000;"></canvas>
  12.  
  13. <script>
  14. // 1. Get the canvas and its 2D drawing context
  15. const canvas = document.getElementById("myCanvas");
  16. const ctx = canvas.getContext("2d");
  17.  
  18. // 2. Fill the canvas with a white background
  19. // (use fillStyle and fillRect)
  20.  
  21. // 3. Add an event listener for when the canvas is clicked
  22. canvas.addEventListener("click", function(event) {
  23. // 4. Get the x and y position of the mouse click
  24. // (hint: use getBoundingClientRect and clientX/clientY)
  25. let x = 0; // Replace this with the correct x position
  26. let y = 0; // Replace this with the correct y position
  27.  
  28. // 5. Generate a random color
  29. let color = ""; // Replace this with a string like "rgb(r, g, b)"
  30.  
  31. // 6. Draw a circle at the click position
  32. // (use arc and fill)
  33. });
  34. </script>
  35. </body>
  36. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement