04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 21: Upcoming API s<br />

(continued)<br />

context.arc(100, 100, 99, 0, 2 * Math.PI, false);<br />

//draw inner circle<br />

context.moveTo(194, 100);<br />

context.arc(100, 100, 94, 0, 2 * Math.PI, false);<br />

//translate to center<br />

context.translate(100, 100);<br />

//rotate the hands<br />

context.rotate(1);<br />

//draw hour hand<br />

context.moveTo(0,0);<br />

context.lineTo(0, -85);<br />

//draw minute hand<br />

context.moveTo(0, 0);<br />

context.lineTo(-65, 0);<br />

}<br />

//stroke the path<br />

context.stroke();<br />

Since the origin has already been translated to the center of clock, the rotation is applied from that point.<br />

This means that the hands are both anchored at the center and then rotated around to the right. The<br />

result is displayed in Figure 21 - 6 .<br />

Figure 21-6<br />

All of these transformations, as well as properties like fillStyle and strokeStyle , remain set on the<br />

context until explicitly changed. Although there ’ s no way to explicitly reset everything to their default<br />

values, there are two methods that can help keep track of changes. Whenever you want to be able to<br />

return to a specific set of properties and transformations, call the save() method. Once called, this<br />

method pushes all of the settings at the moment onto a stack for safekeeping. You can then go on to<br />

make other changes to the context. When you want to go back to the previous settings, call the<br />

690

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!