-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest-canvas.html
More file actions
190 lines (151 loc) · 3.96 KB
/
test-canvas.html
File metadata and controls
190 lines (151 loc) · 3.96 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
#div1 {
width: 400px;
height: 400px;
}
</style>
</head>
<body>
<div id="div1">
<canvas width="800" height="800" id="cs">你的浏览器不支持canvas!</canvas>
</div>
<script type="text/javascript">
var oCanvas = document.getElementById('cs');
if (oCanvas.getContext) {
var ctx = oCanvas.getContext('2d'),
l = 0,
w = oCanvas.width,
half = w / 2,
span = 5; //每条虚线的实线部分长度为5
ctx.strokeStyle = '#FF0000';
ctx.lineWidth = 2;
ctx.strokeRect(1, 1, w - 2, w - 2);
//依次调用看效果!
//drawReal(ctx);
//drawUnReal(ctx);
//drawUnRealNB(ctx);
drawSimpleUnreal(ctx);
}
//画实线
function drawReal(ctx) {
ctx.save();
ctx.lineWidth = 1;
ctx.beginPath();
//横线
ctx.moveTo(0, half);
ctx.lineTo(w, half);
//竖线
ctx.moveTo(half, 0);
ctx.lineTo(half, w);
//交叉线1
ctx.moveTo(0, 0);
ctx.lineTo(w, w);
//交叉线2
ctx.moveTo(0, w);
ctx.lineTo(w, 0);
ctx.stroke();
ctx.closePath();
ctx.restore();
}
//画虚线,兼容性没问题!
function drawUnReal(ctx) {
ctx.save();
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(0, half);
l = w / 5; //横线路径
for (var i = 0; i < l; i += 2) {
ctx.lineTo(i * span, half);
ctx.moveTo((i + 1) * span, half);
}
//竖线路径
ctx.moveTo(half, 0);
for (var i = 0; i < l; i += 2) {
ctx.lineTo(half, i * span);
ctx.moveTo(half, (i + 1) * span);
}
ctx.translate(half, half);
ctx.rotate(45 * Math.PI / 180);
ctx.moveTo(0, -half * Math.SQRT2);
l = Math.ceil(w * Math.SQRT2 / 5);
//交叉线1
for (var i = 0; i < l; i += 2) {
ctx.lineTo(0, -half * Math.SQRT2 + i * span);
ctx.moveTo(0, -half * Math.SQRT2 + (i + 1) * span);
}
ctx.rotate(90 * Math.PI / 180);
ctx.moveTo(0, -half * Math.SQRT2);
//交叉线2
for (var i = 0; i < l; i += 2) {
ctx.lineTo(0, -half * Math.SQRT2 + i * span);
ctx.moveTo(0, -half * Math.SQRT2 + (i + 1) * span);
}
//统一描边节省性能!
ctx.stroke();
ctx.restore();
}
//画虚线——掏窟窿作法
function drawUnRealNB(ctx) {
drawReal(ctx);
//画窟窿
ctx.save();
ctx.lineWidth = 1;
ctx.beginPath();
l = w / 5; //横线路径
for (var i = 1; i < l; i += 2) {
ctx.rect(i*span,half-2,span,3);
}
//竖线路径
for (var i = 1; i < l; i += 2) {
ctx.rect(half-2,i*span,3,span);
}
ctx.translate(half, half);
ctx.rotate(45 * Math.PI / 180);
l = Math.ceil(w * Math.SQRT2 / 5);
//交叉线1
for (var i = 1; i < l; i += 2) {
ctx.rect(-2,-half * Math.SQRT2+i*span,3,span);
}
ctx.rotate(90 * Math.PI / 180);
//交叉线2
for (var i = 1; i < l; i += 2) {
ctx.rect(-2,-half * Math.SQRT2+i*span,3,span);
}
ctx.restore();
//进行掏窟窿
ctx.clip();
ctx.clearRect(0,0,w,w);
}
//画虚线简单做法,有兼容性问题!
function drawSimpleUnreal(ctx){
ctx.save();
ctx.lineWidth = 1;
ctx.beginPath();
//画布增强功能(注意兼容性,嵌入到ios safari4.3.3有问题!)http://msdn.microsoft.com/zh-cn/library/ie/dn265037(v=vs.85).aspx
//IE11+/safari7+
//ctx.setLineDash([12,3,3,3]); //创建12像素长,间隔为3像素 + 3像素长,间隔为3像素的点划线
ctx.setLineDash([5,5]); //创建5像素长,间隔为5像素的虚线
//横线
ctx.moveTo(0, half);
ctx.lineTo(w, half);
//竖线
ctx.moveTo(half, 0);
ctx.lineTo(half, w);
//交叉线1
ctx.moveTo(0, 0);
ctx.lineTo(w, w);
//交叉线2
ctx.moveTo(0, w);
ctx.lineTo(w, 0);
ctx.stroke();
ctx.closePath();
ctx.restore();
}
</script>
</body>
</html>