Unit Circle
Canvas coordinates start at the upper-left corner, and extend down
and to the right, with pixels as the units. Sometimes this bothers
me.
I like thinking of positive Y as up. I don't always want to work
with pixels as my units of measurement. Also, I like having the
origin at the center. Fortunately the canvas provides a way for me
to set all of this up the way I like it in one easy step: The
setTransform
method.
Drawing this unit circle is a good example of when it's handy to
have logical dimensions decoupled from the actual pixel dimensions
of the canvas. Once the transformation matrix is set up I can
really just draw my circle at the origin with a radius of one. My
line segment goes from (0,0) to (cos(theta), sin(theta)).
This is my current canvas initialization method. For the unit
circle, I just call
this.init_canvas(5.0)
.