Example of using the drawImage () method in the HTML5 Canvas API, html5drawimage
DrawImage () is a key method. It can introduce images, canvases, and videos and scale or crop them.
There are three forms:
Syntax 1
Copy the content to the clipboard using JavaScript Code
- Context. drawImage (img, dx, dy );
Syntax 2
Copy the content to the clipboard using JavaScript Code
- Context. drawImage (img, dx, dy, dw, dw );
Syntax 3
Copy the content to the clipboard using JavaScript Code
- Context. drawImage (img, sx, sy, sw, sh, dx, dy, dw, dh );
Let's take a look at the coordinate sketch:
PS: do not define the width and height of <canvas> in the style. Otherwise, the image is automatically enlarged or reduced.
The three parameters are in the standard format and can be used to load images, canvases, or videos. In addition to the ability to load images, the five parameters can also be used to scale the images with a specified width or height. In addition to scaling, you can also Crop Data. The meanings of parameters are shown in the following table.
Parameters |
Description |
Img |
Sx |
Optional. The x coordinate position of the Start cut. |
Sy |
Optional. Y coordinate position of the Start cut. |
Swidth |
Optional. Width of the cut image. |
Sheight |
Optional. The height of the cut image. |
X |
Place the x coordinate position of the image on the canvas. |
Y |
Place the y coordinate position of the image on the canvas. |
Width |
Optional. The width of the image to be used. (Stretch or zoom out an image) |
Height |
The height of the image to be used. (Stretch or zoom out an image) |
Next, we will try to load an image.
Copy the content to the clipboard using JavaScript Code
- <! DOCTYPE html>
- <Html lang = "zh">
- <Head>
- <Meta charset = "UTF-8">
- <Title> drawImage () </title>
- <Style>
- Body {background: url ("./images/bg3.jpg") repeat ;}
- # Canvas {border: 1px solid # aaaaaa; display: block; margin: 50px auto ;}
- </Style>
- </Head>
- <Body>
- <Div id = "canvas-warp">
- <Canvas id = "canvas">
- Does your browser support Canvas ?! Just change one !!
- </Canvas>
- </Div>
- <Script>
- Window. onload = function (){
- Var canvas = document. getElementById ("canvas ");
- Canvas. width = 800;
- Canvas. height = 600;
- Var context = canvas. getContext ("2d ");
- Context. fillStyle = "# FFF ";
- Context. fillRect (0, 0, 800,600 );
- Var img = new Image ();
- Img. src = "./images/20-1.jpg ";
- Img. onload = function (){
- Context. drawImage (img, 200,50 );
- }
- };
- </Script>
- </Body>
- </Html>
Running result:
Create a photo frame:
Here, we use clip (), drawImage (), and the three-step bezierCurveTo curve () to add a heart-shaped photo frame to the above case ~
Copy the content to the clipboard using JavaScript Code
- <! DOCTYPE html>
- <Html lang = "zh">
- <Head>
- <Meta charset = "UTF-8">
- <Title> draw a heart-shaped Photo Frame </title>
- <Style>
- Body {background: url ("./images/bg3.jpg") repeat ;}
- # Canvas {border: 1px solid # aaaaaa; display: block; margin: 50px auto ;}
- </Style>
- </Head>
- <Body>
- <Div id = "canvas-warp">
- <Canvas id = "canvas">
- Does your browser support Canvas ?! Just change one !!
- </Canvas>
- </Div>
- <Script>
- Window. onload = function (){
- Var canvas = document. getElementById ("canvas ");
- Canvas. width = 800;
- Canvas. height = 600;
- Var context = canvas. getContext ("2d ");
- Context. fillStyle = "# FFF ";
- Context. fillRect (0, 0, 800,600 );
- Context. beginPath ();
- Context. moveTo (400,260 );
- Context. bezierCurveTo (450,220,450,300,400,315 );
- Context. bezierCurveTo (350,300,350,220,400,260 );
- Context. clip ();
- Context. closePath ();
- Var img = new Image ();
- Img. src = "./images/20-1.jpg ";
- Img. onload = function (){
- Context. drawImage (img, 348,240,100,100 );
- }
- };
- </Script>
- </Body>
- </Html>
Running result:
Is it beautiful? Well, now the most important thing is mask and image cropping. in java. awt, drawImage () is also a crucial method. Some people say that to create a Java game interface, you only need to use drawImage () to try it all over the world ~ The same is true in Canvas. The materials provided by the artist are basically images. In this case, drawImage () is very important for image processing. Even basic skills are the most important way to process images.