HTML Canvas 剪辑和 putImageData
我有一个画布,背景中有一个大图像,前面有一个较小的圆形图像。我通过使用像这样的剪辑实现了这个圆形图像效果
ctx.save();
ctx.beginPath();
ctx.arc(x,y,r,0,Math.PI*2, true);
ctx.closePath();
ctx.clip();
ctx.drawImage(img,x-r,y-r,2*r,2*r); // draw the image
ctx.restore();
,然后我想旋转圆形图像,所以我使用第二个上下文并像这样旋转和重绘
backCanvas=document.createElement('canvas');
backContext=backCanvas.getContext('2d');
backCanvas.width=w;
backCanvas.height=h;
backContext.translate(w/2,h/2);
backContext.rotate(a);
backContext.drawImage(img,-w/2,-h/2,w,h);
var imgData=backContext.getImageData(0,0,w,h);
ctx.save();
ctx.beginPath();
ctx.arc(x,y,r,0,Math.PI*2, true);
ctx.closePath();
ctx.clip();
ctx.putImageData(imgData,x,y);
ctx.restore();
但是发生的情况是黑色透明背景从背面画布复制,并且剪辑无法“剪辑”它。
任何帮助将不胜感激
I have a canvas with a large image in the background and a smaller round image in front of it. I achieved this round image effect by using clip like so
ctx.save();
ctx.beginPath();
ctx.arc(x,y,r,0,Math.PI*2, true);
ctx.closePath();
ctx.clip();
ctx.drawImage(img,x-r,y-r,2*r,2*r); // draw the image
ctx.restore();
then I want to rotate the round image, so I use a second context and rotate and redraw like so
backCanvas=document.createElement('canvas');
backContext=backCanvas.getContext('2d');
backCanvas.width=w;
backCanvas.height=h;
backContext.translate(w/2,h/2);
backContext.rotate(a);
backContext.drawImage(img,-w/2,-h/2,w,h);
var imgData=backContext.getImageData(0,0,w,h);
ctx.save();
ctx.beginPath();
ctx.arc(x,y,r,0,Math.PI*2, true);
ctx.closePath();
ctx.clip();
ctx.putImageData(imgData,x,y);
ctx.restore();
But what happens is that the black-transparent background gets copied from the back canvas and the clip fails to "clip" it.
Any help would be appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据
就您而言,为什么要使用额外的画布和像素数据操作?为什么不只是
According to the specs,
In your case, why are you using an additional canvas and pixel data manipulations? Why not just