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(“plea press <esc> 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.clopath();
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; i<4; i++) {
var 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. plea install a html5 compatible browr.”);
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(“plea press <esc> 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.clopath();
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; i<4; i++) {
var x = (i+1)*20;
var 科学计数法y = (i+1)*60;
var newx = y;
var newy = -x;
context.fillrect(newx, newy, 200, 6);
}
}
function sa2020高考录取分数线公布veandrestorecontext(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);
}
本文发布于:2023-04-06 17:19:51,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/f26bc5447011f0dcc35ec48a76b483db.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:HTML5 Canvas实现平移/放缩/旋转deom示例(附截图).doc
本文 PDF 下载地址:HTML5 Canvas实现平移/放缩/旋转deom示例(附截图).pdf
留言与评论(共有 0 条评论) |