我对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();
};