bojue / canvas-excel

基于Canvas开发Excel的技术预研项目(React)
https://bojue.github.io/canvas-excel
71 stars 11 forks source link

根据屏幕分辩率devicePixelRatio判断canvas缩放 #9

Closed bojue closed 3 years ago

bojue commented 3 years ago

现在是通过mac/window操作系统设置canvas屏幕缩放比例

bojue commented 3 years ago

canvas的dpr兼容原理

var ctx = canvas.getContext('2d');          
var dpr = window.devicePixelRatio || window.webkitDevicePixelRatio || window.mozDevicePixelRatio || 1;
var w = canvas.width;
var h = canvas.height;
canvas.width = Math.round(w * dpr);
canvas.height = Math.round(h * dpr);
canvas.style.width = w + 'px';
canvas.style.height = h + 'px';
ctx.scale(dpr, dpr);