我遇到了HTML5 Canvas标记的问题,并多次调整缩放比例。在缩放两次之后,即使我正在调整缩放级别,画布也只使用可用画布高度的一部分,宽度为&。多画布缩放行为不正常
<html>
<head>
<script>
var ctx;
var nScale = 1.00;
function pageLoad() {
ctx=document.getElementById('cnvUni').getContext('2d');
// canvas on page load is 500x500
drawGrid(); // 5 boxes across & 5 down
zoom(0.5); // canvas should be now zoomed out to 1000x1000
drawGrid(); // 10 boxes across & 10 down
zoom(0.5); // effective zoom is now 0.25 = 2000x2000
drawGrid(); // should be 20 boxes across & 20 down
// NOTE: At this point, the grid is drawing boxes @ 20x20 but only using 1/4 of the
// canvas size.
}
function zoom(nZoomLevel) {
nScale = nZoomLevel * nScale
ctx.scale(nScale,nScale);
}
function drawGrid() {
var nWidth, nHeight;
nWidth = Math.floor(ctx.canvas.width/nScale);
nHeight = Math.floor(ctx.canvas.height/nScale);
var nGridSize = 100;
var nGridY = 0;
var nGridX = 0;
// sets a random colour each time grid is drawn.
ctx.strokeStyle = 'rgb(' + Math.floor(Math.random()*256) + ',' + Math.floor(Math.random()*256) + ',' + Math.floor(Math.random()*256) + ')';
for (nGridY=0;nGridY < nHeight;nGridY+=nGridSize) {
for (nGridX=0;nGridX < nWidth;nGridX+=nGridSize) {
// draw the box;
ctx.strokeRect(nGridX, nGridY, nGridSize, nGridSize);
}
}
}
</script>
</head>
<body onload="pageLoad();">
<canvas id="cnvUni" width="500" height="500">
Canvas doesn't work.
</canvas>
</body>
</html>
如果我是通过2绘制网格最后一次它会绘制出整个画布大小时,乘高&宽度,但我想不出为什么这样做是必要的。
我想知道的是:
有没有一种方法来查询画布背景下找出的刻度值(或计算出的高度/宽度)是什么?还是我正确地处理这个问题并且自己跟踪价值?
如果是这样,那么我认为它必须是我的数学,这是搞砸了这一点;我无法指出它。我相信我只是太接近这个问题,并没有看到这个问题。另一组眼睛会有所帮助。
任何意见,将不胜感激。
谢谢!
嗨Kieranmaine - 感谢您的评论;我认为,尽管我正在寻找一种方法来计算比例尺/高度/宽度而不使用保存/恢复功能。这更多的是试图找出公式,而不是得到结果。 – michaely 2010-07-09 00:47:57
我已经找到了问题;我的回答如下。谢谢! – michaely 2010-07-09 18:01:01