Use clip () in the HTML5 Canvas API to crop a region image.

Source: Internet
Author: User

Use clip () in the HTML5 Canvas API to crop a region image.

When using Canvas to draw images, we often want to retain only some of the images. This is the idea that we can use the canvas API to reproduce the image cropping function.
The image cropping function of the Canvas API refers to using a path in the Canvas to draw images of only the regions included in the path, rather than images outside the path. This is a bit like a layer mask in Flash.

Use the clip () method without parameters in the graphic context to crop the Canvas image. This method uses the path to set no cropping area for the Canvas. Therefore, you must first create a path. Call the clip () method to set the cropping area.
It should be noted that the cropping is performed on the canvas, and the cropped canvas cannot be restored to the original size, that is, the canvas is smaller and smaller, pay attention to save () and restore () for drawing with the size originally defined by the canvas (). The canvas is cropped before drawing. It is not necessarily an image, but the path can be included ~

Let's take a look at a simple Demo.

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> crop a region </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. // Draw a large square on the screen
  26. Context. fillStyle = "black ";
  27. Context. fillRect (10, 10, 200,200 );
  28. Context. save ();
  29. Context. beginPath ();
  30. // Crop the square of the canvas from (0, 0) to (50, 50)
  31. Context. rect (0, 0, 50, 50 );
  32. Context. clip ();
  33. // Red circle
  34. Context. beginPath ();
  35. Context. strokeStyle = "red ";
  36. Context. lineWidth = 5;
  37. Context. arc (100,100,100, 0, Math. PI * 2, false );
  38. // Round
  39. Context. stroke ();
  40. Context. closePath ();
  41. Context. restore ();
  42. // Crop the entire canvas again
  43. Context. beginPath ();
  44. Context. rect (0, 0, 500,500 );
  45. Context. clip ();
  46. // Draw a blue line without cropping
  47. Context. beginPath ();
  48. Context. strokeStyle = "blue ";
  49. Context. lineWidth = 5;
  50. Context. arc (100,100, 50, 0, Math. PI * 2, false );
  51. // Round
  52. Context. stroke ();
  53. Context. closePath ();
  54. };
  55. </Script>
  56. </Body>
  57. </Html>

Running result:

The save () and restore () methods are used in combination to limit the painting area. First, we can use the rect () method to enclose an area we want to draw, and then use the clip () method to crop the area.

In this way, no matter what operations are performed in the context, only the limited part is displayed. That is to say, clip () is used to limit the area to be displayed. When we do not want to continue to limit the region, we can use the restore () method to jump out and continue to operate the original context.
Let's take a look at this cropping:

Copy the content to the clipboard using JavaScript Code
  1. Function drawScreen (){
  2. Var x = canvas. width/2;
  3. Var y = canvas. height/2;
  4. Var radius = 75;
  5. Var offset = 50;
  6. // The cropped area is (x, y) a circle with a center radius of 75.
  7. Context. save ();
  8. Context. beginPath ();
  9. Context. arc (x, y, radius, 0, 2 * Math. PI, false );
  10. Context. clip ();
  11. // Draw a blue arc first. The cropped part is not displayed.
  12. Context. beginPath ();
  13. Context. arc (x-offset, y-offset, radius, 0, 2 * Math. PI, false );
  14. Context. fillStyle = 'blue ';
  15. Context. fill ();
  16. // Draw a yellow arc. The cropped part is not displayed.
  17. Context. beginPath ();
  18. Context. arc (x + offset, y, radius, 0, 2 * Math. PI, false );
  19. Context. fillStyle = 'yellow ';
  20. Context. fill ();
  21. // Draw a red arc. The cropped part is not displayed.
  22. Context. beginPath ();
  23. Context. arc (x, y + offset, radius, 0, 2 * Math. PI, false );
  24. Context. fillStyle = 'red ';
  25. Context. fill ();
  26. /*
  27. * The restore () method returns the original state of context, which is prior to clip.
  28. * You can remove the context. beginPath () method and try to see what will happen.
  29. */
  30. Context. restore ();
  31. Context. beginPath ();
  32. Context. arc (x, y, radius, 0, 2 * Math. PI, false );
  33. Context. lineWidth = 10;
  34. Context. strokeStyle = 'blue ';
  35. Context. stroke ();
  36. }


Again, the call form of the cropping function is generally

Save ();
Clip ();
Restore ();
In this order.

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.