-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.变换.html
101 lines (89 loc) · 3.16 KB
/
5.变换.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<style type="text/css">
canvas{
border:1px solid black;
}
#kno{
width: 500px;
height: auto;
float: left;
font-size: 18px;
}
</style>
<script type="text/javascript">
window.onload=function(){
//获取元素
var canvas01=document.getElementById("canvas01");
var canvas02=document.getElementById("canvas02");
var canvas03=document.getElementById("canvas03");
//确定浏览器支持<canvas>元素
if(canvas01.getContext){
//一.学习rotate和translate
//取得上2d下文
var context=canvas01.getContext("2d");
//开始路径
context.beginPath();
//绘制外圆
context.arc(100,100,99,0,2*Math.PI,false);
//绘制内圆
context.moveTo(194,100);
context.arc(100,100,94,0,2*Math.PI,false);
//变换原点
context.translate(100,100);
//旋转表针
context.rotate(1);
//绘制分针
context.moveTo(0,0);
context.lineTo(0,-85);
//绘制时针
context.moveTo(0,0);
context.lineTo(-65,0);
//描边路径
context.stroke();
//二.学习save和restore
//重新取得上2d下文
context=canvas02.getContext("2d");
//设置1:颜色为红,并保存到栈
context.fillStyle="#ff0000";
context.save();
//设置:2:颜色为绿,转换中心点,并保存到栈
context.fillStyle="#00ff00";
context.translate(100,100);
context.save();
//设置3:颜色为蓝,从点(100,100)开始绘制蓝色矩形
context.fillStyle="#0000ff";
context.fillRect(0,0,100,100);
//恢复到设置2,从点(100,100)开始绘制绿色矩形,此时坐标中心还是(100,100)
context.restore();
context.fillRect(0,0,50,50);
//恢复到设置1,从(0,0)开始绘制红色矩形,此时坐标中心是(0,0)
context.restore();
context.fillRect(0,0,100,100);
}
}
</script>
<body>
<!-- canvas元素-->
<canvas id="canvas01" width="200px" height="200px"></canvas>
<canvas id="canvas02" width="200px" height="200px"></canvas>
<canvas id="canvas03" width="200px" height="200px"></canvas>
<!-- 知识点和问题汇总 -->
<div id="kno">
<b>知识点:</b> <br/>
<strong>1.</strong>rotate(angle):围绕原点旋转图像angle弧度 <br/>
<strong>2.</strong>scale(sx,sy):缩放,x乘以sx,y乘以sy <br/>
<strong>3.</strong>translate(x,y):坐标中心移到(x,y) <br/>
<strong>4.</strong>关于变换矩阵的方法:transform,setTransform(暂时不知道怎么玩) <br/>
<strong>5.</strong>save和restore,相当于历史记录功能,save保存的是上下文的设置和变换 <br/>
<br/>
<br/>
<b>问题:</b> <br/>
transform和setTransform如何使用?
</div>
</body>
</html>