-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8.渐变.html
102 lines (90 loc) · 3.11 KB
/
8.渐变.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
102
<!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){
//情景1:普通模式
//取得上2d下文
var context=canvas01.getContext("2d");
//创建新的线性渐变,createLinearGradient(起点x坐标,起点y坐标,终点x坐标,终点x坐标)
var gradient=context.createLinearGradient(50,50,150,150);
//设置开始的颜色
gradient.addColorStop(0,"#fff");
//设置结束的颜色
gradient.addColorStop(1,"#000");
//绘制红色的矩形
context.fillStyle="red";
context.fillRect(10,10,100,100);
//绘制渐变矩形
context.fillStyle=gradient;
context.fillRect(50,50,100,100);
//情景2:升级版(解决对不准渐变目标坐标的问题)
//重新取得上2d下文
context=canvas02.getContext("2d");
//创建一个可以对准坐标的函数
function createRectLinearGradient(context,x,y,width,height){
return context.createLinearGradient(x,y,x+width,y+height);
}
//另一种方式创建 gradient
var gradient=createRectLinearGradient(context,50,50,100,100);
//设置开始的颜色
gradient.addColorStop(0,"#fff");
//设置结束的颜色
gradient.addColorStop(1,"#000");
//绘制红色的矩形
context.fillStyle="red";
context.fillRect(10,10,100,100);
//绘制渐变矩形
context.fillStyle=gradient;
context.fillRect(50,50,100,100);
//情景3:径向渐变:createRadialGradient()
//重新取得上2d下文
context=canvas03.getContext("2d");
//创建新的径向渐变,createRadialGradient(起点圆x坐标,起点圆y坐标,起点圆r半径,终点圆x坐标,终点圆y坐标,终点圆r半径)
var gradient=context.createRadialGradient(100,100,30,100,100,50);
//设置开始的颜色
gradient.addColorStop(0,"#fff");
//设置结束的颜色
gradient.addColorStop(1,"#000");
//绘制红色的矩形
context.fillStyle="red";
context.fillRect(10,10,100,100);
//绘制渐变矩形
context.fillStyle=gradient;
context.fillRect(50,50,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>线性渐变:createLinearGradient(起点x坐标,起点y坐标,终点x坐标,终点x坐标)<br/>
<strong>2.</strong>径向渐变:createRadialGradient(起点圆x坐标,起点圆y坐标,起点圆r半径,终点圆x坐标,终点圆y坐标,终点圆r半径)<br/>
</div>
</body>
</html>