04.11.2015 Views

javascript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 21: Upcoming API s<br />

restore() method, which pops the settings stack and restores all of the settings. You can keep calling<br />

save() to store more settings on the stack and then systematically go back through them using<br />

restore() . Here is an example:<br />

context.fillStyle = “#ff0000”;<br />

context.save();<br />

context.fillStyle = “#00ff00”;<br />

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

context.save();<br />

context.fillStyle = “#0000ff”;<br />

context.fillRect(0, 0, 100, 200); //draws blue rectangle at (100, 100)<br />

context.restore();<br />

context.fillRect(10, 10, 100, 200); //draws green rectangle at (110, 110)<br />

context.restore();<br />

context.fillRect(0, 0, 100, 200); //draws red rectangle at (0,0)<br />

In this code, the fillStyle is set to red and then save() is called. Next, the fillStyle is changed to<br />

green, and the coordinates are translated to (100,100). Once again, save() is called to save these settings.<br />

The fillStyle property is then set to blue and a rectangle is drawn. Because the coordinates are<br />

translated, the rectangle actually ends up being drawn at (100,100). When restore() is called,<br />

fillStyle is set back to green, so the next rectangle that’s drawn is green. This rectangle is drawn at<br />

(110,110) because the translation is still in effect. When restore() is called one more time, the<br />

translation is removed and fillStyle is set back to red. The last rectangle is drawn at (0,0).<br />

Note that save() saves only the settings and transformations applied to the drawing context, but not<br />

the contents of the drawing context.<br />

Working with Images<br />

The 2D drawing context has built - in support for working with images. If you have an existing image that<br />

should be drawn on the canvas, you can do so using the drawImage() method. This method can be<br />

called with three different sets of arguments based on the desired result. The simplest call is to pass in an<br />

HTML < img > element, as well as the destination x and y coordinates, which simply draws the image at<br />

the specified location. Here is an example:<br />

var image = document.images[0];<br />

context.drawImage(image, 10, 10);<br />

This code gets the first image in the document and draws it on the context at position (10,10). The image<br />

is drawn in the same scale as the original. You can change how the image is drawn by adding two more<br />

arguments: the destination width and destination height. This scales the drawing without affecting the<br />

transformation matrix of the context. Here’s an example:<br />

context.drawImage(image, 50, 10, 20, 30);<br />

When this code is executed, the image is scaled to be 20 pixels wide by 30 pixels high.<br />

691

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

Saved successfully!

Ooh no, something went wrong!