Example of using the drawImage () method in the HTML5 Canvas API, html5drawimage

Source: Internet
Author: User

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
  1. Context. drawImage (img, dx, dy );

Syntax 2

Copy the content to the clipboard using JavaScript Code
  1. Context. drawImage (img, dx, dy, dw, dw );

Syntax 3

Copy the content to the clipboard using JavaScript Code
  1. 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
  1. <! DOCTYPE html>
  2. <Html lang = "zh">
  3. <Head>
  4. <Meta charset = "UTF-8">
  5. <Title> drawImage () </title>
  6. <Style>
  7. Body {background: url ("./images/bg3.jpg") repeat ;}
  8. # Canvas {border: 1px solid # aaaaaa; display: block; margin: 50px auto ;}
  9. </Style>
  10. </Head>
  11. <Body>
  12. <Div id = "canvas-warp">
  13. <Canvas id = "canvas">
  14. Does your browser support Canvas ?! Just change one !!
  15. </Canvas>
  16. </Div>
  17. <Script>
  18. Window. onload = function (){
  19. Var canvas = document. getElementById ("canvas ");
  20. Canvas. width = 800;
  21. Canvas. height = 600;
  22. Var context = canvas. getContext ("2d ");
  23. Context. fillStyle = "# FFF ";
  24. Context. fillRect (0, 0, 800,600 );
  25. Var img = new Image ();
  26. Img. src = "./images/20-1.jpg ";
  27. Img. onload = function (){
  28. Context. drawImage (img, 200,50 );
  29. }
  30. };
  31. </Script>
  32. </Body>
  33. </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
  1. <! DOCTYPE html>
  2. <Html lang = "zh">
  3. <Head>
  4. <Meta charset = "UTF-8">
  5. <Title> draw a heart-shaped Photo Frame </title>
  6. <Style>
  7. Body {background: url ("./images/bg3.jpg") repeat ;}
  8. # Canvas {border: 1px solid # aaaaaa; display: block; margin: 50px auto ;}
  9. </Style>
  10. </Head>
  11. <Body>
  12. <Div id = "canvas-warp">
  13. <Canvas id = "canvas">
  14. Does your browser support Canvas ?! Just change one !!
  15. </Canvas>
  16. </Div>
  17. <Script>
  18. Window. onload = function (){
  19. Var canvas = document. getElementById ("canvas ");
  20. Canvas. width = 800;
  21. Canvas. height = 600;
  22. Var context = canvas. getContext ("2d ");
  23. Context. fillStyle = "# FFF ";
  24. Context. fillRect (0, 0, 800,600 );
  25. Context. beginPath ();
  26. Context. moveTo (400,260 );
  27. Context. bezierCurveTo (450,220,450,300,400,315 );
  28. Context. bezierCurveTo (350,300,350,220,400,260 );
  29. Context. clip ();
  30. Context. closePath ();
  31. Var img = new Image ();
  32. Img. src = "./images/20-1.jpg ";
  33. Img. onload = function (){
  34. Context. drawImage (img, 348,240,100,100 );
  35. }
  36. };
  37. </Script>
  38. </Body>
  39. </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.

Related Article

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.