网站首页 > 技术文章 正文
<video id="video" style="height: 400px;width: 400px;" autoplay></video>
<button id="start">开始</button>
<button id="stop">停止</button>
<script>
// js
const displayMediaOptions = { // 参数的详情可以在mdn网站查找
video: {
cursor: "always"
},
audio: false
};
let videoElem = document.getElementById("video");
document.getElementById("start").addEventListener('click', function () {
startCapture()
})
document.getElementById("stop").addEventListener('click', function () {
stopCapture()
})
async function startCapture() {
try {
videoElem.srcObject = await navigator.mediaDevices.getDisplayMedia(displayMediaOptions);
videoElem.addEventListener("loadedmetadata", function () {
canvas.width = videoElem.videoWidth;
canvas.height = videoElem.videoHeight;
});
videoElem.addEventListener("timeupdate", function () {
try {
const currentTime = parseFloat(videoElem.currentTime);
if (currentTime >= 2) {
stopCapture()
}
} catch (err) {
console.log("video has deleted: " + err);
}
});
videoElem.play();
} catch (err) {
console.error("Error: " + err);
}
}
function stopCapture(evt) {
let ctx = canvas.getContext("2d");
ctx.drawImage(videoElem, 0, 0, videoElem.videoWidth, videoElem.videoHeight);
//拿到一个dataUrl,可new Image(),然后图片的对象的src设置为url,就可以得到图片dom元素
let imgURL = canvas.toDataURL("image/jpeg");
console.log(imgURL)
let tracks = videoElem.srcObject.getTracks();
tracks.forEach(track => track.stop());
videoElem.srcObject = null;
}
</script>
猜你喜欢
- 2024-11-21 延迟加载图像以提高Web网站性能的五种方法「实践」
- 2024-11-21 web前端学到什么程度就可以找工作了?学到什么程度可以涨薪?
- 2024-11-21 web前端学到什么程度就可以找工作了?
- 2024-11-21 flv.js源码知识点(下)
- 2024-11-21 iG战队让一追三夺得德玛西亚杯西安站冠军
- 2024-11-21 前端新趋势?有了Web Component,还用纠结Vue或React?
- 2024-11-21 Python Web全栈之旅11--Web前端●CSS知识点汇总,建议收藏
- 2024-11-21 快速掌握前端开发中的常见错误
- 2024-11-21 用WebRTC和Node.js开发实时视频聊天应用
- 2024-11-21 9 个你可能从未使用过的很棒的 CSS 属性
- 标签列表
-
- content-disposition (47)
- nth-child (56)
- math.pow (44)
- 原型和原型链 (63)
- canvas mdn (36)
- css @media (49)
- promise mdn (39)
- readasdataurl (52)
- if-modified-since (49)
- css ::after (50)
- border-image-slice (40)
- flex mdn (37)
- .join (41)
- function.apply (60)
- input type number (64)
- weakmap (62)
- js arguments (45)
- js delete方法 (61)
- blob type (44)
- math.max.apply (51)
- js (44)
- firefox 3 (47)
- cssbox-sizing (52)
- js删除 (49)
- js for continue (56)
- 最新留言
-