Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 4. Images and Video > 4.3. Drawing a Canvas into a Canvas

4.3. Drawing a Canvas into a Canvas

The application shown in Figure 4.7 draws an image into a canvas, and then draws some text, known as a watermark, on top of the image.

image

Figure 4.7. A watermark

When the user adjusts the scale with the slider in the upper-left corner, the application scales both the image and the text. You could scale the image and text together by drawing them into an offscreen canvas at the specified scale and subsequently copying the offscreen canvas back into the onscreen canvas; in this case, however, an offscreen canvas is not strictly necessary because drawImage() can draw a canvas back into itself, like this:

var canvas = document.getElementById('canvas'),
    context = canvas.getContext('2d'),
    scaleWidth = ..., // Calculate scales for width and height
    scaleHeight = ...;

...
context.drawImage(canvas, 0, 0, scaleWidth, scaleHeight);
...


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial