我正在使用this代码来创建淡化线索。它使用context.fillStyle = 'rgba(0,0,0,0.12)';
来创建淡入淡出。问题是我想在<video>
元素顶部使用淡化轨迹,淡入淡出隐藏<video>
元素。
有没有办法在<video>
之上放置一个淡入淡出的画布而不隐藏它?
答案 0 :(得分:2)
您可以使用全局alpha -
将视频元素绘制到画布<强>更新强>
要启用淡出,您也可以使用此修改后的代码(演示使用按钮触发淡出,但您可以从任何设置fade = true
启动它):
的 ONLINE DEMO 强>
var fade = false;
var fadeVal = 1;
ctx.globalAlpha = 0.2; //adjust as needed
function loop() {
/// draw frame from video at current global alpha
if (video1) ctx.drawImage(video1, 0, 0, w, h);
/// if fade = true start fade out
if (fade === true) {
/// we need to make opaque again or the black box
/// will be transparent resulting in none-working effect
ctx.globalAlpha = 0.2 + (1-fadeVal);
/// set fill color for black box
ctx.fillStyle = 'rgba(0,0,0,' + (1 - fadeVal) + ')';
/// draw on top of video
ctx.fillRect(0, 0, w, h);
/// speed of fade out
fadeVal -= 0.02;
}
/// when faded out, stop loop (stop video too, not shown)
if (fadeVal >= 0) requestAnimationFrame(loop);
}
loop();
这将从之前绘制的帧中留下视频片段,并允许您淡出保留路径的视频。
这只是一个愚蠢的例子,只有很多方法可以做到这一点 - 根据您的需求进行调整。
要重新启动视频,请设置fade = false
,fadeVal = 1
并调用loop()
。