您可以通过使用画布元素来解决这个问题。它需要更多的代码,但这种方法也开辟了许多其他可能性和控制。
您可以将 CSS 与 Canvas 元素一起应用:
#canvas {
background:#000;
border-radius:20px 0 0 20px; /* top left and bottom left as in OP */
}
然后手动创建和加载视频(或者您可以使用 HTML 加载视频并隐藏原始视频元素):
var video = document.createElement('video'),
url;
/// setup video instance
video.preload = 'auto';
video.addEventListener('canplaythrough', start, false);
/// check what we can play and borrow some online video link
if (video.canPlayType('video/ogg').length > 0 ) {
url = 'http://www.w3schools.com/html/movie.ogg';
} else {
url = 'http://www.w3schools.com/html/movie.mp4';
}
/// start loading video
video.src = url;
/// start the loop
function start(e) {
/// get canvas and context
var canvas = document.getElementById('canvas'),
ctx = canvas.getContext('2d'),
w = canvas.width,
h = canvas.height,
toggle = false;
/// start video and loop
video.play();
loop();
function loop() {
/// we won't need 60 FPS so reduce to 30 FPS
toggle = !toggle;
if (toggle === false) {
requestAnimationFrame(loop);
return;
}
/// draw video frame onto canvas
ctx.drawImage(video, 0, 0, w, h);
requestAnimationFrame(loop);
}
}
这当然是最小的例子:对于其他类型,您需要正确检查 canPlayType 参数(有些浏览器说“不”,所以也检查这个或用空字符串替换 no 以使用长度,如这里)好吧(即 webm)。
但请注意:这不适用于 Safari 浏览器,因为它们不符合(在当前编写时)Canvas 标准。在这种情况下,它们不支持以视频元素为源的 drawImage。