Page 22: Try Canvas Programming
Spring 2026 Sample Solution
This page has one simple programming exercise. The next page has a more difficult exercise, and the opportunity for advanced items.
You should do this exercise by editing the file 01-22-01.js .
Box 1: Canvas Event Programming
In the Canvas element box (with id box1canvas), please implement the following behavior:
-
When the user clicks in the element, a circle appears under the mouse. Circles get added, so the user can make lots of circles. When I say circle, I mean a filled in circle (mathematically, this is a disc).
-
If a circle is under the mouse, it changes color. The circle changes color back to its original color when the mouse leaves. When the circle first appears, it might have the “under the mouse color” or the “not under the mouse” color. While a newly created circle is under the mouse, a new circle might not notice that it is under the mouse until after the mouse moves over it. Either way is OK.
-
(advanced) If you click on a circle, it changes its color, but it still must be affected by mouse over. Note: this means that if a click is over a circle, you do not make a new circle (#1).
If you do parts 1 and 2 (which everyone should do), (these colors are examples): when you click, a green circle appears; when the mouse enters a green circle, it changes to yellow, when it leaves the yellow circle it changes back to green. In part 3, if you click on a yellow circle (if you try to click on a green circle, it would change to yellow when you enter), it may change to red. The red circle would change to orange (or yellow) when the mouse enters it, but would change back to red, not green, when the mouse leaves. Of course, you need to let the user make lots of circles - and they should all work correctly.
All of your code for this exercise should go into the file 01-22-01.js . You should not need to change 01-22-01.html We’ve started things for you. You should use the canvas API to draw, not SVG.
view - look at the box and experiment with it
examine - look at the code for the box
edit - change the box's content
rubric - several steps are suggested in the rubric page - form elements on page in rubric
01-22-01.html 01-22-01.js
| Kind
Kind of rubric items: standard - expected from all students advanced - used to get a better grade creative - an opportunity to do something creative levels - rubric item graded by selected achievement level optional - not required, but encouraged |
Description | |
| standard | circles appear at mouse click | |
| standard | circles change change color when mouse is over them | |
| standard | circles change back when mouse leaves | |
| advanced | circle changes color when clicked, hover still works |
That’s all for this page. Next: Page 23 - Fireworks