html5 canvas中提供了实现图形平移,旋转,放缩的api。
平移(translate)
平移坐标translate(x, y)意思是把(0,0)坐标平移到(x, y),原来的(0,0)坐标则变成(-x, -y)
图示如下:
任何原来的坐标点p(ox, oy)在translate之后的坐标点为p(ox-x, oy-y),其中点(x, y)为平移
点坐标translate(x, y)。
代码演示:
复制代码
代码如下:
// translate is move the startpoint to centera and back to top left corner
function rendertext(width, height, context) {
context.translate(width/ 2, height / 2); // 中心点坐标为(0, 0)
context.font=18px arial;
context.fillstyle=blue;
context.filltext(please press to exit game,5,50);
context.translate(-width/2, -height/2); // 平移恢复(0,0)坐标为左上角
context.filltext(i'm back to top,5,50);
}
放缩(scale)
scale(a, b)意思是将对象沿着xy轴分别放缩至a*x, b*y大小。效果如图示:
复制代码
代码如下:
// translation the rectangle.
function drawpath(context) {
context.translate(200,200);
context.scale(2,2);// scale twice size of original shape
context.strokestyle= green;
context.beginpath();
context.moveto(0,40);
context.lineto(80,40);
context.lineto(40,80);
context.closepath();
context.stroke();
}
旋转(rotate)
旋转角度rotate(math.pi/8)
旋转前的坐标p(x, y)在对应的旋转后的坐标p(rx, ry)为
rx = x * cos(-angle) - y * sin(-angle);
ry = y * cos(-angle) + x * sin(-angle);
旋转90度可以简化为:
rx = y;
ry = -x;
canvas中旋转默认的方向为顺时针方向。演示代码如下:
复制代码
代码如下:
// new point.x = x * cos(-angle) -y * sin(-angle),
// new point.y = y * cos(-angle) +x * sin(-angle)
function renderrotatetext(context) {
context.font=24px arial;
context.fillstyle=red;
context.filltext(i'm here!!!,5,50);
// rotate -90 degreee
// context.rotate(-math.pi/2);
// context.fillstyle=blue;
// context.filltext(i'm here!!!, -400,30);
// rotate 90 degreee
context.rotate(math.pi/2);
context.fillstyle=blue;
context.filltext(i'm here!!!,350,-420);
console.log(math.sin(math.pi/2));
// rotae 90 degree and draw 10 lines
context.fillstyle=green;
for(var i=0; ivar x = (i+1)*20;
var y = (i+1)*60;
var newx = y;
var newy = -x;
context.fillrect(newx,newy, 200, 6);
}
}
通常做法是旋转与平移一起使用,先将坐标(0,0)平移到中心位置
translate (width/2, height/2)然后再使用rotate(math.pi/2)完成旋转
代码示例如下:
复制代码
代码如下:
function saveandrestorecontext(context) {
context.save();
context.translate(200,200);
context.rotate(math.pi/2);
context.fillstyle=black;
context.filltext(2d context rotate and translate, 10, 10);
context.restore();
context.filltext(2d context rotate and translate, 10, 10);
}
全部javascript代码:
复制代码
代码如下:
var tempcontext = null; // global variable 2d context
window.onload = function() {
var canvas = document.getelementbyid(target);
canvas.width = 450;
canvas.height = 450;
if (!canvas.getcontext) {
console.log(canvas not supported. please install a html5 compatible browser.);
return;
}
// get 2d context of canvas and draw image
tempcontext = canvas.getcontext(2d);
// rendertext(canvas.width, canvas.height, tempcontext);
saveandrestorecontext(tempcontext);
// drawpath(tempcontext);
}
// translate is move the start point to centera and back to top left corner
function rendertext(width, height, context) {
context.translate(width / 2, height / 2);
context.font=18px arial;
context.fillstyle=blue;
context.filltext(please press to exit game,5,50);
context.translate(-width / 2, -height / 2);
context.filltext(i'm back to top,5,50);
}
// translation the rectangle.
function drawpath(context) {
context.translate(200, 200);
context.scale(2,2); // scale twice size of original shape
context.strokestyle = green;
context.beginpath();
context.moveto(0, 40);
context.lineto(80, 40);
context.lineto(40, 80);
context.closepath();
context.stroke();
}
// new point.x = x * cos(-angle) - y * sin(-angle),
// new point.y = y * cos(-angle) + x * sin(-angle)
function renderrotatetext(context) {
context.font=24px arial;
context.fillstyle=red;
context.filltext(i'm here!!!,5,50);
// rotate -90 degreee
// context.rotate(-math.pi/2);
// context.fillstyle=blue;
// context.filltext(i'm here!!!, -400,30);
// rotate 90 degreee
context.rotate(math.pi/2);
context.fillstyle=blue;
context.filltext(i'm here!!!, 350,-420);
console.log(math.sin(math.pi/2));
// rotae 90 degree and draw 10 lines
context.fillstyle=green;
for(var i=0; ivar x = (i+1)*20;
var y = (i+1)*60;
var newx = y;
var newy = -x;
context.fillrect(newx, newy, 200, 6);
}
}
function saveandrestorecontext(context) {
context.save();
context.translate(200,200);
context.rotate(math.pi/2);
context.fillstyle=black;
context.filltext(2d context rotate and translate, 10, 10);
context.restore();
context.filltext(2d context rotate and translate, 10, 10);
}