HTML5 Use the DrawImage () method to draw an image

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.