惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
K
Kaspersky official blog
Forbes - Security
Forbes - Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Proofpoint News Feed
G
GRAHAM CLULEY
V
Vulnerabilities – Threatpost
Security Latest
Security Latest
Scott Helme
Scott Helme
S
Securelist
美团技术团队
T
Threat Research - Cisco Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
SegmentFault 最新的问题
W
WeLiveSecurity
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Apple Machine Learning Research
Apple Machine Learning Research
The Cloudflare Blog
AI
AI
L
Lohrmann on Cybersecurity
S
Security Affairs
Cloudbric
Cloudbric
SecWiki News
SecWiki News
爱范儿
爱范儿
雷峰网
雷峰网
Engineering at Meta
Engineering at Meta
C
Cyber Attacks, Cyber Crime and Cyber Security
大猫的无限游戏
大猫的无限游戏
N
News and Events Feed by Topic
I
InfoQ
S
Secure Thoughts
AWS News Blog
AWS News Blog
A
About on SuperTechFans
Schneier on Security
Schneier on Security
酷 壳 – CoolShell
酷 壳 – CoolShell
The Last Watchdog
The Last Watchdog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Check Point Blog
P
Palo Alto Networks Blog
博客园 - 【当耐特】
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Google DeepMind News
Google DeepMind News
Latest news
Latest news
I
Intezer
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
V
V2EX
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LangChain Blog
D
Docker

博客园 - bigwhiteshark(云飞扬)

linux下svn定时更新项目 PHP合成图片、生成文字、居中对齐、画线、矩形、三角形、多边形、图片抗锯齿、不失真 高性能源码示例 uv纹理坐标设定与贴图规则 容易答错的JS笔试题 JavaScript严谨模式(Strict Mode) property和attribute的区别 深入理解javascript 中的 delete(转) javascript中的call()和apply()方法的使用 Javascript 异步加载详解(转) Javascript框架的自定义事件(转) array2json() - Convert PHP arrays to JSON 一些Vim使用的小技巧 virtualbox centos安装增强工具和Centos与VirtualBox共享文件夹设置 yum搭建lnmp环境(CentOS6.4) CentOS中文man安装配置 在Linux里设置环境变量的方法(export PATH) 更新CentOS防火墙设置开启80端口访问 centos 6.4 server 安装nginx Centos安装vim
Html5 Canvas transform setTransform
bigwhiteshark(云飞扬) · 2014-11-09 · via 博客园 - bigwhiteshark(云飞扬)

Html5 Canvas transform就是矩阵变换,一种坐标的变形。

坐标变形的三种方式,平移translate, 缩放scale以及旋转rotate都可以通过transform做到。

transform(m11, m12, m21, m22, dx, dy):这个方法必须将当前的变形矩阵乘上下面的矩阵:

m11

m21

dx

m12

m22

dy

0

0

1

也就是说假设 变化前A(x,y)得到 变换后B(x’,y’)可以通过乘以上述矩阵即可得到:

比如说:缩放scale

x”=x*a;            //x放大a倍

y”=y*b;            //y放大b倍

只需transform(a, 0, 0, b, 0, 0);

A(x,y)通过transform就得到了放大后的B(x’,y’);与方法context.scale(a,b)同效;

比如 旋转rotate

假设将(x,y)绕原点逆时针旋转θ得到(x”,y”),则:

x’=x*cosθ-y*sinθ

y’=x*sinθ y*cosθ

只需transform(Math.cos(θ*Math.PI/180),Math.sin(θ*Math.PI/180),-Math.sin(θ*Math.PI/180),Math.cos(θ*Math.PI/180),0,0)

A(x,y)通过transform就得到了旋转后的B(x’,y’);与方法context.rotate(θ)同效;

就是说只要知道变化前和变换后的坐标转化公式,就能通过与矩阵相乘的方法得到;

setTransform这个方法重置当前的变形矩阵为单位矩阵,然后以相同的参数调用 transform 方法。就是消除前面transform行为对这次行为的影响;

提供一个代码可以自己研究一下

<!DOCTYPE HTML>
<html>
<head>
<script type=”text/javascript”>
function drawShape(){
// get the canvas element using the DOM
var canvas = document.getElementById(“mycanvas”);
// Make sure we don”t execute when canvas isn”t supported
if (canvas.getContext){
// use getContext to use the canvas for australian casinos online drawing
var ctx = canvas.getContext(“2d”);
var sin = Math.sin(Math.PI/6);
var cos = Math.cos(Math.PI/6);
ctx.translate(200, 200);
var c = 0;
for (var ratedbet.co.uk i=0; i <= 12; i ) {
c = Math.floor(255 / 12 * i);
ctx.fillStyle = “rgb(” c “,” c “,” c “)”;
ctx.fillRect(0, 0, 100, 100);
ctx.transform(cos, sin, -sin, cos, 0,0);
}
ctx.setTransform(-1, 0, 0, 1, 200, 200);
ctx.fillStyle = “rgba(100, 100, 255, 0.5)”;
ctx.fillRect(50, 50, 100, 100);
}else {
alert(“You need Safari or Firefox 1.5 to see this demo.”);
}
}
</script>
</head>
<body onload=”drawShape();”>
<canvas id=”mycanvas” width=”400″ height=”400″></canvas>
</body>
</html>