news 2026/8/26 10:38:43

canvas的画布尺寸

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
canvas的画布尺寸

这个设置的是canvas的画布尺寸

canvas.width = myVideo.videoWidth || 900; // 视频原生宽度

canvas.height = myVideo.videoHeight || 500; // 视频原生高度

这个设置的是canvas html 元素在页面上显示的尺寸

canvas.style.width = "900px"; // 保持显示尺寸

canvas.style.height = "500px"; // 保持显示尺寸

当canvas的画布尺寸(像素尺寸)过小的时候,画面就会很模糊

下面是使用canvas复制video画面的一个demo

<html> <head> <meta charset="UTF-8"> <link rel="icon" type="image/png" href="webrtc.png" /> <script src="webrtcconfig.js" ></script> <script src="webrtcstreamer.js" ></script> </head> <body> <!-- muted 静音播放 --> <!-- iOS 需要 playsinline 和 muted 才能自动播放 --> <!-- <video autoplay muted playsinline></video> --> <video id="my-video" style="width: 900px; height: 500px;" muted ></video> <canvas id="displayCanvas" style="width: 900px; height: 500px;"></canvas> </body> <script> const myVideo = document.getElementById('my-video'); const canvas = document.getElementById('displayCanvas'); const ctx = canvas.getContext('2d'); window.onload = function() { this.webRtcServer = new WebRtcStreamer("my-video", webrtcConfig.url); this.webRtcServer.connect("a12-篮球场-东北角", "", webrtcConfig.options); // setTimeout(()=>{ // console.log('=== 分辨率诊断 ==='); // console.log('Video 视频尺寸:', myVideo.videoWidth, '×', myVideo.videoHeight); // console.log('Video 显示尺寸:', myVideo.clientWidth, '×', myVideo.clientHeight); // console.log('Canvas 像素尺寸:', canvas.width, '×', canvas.height); // console.log('Canvas 显示尺寸:', canvas.clientWidth, '×', canvas.clientHeight); // console.log('设备像素比:', window.devicePixelRatio); // console.log('=================='); // }, 5000); // 开始绘制到画布 setTimeout(()=>{ drawVideoToCanvas(); }, 5000); } window.onbeforeunload = function() { this.webRtcServer.disconnect() } // 绘制视频到画布 function drawVideoToCanvas() { // console.log(myVideo.readyState); if (myVideo.readyState !== 4) { requestAnimationFrame(drawVideoToCanvas); return; } // 清除画布 ctx.clearRect(0, 0, canvas.width, canvas.height); // 将Canvas像素尺寸设置为与视频原生分辨率一致 canvas.width = myVideo.videoWidth || 900; // 视频原生宽度 canvas.height = myVideo.videoHeight || 500; // 视频原生高度 canvas.style.width = "900px"; // 保持显示尺寸 canvas.style.height = "500px"; // 保持显示尺寸 // 绘制视频到画布 ctx.drawImage(myVideo, 0, 0, canvas.width, canvas.height); // 添加边框效果 ctx.strokeStyle = '#4cc9f0'; ctx.lineWidth = 3; ctx.strokeRect(0, 0, canvas.width, canvas.height); // 继续绘制下一帧 requestAnimationFrame(drawVideoToCanvas); } </script> </html>
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/25 12:21:25

FileSaver.js终极指南:轻松实现前端文件下载功能

FileSaver.js终极指南&#xff1a;轻松实现前端文件下载功能 【免费下载链接】FileSaver.js An HTML5 saveAs() FileSaver implementation 项目地址: https://gitcode.com/gh_mirrors/fi/FileSaver.js 还在为前端文件下载的各种兼容性问题而烦恼吗&#xff1f;FileSaver…

作者头像 李华
网站建设 2026/8/25 19:54:03

Taskbar Lyrics终极配置指南:Windows 11任务栏歌词完整部署手册

Taskbar Lyrics终极配置指南&#xff1a;Windows 11任务栏歌词完整部署手册 【免费下载链接】Taskbar-Lyrics BetterNCM插件&#xff0c;在任务栏上嵌入歌词&#xff0c;目前仅建议Windows 11 项目地址: https://gitcode.com/gh_mirrors/ta/Taskbar-Lyrics Taskbar Lyri…

作者头像 李华
网站建设 2026/8/25 8:08:27

解放双手:智能剧情助手让鸣潮体验更纯粹

解放双手&#xff1a;智能剧情助手让鸣潮体验更纯粹 【免费下载链接】better-wuthering-waves &#x1f30a;更好的鸣潮 - 后台自动剧情 项目地址: https://gitcode.com/gh_mirrors/be/better-wuthering-waves 还记得那些深夜赶进度&#xff0c;却被重复剧情点击折磨到手…

作者头像 李华
网站建设 2026/8/25 9:04:13

ColabFold快速实战手册:AI蛋白质预测的极速入门

ColabFold快速实战手册&#xff1a;AI蛋白质预测的极速入门 【免费下载链接】ColabFold 项目地址: https://gitcode.com/gh_mirrors/co/ColabFold ColabFold作为一款基于AlphaFold2和RoseTTAFold等先进AI模型的蛋白质结构预测工具&#xff0c;正在改变生物信息学研究的…

作者头像 李华
网站建设 2026/8/25 17:20:54

Umi-OCR:让文字识别变得如此简单

还在为图片中的文字无法复制而烦恼吗&#xff1f;每天面对大量扫描文档、截图资料时&#xff0c;手动输入文字既耗时又容易出错。Umi-OCR这款免费开源的离线OCR工具&#xff0c;正是为了解决这些痛点而生。无论你是学生、办公人员还是开发者&#xff0c;这款工具都能让你的文档…

作者头像 李华