【发布时间】:2016-05-02 07:54:14
【问题描述】:
我是画布的新手,我已经在谷歌上搜索了几个小时,但我被卡住了。
我想做的是在画布元素上渲染视频,将其分割并为各个部分设置动画。我已经完成了一半(请参阅:http://jsbin.com/riduxadazi/edit?html,css,js,console,output)但我有几个问题:
- 我做的事情是对的,还是效率极低?
- 我想全屏使用视频。无论我尝试什么,画布网格 + 视频似乎都不匹配大小。
- 我想为视频片段制作动画,但我不知道应该如何处理它们。我可以得到某种数组并一个一个地为这些片段设置动画吗?
我的 JS 看起来像这样。我试图将 cmets 添加到最重要的部分。至少我认为是最重要的部分;)
var video = document.getElementById('video'); // Get the video
var ctx = canvas.getContext('2d'),
columns = 6,
rows = 4,
w, h, tileWidth, tileHeight;
// Start video and add it to canvas
video.addEventListener('play', function() {
var $this = this; //cache
(function loop() {
if (!$this.paused && !$this.ended) {
ctx.drawImage($this, 0, 0,window.innerWidth,window.innerHeight);
calcSize(); // Divide video
setTimeout(loop, 1000 / 30); // drawing at 30fps
}
})();
}, 0);
function calcSize() {
video.width = w = window.innerWidth;
video.height = h = window.innerHeight;
tileWidth = w / columns;
tileHeight = h / rows;
ctx.strokeStyle = '#000';
render();
}
function render() {
for(var x = 0; x < columns; x++) {
ctx.moveTo(x * tileWidth, 0);
ctx.lineTo(x * tileWidth, h);
}
for(var y = 0; y < rows; y++) {
ctx.moveTo(0, y * tileHeight);
ctx.lineTo(w, y * tileHeight);
}
ctx.stroke();
}
【问题讨论】:
-
所以现在你只是在你的视频上画一个网格对吗?您必须使用单元格的坐标和 drawImage 的裁剪选项来实际分割您的视频。
标签: javascript jquery canvas html5-canvas html5-video