我對HTML5畫布上的座標系感到困惑。我已經創建了一個500x500的畫布和as per this answer我試圖以更易理解的笛卡爾格式來定位它。但是,我的橙色廣場出現了意想不到的行爲。在HTML5畫布上定位
在畫布500,500的情況下,我期望fillRect(-250,-250,100,100)
在畫布的左上角有一個左上角,但是卻沒有找到。出於某種原因,fillRect(150,150,100,100)
在畫布的右上角有一個右上角。
我毫無疑問困惑,有人可以闡明HTML5畫布的動態?
drawanimation.html:
<!DOCTYPE html>
<html>
<head>
<title>Animate Something!</title>
<link rel='stylesheet' href='drawanimation.css'></link>
<script src='drawanimation.js'></script>
</head>
<body onload = "draw();">
<canvas id="drawer" width="500" height="500"></canvas>
</body>
</html>
drawanimation.js
var width = 500;
var height = 500;
var draw = function(){
var canvas = document.getElementById('drawer');
var context = canvas.getContext('2d');
context.save();
context.clearRect(0,0,width,height);
context.translate(width/2,height/2); //cartesian attempt
//context.rotate(-Math.PI/2); //spin this thing until it's oriented right!
context.fillStyle = 'orange';
context.fillRect(150, 150 ,100,100);
context.restore();
};