【发布时间】:2017-08-23 22:40:08
【问题描述】:
我在画布上有网络摄像头流,但无法在顶部显示矩形。网络摄像头流有效。 问题是矩形没有出现,有时矩形会快速闪烁(而且也很模糊......)
网络摄像头到画布设置:
const canvas = document.querySelector('.photo');
const ctx = canvas.getContext('2d');
// gets the video stream from the user's webcam
function getVideo() {
navigator.mediaDevices.getUserMedia({
video: true,
audio: false
}).then(localMediaStream => {
video.src = window.URL.createObjectURL(localMediaStream);
video.play();
}).catch(err => {
console.error('oh no', err);
});
}
// Applies the webcam stream to a canvas
function paintToCanvas() {
const width = video.videoWidth;
const height = video.videoHeight;
canvas.width = width;
canvas.height = height;
return setInterval(() => {
ctx.drawImage(video, 0, 0, width, height);
}, 16);
}
在顶部绘制矩形:
function drawRectangle() {
ctx.rect(150, 150, 50, 30);
ctx.lineWidth = 2;
ctx.strokeStyle = '#FF0000';
ctx.stroke();
}
drawRectangle();
我尝试了不同的笔触、线宽和面积,但结果相同。 我究竟做错了什么?为什么它有时会闪烁(在页面加载时)而不是停留?
【问题讨论】:
标签: javascript canvas webcam