news 2026/8/3 20:58:48

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/2 19:16:28

终极指南:5分钟让Windows完美显示iPhone HEIC照片缩略图

还在为Windows系统无法预览iPhone拍摄的HEIC格式照片而烦恼吗&#xff1f;每次在资源管理器中看到一堆灰色图标&#xff0c;却不知道哪张才是你想要的照片&#xff1f;今天为大家带来一款开源神器——windows-heic-thumbnails&#xff0c;它能彻底解决这个问题&#xff0c;让你…

作者头像 李华
网站建设 2026/8/3 8:00:34

Cimoc:Android平台终极漫画阅读解决方案

Cimoc&#xff1a;Android平台终极漫画阅读解决方案 【免费下载链接】Cimoc 漫画阅读器 项目地址: https://gitcode.com/gh_mirrors/ci/Cimoc 在移动互联网时代&#xff0c;漫画爱好者需要一个既能聚合全网资源&#xff0c;又能提供纯净阅读体验的工具。Cimoc作为开源An…

作者头像 李华
网站建设 2026/8/3 13:33:27

TrollInstallerX下载被拦截?这些方法让你顺利安装

TrollInstallerX下载被拦截&#xff1f;这些方法让你顺利安装 【免费下载链接】TrollInstallerX A TrollStore installer for iOS 14.0 - 16.6.1 项目地址: https://gitcode.com/gh_mirrors/tr/TrollInstallerX 为什么每次下载TrollInstallerX时总被系统拦截&#xff1f…

作者头像 李华
网站建设 2026/8/3 14:11:44

Draw.io Mermaid插件终极指南:从代码到图表的智能革命

Draw.io Mermaid插件终极指南&#xff1a;从代码到图表的智能革命 【免费下载链接】drawio_mermaid_plugin Mermaid plugin for drawio desktop 项目地址: https://gitcode.com/gh_mirrors/dr/drawio_mermaid_plugin 在当今快节奏的技术开发环境中&#xff0c;传统的手动…

作者头像 李华
网站建设 2026/8/3 13:31:30

如何快速掌握HSTracker:macOS炉石传说智能助手的完整指南

还在为记不住对手卡牌而苦恼&#xff1f;每次对战都感觉在"盲打"&#xff1f;这款专为macOS打造的HSTracker工具将彻底改变你的游戏体验&#xff0c;让你从被动应对转向主动掌控&#xff01; 【免费下载链接】HSTracker A deck tracker and deck manager for Hearths…

作者头像 李华
网站建设 2026/8/3 14:10:25

VDA5050协议终极指南:AGV通信标准的完整解析与实战应用

VDA5050协议终极指南&#xff1a;AGV通信标准的完整解析与实战应用 【免费下载链接】VDA5050 项目地址: https://gitcode.com/gh_mirrors/vd/VDA5050 在智能制造和工业4.0的浪潮中&#xff0c;自动化导引车&#xff08;AGV&#xff09;作为智能物流的核心装备&#xff…

作者头像 李华