is a new HTML element that can be used by the script language (usually JavaScript) to draw graphics. For example, it can be used to paint, synthesize images, or make simple (and not so simple) animations.
The most common way to paint on a canvas is to use an Image object. The supported source picture format relies on browser support, however, some typical picture formats (png,jpg,gif, etc.) are basically no problem.
In all the examples below, the image source will use this 256x256 size image.
To draw a picture: In the most basic drawing operation, all you need is the position where you want the image to appear (x and Y coordinates). The position of the image is judged relative to its upper-left corner. Using this method, the image can be simply drawn on the canvas with its original size.
The code is as follows
var myimage=document.getelementbyidx_x ("MyCanvas");
var Cxt=myimage.getcontext ("2d");
var img=new Image ();
Img.src= "Tu0.jpg";
Cxt.drawimage (img,50,50);
Cxt.drawimage (img,125,125);
Cxt.drawimage (img,210,210);
Generate page effects based on the code you write
Picture morphing: Changing the size of the image, you need to use the overloaded DrawImage function to give it the desired width and height parameters.
The code is as follows
var myimage=document.getelementbyidx_x ("MyCanvas");
var Cxt=myimage.getcontext ("2d");
var img=new Image ();
Img.src= "Tu0.jpg";
Cxt.drawimage (img,50,50,100,100);
Cxt.drawimage (img,125,125,200,50);
Cxt.drawimage (img,210,210,500,500);
This example shows how to draw an image that is smaller than the original, a different aspect ratio image, and a method that is larger than the original image.
Picture clipping: The function of the DrawImage method is to crop the image. DrawImage (Image,sourcex,sourcey,sourcewidth,sourceheight,destx,desty,destwidth, Destheight)
There are many parameters, but basically you can think of it as taking a rectangular area out of the original, and then drawing it to the target area on the canvas.
The code is as follows
var myimage=document.getelementbyidx_x ("MyCanvas");
var Cxt=myimage.getcontext ("2d");
var img=new Image ();
Img.src= "Tu0.jpg";
Cxt.drawimage (img,0,0,50,50,25,25,100,100);
Cxt.drawimage (img,125,125,100,100,125,125,150,150);
Cxt.drawimage (img,80,80,100,100,250,250,220,220);
Generate page effects based on code
These are the basic operations for drawing and processing images in the canvas (canvas) markup in HTML5.
HTML5 Canvas Drawing Detailed drawImage () method There is a picture of the truth!