news 2025/12/25 14:39:41

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进行投诉反馈,一经查实,立即删除!
网站建设 2025/12/23 4:17:43

Java工程师Python实战教程:通过MCP服务器掌握Python核心语法

核心目标 本指南专为Java工程师设计&#xff0c;通过使用Python构建MCP&#xff08;Model Context Protocol&#xff09;服务器这一实际项目&#xff0c;系统讲解Python语法要点。我们将采用"结果导向"模式&#xff1a;先展示完整代码&#xff0c;再逐行解析Python语…

作者头像 李华
网站建设 2025/12/23 4:16:28

R3nzSkin英雄联盟皮肤修改器:免费体验全英雄皮肤的秘密武器

R3nzSkin英雄联盟皮肤修改器&#xff1a;免费体验全英雄皮肤的秘密武器 【免费下载链接】R3nzSkin Skin changer for League of Legends (LOL).Everyone is welcome to help improve it. 项目地址: https://gitcode.com/gh_mirrors/r3n/R3nzSkin 还在为英雄联盟中那些昂…

作者头像 李华
网站建设 2025/12/23 4:14:57

突破语言壁垒:jsPDF多语言PDF文档生成实战指南

突破语言壁垒&#xff1a;jsPDF多语言PDF文档生成实战指南 【免费下载链接】jsPDF 项目地址: https://gitcode.com/gh_mirrors/jsp/jsPDF 你是否曾经遇到过这样的场景&#xff1a;精心生成的PDF文档在海外客户那里显示异常&#xff0c;阿拉伯语文本错乱不堪&#xff0c…

作者头像 李华
网站建设 2025/12/23 4:14:15

Zygisk-Il2CppDumper完整指南:绕过保护实现Unity游戏动态分析

Zygisk-Il2CppDumper完整指南&#xff1a;绕过保护实现Unity游戏动态分析 【免费下载链接】Zygisk-Il2CppDumper Zygisk-Il2CppDumper - 利用 Zygisk 框架在运行时转储 Il2Cpp 数据的工具&#xff0c;可以绕过保护、加密和混淆&#xff0c;适合移动安全研究者和逆向工程师。 …

作者头像 李华
网站建设 2025/12/23 4:14:01

如何快速生成专业数据库文档:DBCHM终极使用教程

在软件开发过程中&#xff0c;数据库结构文档的编写往往是一项耗时且容易遗漏的工作。开发人员经常面临文档与数据库结构不同步、文档格式不统一、维护成本高等痛点。DBCHM数据库字典生成工具应运而生&#xff0c;它能够自动分析数据库结构并生成多种格式的文档&#xff0c;支持…

作者头像 李华
网站建设 2025/12/25 14:16:40

ImageGlass终极指南:快速高效的免费图片浏览解决方案

ImageGlass终极指南&#xff1a;快速高效的免费图片浏览解决方案 【免费下载链接】ImageGlass &#x1f3de; A lightweight, versatile image viewer 项目地址: https://gitcode.com/gh_mirrors/im/ImageGlass 还在为Windows系统自带的图片查看器启动缓慢、功能单一而烦…

作者头像 李华