简体   繁体   English

调整HTML5画布元素的大小

[英]Resize HTML5 canvas element

How can I achieve, so that the HTML5 canvas element ist resizeable? 我怎样才能实现,以便HTML5 canvas元素可以调整大小?

I would like to implement this element so that you can scale it in any size. 我想实现这个元素,以便您可以以任何大小缩放它。 The event of the scaling should be the mouse which snaps the edge and the user resizes the element. 缩放的事件应该是捕捉边缘并且用户调整元素大小的鼠标。

I've already read about how you can achieve this on a object in the canvas element. 我已经阅读过如何在canvas元素中的对象上实现这一点。 But in my case I need this on the canvas element itself ( <canvas> ). 但在我的情况下,我需要在canvas元素本身( <canvas> )上。

Setting a canvas's width or height properties has the effect of clearing the canvas. 设置画布的宽度或高度属性具有清除画布的效果。 Even canvas.width = canvas.width ; 甚至canvas.width = canvas.width ; will cause you to lose everything in the canvas. 会导致你丢失画布中的所有内容。 Sometimes this is desirable, but in your case it probably isn't. 有时这是可取的,但在你的情况下,它可能不是。

What you will probably need to do is something like this 您可能需要做的是这样的事情

 var myCanvas = document.getElementById('myCanvas');
 var tempCanvas = document.createElement('canvas');
 tempCanvas.width = myCanvas.width;
 tempCanvas.height = myCanvas.height;

 // save your canvas into temp canvas
 tempCanvas.getContext('2d').drawImage(myCanvas, 0, 0);

 // resize my canvas as needed, probably in response to mouse events
 myCanvas.width = newWidth;
 myCanvas.height = newHeight;

 // draw temp canvas back into myCanvas, scaled as needed
 myCanvas.getContext('2d').drawImage(tempCanvas, 0, 0, tempCanvas.width, tempCanvas.height, 0, 0, myCanvas.width, myCanvas.height);

In most browsers, the scaling will be done with a bicubic scaling algorithm, causing it to get blurry. 在大多数浏览器中,缩放将使用双三次缩放算​​法完成,从而导致模糊。 In some cases you can set a CSS property to cause nearest neighbor on the canvas if you want, but browser support for this is very spotty right now. 在某些情况下,如果需要,您可以设置CSS属性以在画布上生成最近邻居,但是浏览器对此的支持现在非常不稳定。 You can instead manually do a nearest neighbor scale , as this question shows: How to stretch images with no antialiasing 您可以手动执行最近邻居比例,因为此问题显示: 如何在不使用抗锯齿的情况下拉伸图像

Alternative CSS Approach 替代CSS方法

Another approach is to scale the canvas using CSS. 另一种方法是使用CSS缩放画布。 In Chrome/Safari/IE you can just do: 在Chrome / Safari / IE中你可以这样做:

<canvas style="zoom:200%" />

In Firefox, you can use a scale transform to achieve the same effect: 在Firefox中,您可以使用缩放变换来实现相同的效果:

<canvas style="-moz-transform:scale(2)" />

In many ways this approach is easier, but it comes with its own little gotchas and browser specific quirks. 在许多方面,这种方法更容易,但它带有自己的小问题和浏览器特定的怪癖。

I think you need to bind the onresize event to your body of document. 我认为您需要将onresize事件绑定到您的文档正文。

Then inside the the event you need to resize the canvas using window.innerWidth and window.innerHeight. 然后在事件内部,您需要使用window.innerWidth和window.innerHeight调整画布大小。

Have a look @ http://kile.stravaganza.org/lab/js/canvas_resize/ (view source) 看看@ http://kile.stravaganza.org/lab/js/canvas_resize/ (查看源代码)

Although it's a bit late to answer this question, I'd like to share what I found to solve the same question. 虽然回答这个问题有点迟,但我想分享我发现的解决同一个问题的方法。 Take it a look please. 好好看看吧。

panel.css panel.css

#Panel {
width:  100%;
height: 30%;
}

app.js app.js

var widthG = 0, height=G = 0;
function updateScale() {
    widthG  = parseInt($('#Panel').width());
    heightG = parseInt($('#anel').height());
}
...

function updateCanvas() {
    ctx = $('#canvas').get(0).getContext('2d');
    ctx.clearRect(0,0,ctx.canvas.width, ctx.canvas.height);
    ctx.canvas.width = widthG;
    ctx.canvas.width = heightG;
}

I also tried to re-assign these properties by css syntax, but it doesn't work. 我还尝试通过css语法重新分配这些属性,但它不起作用。

$('#canvas').width(panelW);
$('#canvas').height(panelH);      

Hope this helps ppl suffered from the same question. 希望这有助于ppl遭受同样的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM