First, draw the image
Draws an image using the DrawImage () method. The drawing environment provides three different versions of the method.
DrawImage (Image,x,y): Draws a picture in canvas (x,y).
DrawImage (Image,x,y,width,height): Draws a picture in canvas (x,y) and scales it to the specified width and height.
DrawImage (image,sourcex,sourcey,sourcewidth,sourceheight,x,y,width,height): Cut out a rectangular area from the picture (Sourcex,sourcey, Sourcewidth,sourceheight), scaled to the specified width and height, and drawn in canvas (x,y).
Second, label
Before you draw a picture, you also need to load the picture into the browser. Here we just add a tag behind the canvas tag.
Third, draw the picture
Copy Code code as follows:
<! DOCTYPE html>
<html>
<head>
<meta http-equiv= "Content-type" content= "text/html"; CharSet = utf-8 ">
<title>HTML5</title>
<script type= "text/javascript" charset = "Utf-8" >
//This function will be called after the page is fully loaded
function pageLoaded ()
{
Get a reference to the canvas object, note that the Tcanvas name must be the same as the ID in the following body
var canvas = document.getElementById (' Tcanvas ');
//Get the 2D drawing environment of the canvas
var context = Canvas.getcontext (' 2d ');
//Get a reference to a picture object
var image = document.getElementById (' tkjpg ');
//Draw pictures at (0,50)
context.drawimage (image,0,50);
//Shrink the picture to the original half size
context.drawimage (IMAGE,200,50,165/2,86/2);
//Draw part of the picture (cut 0.7 from the top left corner of the picture)
context.drawimage (image,0,0,0.7*165,0.7*86,300,70,0.7*165,0.7*86);
}
</script>
</head>
<body onload= "pageLoaded ();" >
<canvas width = "height" = "id" = "tcanvas" style = "border:black 1px solid;" >
<!--Displays the following font if the browser does not support-->
Tip: Your browser does not support <canvas> tags
</canvas>
<img src = "tk.jpg" id = "Tkjpg" >
</body>
</html>
Four, draw the effect