简体   繁体   English

使用HTML5画布的遮罩效果

[英]Masking effect using HTML5 canvas

Using canvas and mousemove I create a masking effect. 使用canvasmousemove创建遮罩效果。

Example: FIDDLE 示例: FIDDLE

The masking effect works but on fast scrolling performance isn't ideal. 遮罩效果有效,但在快速滚动性能上并不理想。

How can I improve the framerate? 如何提高帧率?

I fixed it simply by using the window mouse coordinates, instead of the listener on the canvas (assuming you want the picture to just be black). 我只是通过使用窗口鼠标坐标而不是画布上的侦听器来修复它(假设您希望图片只是黑色的)。

http://jsfiddle.net/gfZ5C/166/ http://jsfiddle.net/gfZ5C/166/

I also changed to requestAnimationFrame, you'll notice a complete FPS difference in the movement of the circle, instead of it moving according to the listener. 我还更改为requestAnimationFrame,您会注意到圆的移动完全是FPS差异,而不是根据侦听器移动。

window.onmousemove = function (e) {
    circle.posX = e.pageX-can.offsetLeft;
    circle.posY = e.pageY-can.offsetTop;
    circle.hide = (circle.posX >= 550 || circle.posY >= 550) ? true : false;
}
if (!circle.hide) ctx.arc(circle.posX, circle.posY, 70, 0, Math.PI * 2, true);

I left it at 550, so that you could see it actually disappear, you can change it to a big number if you want the circle to just drag right off the canvas. 我将其保留在550,这样您就可以看到它实际上消失了,如果您希望圆刚从画布上拖出,则可以将其更改为一个大数字。

Also there is no lag on the circle following the mouse now. 现在,跟随鼠标的圆圈也没有滞后了。

Update: Fixed the fiddle so that the circle adjusts for window offset too. 更新:修正了小提琴,使圆也可以针对窗口偏移进行调整。 The offset is relevant to its parent element, so depending on how far deep your canvas is, you will have to subtract each elements offset that it is inside. 偏移量与其父元素有关,因此,根据画布的深度,您必须减去其内部的每个元素偏移量。

Update : Switched to handle 'onmouse' events, this will work much better on a real site, because mousing over the iFrame doesn't work well, but it still works. 更新 :切换为处理“ onmouse”事件,这将在实际站点上更好地工作,因为在iFrame上进行鼠标操作虽然效果不佳,但仍然有效。 Will work 100% as intended on a real site. 将在实际网站上按预期工作100%。

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

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