js 给html设置dpi,javascript - Higher DPI graphics with HTML5 canvas - Stack Overflow

Canvases have two different 'sizes': their DOM width/height and their CSS width/height. You can increase a canvas' resolution by increasing the DOM size while keeping the CSS size fixed, and then using the .scale() method to scale all of your future draws to the new bigger size. Here's an example:

function changeResolution(canvas, scaleFactor) {

// Set up CSS size.

canvas.style.width = canvas.style.width || canvas.width + 'px';

canvas.style.height = canvas.style.height || canvas.height + 'px';

// Resize canvas and scale future draws.

canvas.width = Math.ceil(canvas.width * scaleFactor);

canvas.height = Math.ceil(canvas.height * scaleFactor);

var ctx = canvas.getContext('2d');

ctx.scale(scaleFactor, scaleFactor);

}

The canvas default resolution is 96dpi (CSS inches, not based on the actual screen). So a scaleFactor of 2 gives 192dpi, 3 is 288dpi, etc. In fact, here's a version that should give your desired DPI:

function setDPI(canvas, dpi) {

// Set up CSS size.

canvas.style.width = canvas.style.width || canvas.width + 'px';

canvas.style.height = canvas.style.height || canvas.height + 'px';

// Resize canvas and scale future draws.

var scaleFactor = dpi / 96;

canvas.width = Math.ceil(canvas.width * scaleFactor);

canvas.height = Math.ceil(canvas.height * scaleFactor);

var ctx = canvas.getContext('2d');

ctx.scale(scaleFactor, scaleFactor);

}

Have fun! Note that both these code samples can only be used once per canvas, they assume the current DOM size is the original (they could be tweaked to change that). Also the rescaling needs to happen before you do any drawing on the canvas. Thanks to this post for the method and information!

Edit: Here is a more robust function that will scale future draws and maintain existing canvas contents. This can be called to rescale multiple times.

function setDPI(canvas, dpi) {

// Set up CSS size.

canvas.style.width = canvas.style.width || canvas.width + 'px';

canvas.style.height = canvas.style.height || canvas.height + 'px';

// Get size information.

var scaleFactor = dpi / 96;

var width = parseFloat(canvas.style.width);

var height = parseFloat(canvas.style.height);

// Backup the canvas contents.

var oldScale = canvas.width / width;

var backupScale = scaleFactor / oldScale;

var backup = canvas.cloneNode(false);

backup.getContext('2d').drawImage(canvas, 0, 0);

// Resize the canvas.

var ctx = canvas.getContext('2d');

canvas.width = Math.ceil(width * scaleFactor);

canvas.height = Math.ceil(height * scaleFactor);

// Redraw the canvas image and scale future draws.

ctx.setTransform(backupScale, 0, 0, backupScale, 0, 0);

ctx.drawImage(backup, 0, 0);

ctx.setTransform(scaleFactor, 0, 0, scaleFactor, 0, 0);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值