有个window属性叫devicePixelRatio
,能够算出过后设施的物理像素与CSS像素的比率。
语法:
value = window.devicePixelRatio;
用法:
<script> /** @type {HTMLCanvasElement} */ let canvas = document.getElementById("canvas") let ctx = canvas.getContext('2d') let dpr = window.devicePixelRatio || window.webkitDevicePixelRatio || window.mozDevicePixelRatio || 1; canvas.width = 600 canvas.height = 400 canvas.style.width = canvas.width + 'px' canvas.style.height = canvas.height + 'px' canvas.width = canvas.width * dpr canvas.height = canvas.height * dpr ctx.scale(dpr, dpr); ctx.rect(20, 30, 400, 200) ctx.fillStyle = "purple" ctx.fillRect(20, 30, 400, 300) ctx.font = '28px Arial' ctx.fillStyle = '#fff' ctx.fillText('ABCD', 100,150) ctx.strokeStyle = '#fff' ctx.strokeText('YES',100,180) </script>