简体   繁体   English

如何在画布元素上绘制视频?

[英]How to draw over video on a canvas element?

I'm trying to use a canvas for two things in my application.我正在尝试将画布用于我的应用程序中的两件事。 First is to display a video on canvas (which I already did).首先是在画布上显示视频(我已经这样做了)。 After that, I need to draw squares over this video, and it's in that part that I've got some trouble.在那之后,我需要在这个视频上画正方形,在这部分我遇到了一些麻烦。 When I try to use one canvas over another, the video always stays on top of the squares I'm creating.当我尝试在另一个画布上使用一个画布时,视频总是停留在我创建的方块的顶部。 If i use only one canvas the same thing happens.如果我只使用一张画布,也会发生同样的事情。 Can someone help me please?有人能帮助我吗?

With canvas it's all about order of operations, in your example you are drawing a video and want the sqaures on top, so you need to draw the squares AFTER you draw the video.使用画布,一切都与操作顺序有关,在您的示例中,您正在绘制视频并希望正方形位于顶部,因此您需要在绘制视频绘制正方形。

Simple example:简单的例子:

https://jsfiddle.net/a3z5hb45/ https://jsfiddle.net/a3z5hb45/

ctx.fillStyle = "red"
ctx.fillRect(x, y, 100, 100);

ctx.fillStyle = "blue"
ctx.fillRect(x + 50, y + 50, 100, 100);

Here you see that red goes under blue.在这里你看到红色在蓝色下面。 Imagine that red is your video, so if you drew something after it will appear above.想象一下,红色是你的视频,所以如果你画了一些东西,它就会出现在上面。

If you reverse the order you get this:如果你颠倒顺序,你会得到这个:

https://jsfiddle.net/a3z5hb45/1/ https://jsfiddle.net/a3z5hb45/1/

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

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