HTML5 Canvas实现平移/放缩/旋转deom示例(附截图)


Posted in HTML / CSS onJuly 04, 2013

HTML5 Canvas中提供了实现图形平移,旋转,放缩的API。
平移(translate)
平移坐标translate(x, y)意思是把(0,0)坐标平移到(x, y),原来的(0,0)坐标则变成(-x, -y)
图示如下:
HTML5 Canvas实现平移/放缩/旋转deom示例(附截图) 
任何原来的坐标点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 <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大小。效果如图示:
HTML5 Canvas实现平移/放缩/旋转deom示例(附截图) 
复制代码
代码如下:

// 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)
HTML5 Canvas实现平移/放缩/旋转deom示例(附截图) 
旋转前的坐标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. 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 <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.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; 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 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);
}
HTML / CSS 相关文章推荐
什么是CSS3 HSLA色彩模式?HSLA模拟渐变色条
Apr 26 HTML / CSS
CSS3 3D旋转rotate效果实例介绍
May 03 HTML / CSS
css3 实现元素弧线运动的示例代码
Apr 24 HTML / CSS
详解HTML5新增标签
Nov 27 HTML / CSS
HTML4和HTML5之间除了相似以外的10个主要不同
Dec 13 HTML / CSS
html5 Canvas画图教程(10)—把面拆成线条模拟出圆角矩形
Jan 09 HTML / CSS
HTML5之SVG 2D入门12—SVG DOM及DOM操作介绍
Jan 30 HTML / CSS
html5 利用重力感应实现摇一摇换颜色可用来做抽奖等等
May 07 HTML / CSS
html5本地存储之localstorage 、本地数据库、sessionStorage简单使用示例
May 08 HTML / CSS
处理HTML5新标签的浏览器兼容版问题
Mar 13 HTML / CSS
移动端Html5中百度地图的点击事件
Jan 31 HTML / CSS
HTML+css盒子模型案例(圆,半圆等)“border-radius” 简单易上手
May 10 HTML / CSS
HTML5 Canvas鼠标与键盘事件demo示例
Jul 04 #HTML / CSS
HTML5 Canvas的性能提高技巧经验分享
Jul 02 #HTML / CSS
html5 CSS过度-webkit-transition使用介绍
Jul 02 #HTML / CSS
仿CSDN Blog返回页面顶部功能实现原理及代码
Jun 30 #HTML / CSS
Html5实现iPhone开机界面示例代码
Jun 30 #HTML / CSS
html5 input属性使用示例
Jun 28 #HTML / CSS
HTML中使用SVG与SVG预定义形状元素介绍
Jun 28 #HTML / CSS
You might like
用php和MySql来与ODBC数据连接
2006/10/09 PHP
在PHP里得到前天和昨天的日期的代码
2007/08/16 PHP
PHP 简易输出CSV表格文件的方法详解
2013/06/20 PHP
php实现两个数组相加的方法
2015/02/17 PHP
yii2局部关闭(开启)csrf的验证的实例代码
2017/07/10 PHP
jQuery新闻滚动插件 jquery.roller.js
2011/06/27 Javascript
jquery parent和parents的区别分析
2013/10/02 Javascript
js实现鼠标经过时图片滚动停止的方法
2015/02/16 Javascript
微信小程序 wxapp内容组件 progress详细介绍
2016/10/31 Javascript
JS实现复制内容到剪贴板功能
2017/02/05 Javascript
vue2.0与bootstrap3实现列表分页效果
2017/11/28 Javascript
Vue仿支付宝支付功能
2018/05/25 Javascript
如何根据业务封装自己的功能组件
2019/04/19 Javascript
封装微信小程序http拦截器过程解析
2019/08/13 Javascript
Node.js API详解之 string_decoder用法实例分析
2020/04/29 Javascript
swiperjs实现导航与tab页的联动
2020/12/13 Javascript
使用python开发vim插件及心得分享
2014/11/04 Python
Python中使用装饰器和元编程实现结构体类实例
2015/01/28 Python
Python3中多线程编程的队列运作示例
2015/04/16 Python
pytorch + visdom 处理简单分类问题的示例
2018/06/04 Python
解决pycharm 误删掉项目文件的处理方法
2018/10/22 Python
python单向链表的基本实现与使用方法【定义、遍历、添加、删除、查找等】
2019/10/24 Python
如何给Python代码进行加密
2020/01/10 Python
python对数组进行排序,并输出排序后对应的索引值方式
2020/02/28 Python
Python2.6版本pip安装步骤解析
2020/08/17 Python
移动端html5 meta标签的神奇功效
2016/01/06 HTML / CSS
世界上最好的精品店:Shoptiques
2018/02/05 全球购物
什么是类的返射机制
2016/02/06 面试题
学校门卫工作职责
2013/12/07 职场文书
开学季活动策划方案
2014/02/28 职场文书
聚美优品的广告词
2014/03/14 职场文书
环保建议书100字
2014/05/14 职场文书
毕业生自荐材料范文
2014/12/30 职场文书
500字作文之周记
2019/12/13 职场文书
win11高清晰音频管理器在哪里?win11找不到高清晰音频管理器解决办法
2022/04/08 数码科技
Go语言grpc和protobuf
2022/04/13 Golang