Page 7: Try Canvas Programming
CS559 Spring 2023 Sample Solution
This page has one simple programming exercise. The next page has a more difficult exercise, and the opportunity for advanced points.
You should do this exercise by editing the file 02-07-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 - more work for less points) 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 02-07-01.js. You should not need to change 02-07-01.html We’ve started things for you. You should use the canvas API to draw, not SVG.
Box 02-07-01 Rubric (11 points total)
That’s all for this page. Next: Fireworks