简体   繁体   English

html5中的画布孔,图像不起作用

[英]canvas hole in html5 with image not working

I am trying to create a hole in canvas; 我正在尝试在画布上打洞; and loading image in canvas after. 然后将图像加载到画布中。

I want this canvas to contain a hole at a top layer. 我希望此画布在顶层包含一个孔。

I just read about counter-clockwise canvas rules and then created hole with counter clockwise position as below- 我刚刚阅读了逆时针画布规则,然后创建了具有如下逆时针位置的孔-

  var c = document.getElementById("canvas-front");
        var ctx = c.getContext("2d");
        var centerX = c.width / 2;
        var centerY = c.offsetTop;
        var radius = 30;
        ctx.beginPath();
        ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI,true);//boolean true to counter-clockwise
        ctx.fillStyle = 'black';
        ctx.fill();

I have canvas before image in it- 我的图像之前有画布-

在此处输入图片说明

After applying image in this canvas- 在此画布中应用图片后,

在此处输入图片说明

Canvas- 帆布-

                    <canvas id="canvas-front" width="261" height="506"></canvas>

As you can see from both the images, I can't get this hole in canvas work. 从这两个图像中都可以看到,我无法在画布工作中遇到这个漏洞。

How do I create this arc so that image doesn't overlap this. 如何创建此弧线,以使图像不会与此重叠。

I get this issue solved- 我解决了这个问题-

What I had to do- 我要做的是

On change of file I did empty to canvas and redrew image and then while image is being loaded the arc is being created to counter-clockwise position- 更改文件后,我清空了画布并重新绘制图像,然后在加载图像时将弧创建为逆时针位置,

Drawing the canvas again on image change- 再次在图像更改上绘制画布-

   var canvas = document.getElementById("canvas-front");
                        canvas.width = canvas.width;//blanks the canvas
                        var c = canvas.getContext("2d");

Creating image and giving it required src from changed input- 创建图片并根据更改后的输入提供所需的src-

 var img = new Image();
    img.src = e.target.result;

Loading image and creating arc parallely- 加载图像并创建圆弧平行线-

   img.onload = function () {
   c.drawImage(img, 0, 0);
   var centerX = canvas.width / 2;
   var centerY = canvas.offsetTop;
   var radius = 30;
   c.beginPath();
   c.arc(centerX, centerY, radius, 0, 2 * Math.PI, true);
   c.fillStyle = 'black';
   c.fill();
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM