【发布时间】:2015-08-06 19:55:45
【问题描述】:
我正在尝试将 canvas 的 drawImage 方法与视频源一起使用,但它在 Android 4.4.2 中不起作用,并在 Chrome 浏览器中进行了测试。
这是我的代码:
$(function() {
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var video = document.getElementById('video');
var videoWidth;
var videoHeight;
var paused = false;
canvas.addEventListener('click', function() {
if (paused) {
video.play();
} else {
video.pause();
}
paused = !paused;
});
video.addEventListener("loadedmetadata", function() {
videoWidth = this.videoWidth || this.width;
videoHeight = this.videoHeight || this.height;
canvas.width = videoWidth;
canvas.height = videoHeight;
}, false);
video.addEventListener('play', function() {
var $this = this; // cache
(function loop() {
if ( ! $this.paused && ! $this.ended ) {
drawImage($this);
requestAnimationFrame(loop);
// setTimeout(loop, 1000 / 25); // drawing at 25 fps
}
})();
}, 0);
function drawImage(frame) {
ctx.drawImage(frame, 0, 0);
// ctx.clearRect ( 0 , 0 , canvas.width, canvas.height );
}
});
小提琴: http://jsfiddle.net/h1hjp0Lp/
有没有办法让它也适用于 Android 和 iOS 设备?
【问题讨论】:
-
尝试将
click事件替换为touchstart,看看是否有效。 -
@Allen,谢谢,但它不起作用。
-
阅读此内容后,Chrome 移动版似乎存在错误。我刚刚检查了以前使用 Video -> Canvas 完成的演示,它们不再工作。我会继续调查。 (Android 5.1)
标签: android html video canvas drawimage