<video ref="video" :src="videoSrc" type="video/mp4" id="myVideo" width="200px" height="200px"
show-snapshot-button></video>
<button @click="capture" id="captureBtn">拍照</button>
<canvas id="screenshotCanvas" style="display:none;"></canvas>
methods: {
capture() {
const video = document.getElementById('myVideo');
const canvas = document.getElementById('screenshotCanvas');
const ctx = canvas.getContext('2d');
video.addEventListener('canplaythrough', function() {
// 设置canvas尺寸与视频相同
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
// 绘制视频帧到canvas
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
});
// 绘制视频帧到canvas
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
// 将canvas图像转为URL格式
const imgURL = canvas.toDataURL('image/jpeg'); // 或者 'image/png'
console.log(imgURL);
// 预览截图,插入到preview div中
const img = document.createElement('img');
img.src = imgURL;
img.className = 'previewImg';
document.getElementById('preview').appendChild(img);
},
}
然后报错Error in v-on handler: "TypeError: canvas.getContext is not a function"