Home  >  Article  >  Web Front-end  >  Cropping area images using clip() method in HTML5 Canvas API_html5 tutorial tips

Cropping area images using clip() method in HTML5 Canvas API_html5 tutorial tips

WBOY
WBOYOriginal
2016-05-16 15:52:011337browse

When using Canvas to draw an image, we often want to keep only a part of the image. We can use the image cropping function of the canvas API to achieve this idea.
The image cropping function of Canvas API means that using a path within the canvas, only the image of the area contained in the path will be drawn, and the image outside the path will not be drawn. This is a bit like layer masks in Flash.

Use the clip() method without parameters of the graphics context to implement the image cropping function of Canvas. This method uses a path to set a clipping area for the Canvas. Therefore, the path must be created first. After creation is complete, call the clip() method to set the cropping area.
It should be noted that cropping is performed on the canvas. The cropped canvas cannot be restored to its original size, which means that the canvas becomes smaller as it is cut. To ensure that it can still be at the size originally defined by the canvas in the end Drawing needs to pay attention to save() and restore(). The canvas is cut first before drawing. It doesn’t have to be a picture, the path can also be put in~

Let’s take a look at a simple Demo first.

JavaScript CodeCopy content to clipboard
  1.   
  2. "zh">   
  3.   
  4.     "UTF-8">   
  5.     裁剪区域   
  6.        
  7.   
  8.   
  9. "canvas-warp">   
  10.     "canvas">   
  11.         你的浏览器居然不支持Canvas?!赶快换一个吧!!   
  12.        
  • <script> </span> </li> <li class="alt"> <span> window.onload = </span><span class="keyword">function</span><span>(){ </span> </li> <li> <span> </span><span class="keyword">var</span><span> canvas = document.getElementById(</span><span class="string">"canvas"</span><span>); </span> </li> <li class="alt"> <span> canvas.width = 800; </span> </li> <li> <span> canvas.height = 600; </span> </li> <li class="alt"> <span> </span><span class="keyword">var</span><span> context = canvas.getContext(</span><span class="string">"2d"</span><span>); </span> </li> <li> <span> context.fillStyle = </span><span class="string">"#FFF"</span><span>; </span> </li> <li class="alt"> <span> context.fillRect(0,0,800,600); </span> </li> <li> <span> </span> </li> <li class="alt"> <span> </span><span class="comment">//Draw a large square on the screen </span><span> </span> </li> <li> <span> context.fillStyle = </span><span class="string">"black"</span><span>; </span> </li> <li class="alt"> <span> context.fillRect(10,10,200,200); </span> </li> <li> <span> context.save(); </span> </li> <li class="alt"> <span> context.beginPath(); </span> </li> <li> <span> </span> </li> <li class="alt"> <span> </span><span class="comment">//Cut the canvas from the (0, 0) point to the square of (50, 50) </span><span> </span> </li> <li> <span> context.rect(0,0,50,50); </span> </li> <li class="alt"> <span> context.clip(); </span> </li> <li> <span> </span> </li> <li class="alt"> <span> </span><span class="comment">//Red circle </span><span> </span> </li> <li> <span> context.beginPath(); </span> </li> <li class="alt"> <span> context.strokeStyle = </span><span class="string">"red"</span><span>; </span> </li> <li> <span> context.lineWidth = 5; </span> </li> <li class="alt"> <span> context.arc(100,100,100,0,Math.PI * 2,</span><span class="keyword">false</span><span>); </span> </li> <li> <span> </span><span class="comment">//Full circle </span><span> </span> </li> <li class="alt"> <span> context.stroke(); </span> </li> <li> <span> context.closePath(); </span> </li> <li class="alt"> <span> </span> </li> <li> <span> context.restore(); </span> </li> <li class="alt"> <span> </span> </li> <li> <span> </span><span class="comment">//Cut the entire canvas again </span><span> </span> </li> <li class="alt"> <span> context.beginPath(); </span> </li> <li> <span> context.rect(0,0,500,500); </span> </li> <li class="alt"> <span> context.clip(); </span> </li> <li> <span> </span> </li> <li class="alt"> <span> </span><span class="comment">//Draw an uncut blue line </span><span> </span> </li> <li> <span> context.beginPath(); </span> </li> <li class="alt"> <span> context.strokeStyle = </span><span class="string">"blue"</span><span>; </span> </li> <li> <span> context.lineWidth = 5; </span> </li> <li class="alt"> <span> context.arc(100,100,50,0,Math.PI * 2,</span><span class="keyword">false</span><span>); </span> </li> <li> <span> </span><span class="comment">//Full circle </span><span> </span> </li> <li class="alt"> <span> context.stroke(); </span> </li> <li> <span> context.closePath(); </span> </li> <li class="alt"> <span> }; </span> </li> <li> <span></script>
  • Run result:
    2016325102516578.jpg (850×500)

    Using a mixture of save() and restore() methods, we can limit the drawing area. First, we can use the rect() method to surround an area we want to draw, and then use the clip() method to crop the area.

    In this way, no matter what operations we do in the context, only the limited part will be displayed. In other words, the function of clip() is to limit the area to be displayed. When we no longer want to limit the area, we can use the restore() method to jump out and continue operating the original context.
    Look at this cropping again:
    2016325102545351.png (275×198)

    JavaScript CodeCopy content to clipboard
    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), which is 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.  // First draw a blue arc, the part that exceeds the crop will not be 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, and the part that exceeds the crop will not be 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, and the part that exceeds the crop will not be 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 will return to the original state of the context, in this case the state before clip().
    28. * You can remove the context.beginPath() method and try what happens.
    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. }


    I emphasize again that the general calling form of cropping function is

    save();
    clip();
    restore();
    are called in this order.

    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Previous article:Summary of methods to achieve text alignment in HTML5 Canvas_html5 tutorial skillsNext article:Summary of methods to achieve text alignment in HTML5 Canvas_html5 tutorial skills

    Related articles

    See more