700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > canvas绘图在高清屏显示模糊

canvas绘图在高清屏显示模糊

时间:2022-08-19 14:19:51

相关推荐

canvas绘图在高清屏显示模糊

问题:canvas在高清屏显示模糊

前几天在做PDF预览时(使用mozilla的pdf.js),发现PDF文字在canvas中显示非常模糊,在网上搜出来的中文结果都没有这方面相关的,后面用英文搜索到了原因,因为我测试用的是retina屏。立即用普通显示器上试了一下,表现果然是正常的。

分析

设备像素比 = 物理像素 / 设备独立像素dip

canvas元素依赖于设备像素比,在retina屏中设备像素比是2,意味着宽度为100pxcanvas,需要200px的填充才会清晰地显示。

解决方法

既然是高清屏的原因,那么只要对高清屏做下适配就OK啦。

代码

pdf.js的解决方案详见 /lwenn/pdf-viewer-easy

// 获取设备像素比var PIXEL_RATIO = (function () {var ctx = document.createElement('canvas').getContext('2d'),dpr = window.devicePixelRatio || 1,bsr = ctx.webkitBackingStorePixelRatio ||ctx.mozBackingStorePixelRatio ||ctx.msBackingStorePixelRatio ||ctx.oBackingStorePixelRatio ||ctx.backingStorePixelRatio || 1;return dpr / bsr;})();var canvasWidth = 1000,canvasHeight = 500;// 适配高清屏,canvas内容的宽高是实际的宽高的PIXEL_RATIO倍canvas.width = canvasWidth * PIXEL_RATIO;canvas.height = canvasHeight * PIXEL_RATIO;canvas.style.width = canvasWidth + 'px';canvas.style.height = canvasHeight + 'px';// 缩放绘图context.setTransform(PIXEL_RATIO, 0, 0, PIXEL_RATIO, 0, 0);

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。