-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbsr.html
189 lines (158 loc) · 5.95 KB
/
bsr.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
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
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title>Bézier Curve (de Casteljau algorithm)</title>
<style>
body { -webkit-user-select:none; font-family:georgia,serif }
small { font-size:16px; margin-left:20px }
input{ vertical-align:middle; margin-left:5px }
.canvas { position:relative; box-shadow:0 0 0 1px hsl(0, 0%, 80%); background:hsl(0, 0%, 95%); width:800px; height:600px }
canvas { position:absolute; top:0; left:0 }
.center{
width: 800px;
margin: 0 auto;
}
</style>
</head>
<body>
<h1 class="center">Bézier Curve 贝塞尔曲线<small>点击对应数目即可</small></h1>
<p class="center">控制点数目<input type=range min=2 max=7 value=4 disabled><span></span></p>
<div class="center canvas">
<canvas width=800 height=600 style=z-index:1></canvas>
<canvas width=800 height=600 style=z-index:2></canvas>
<canvas width=800 height=600 style=z-index:3></canvas>
</div>
<div class="myinfo">
<a href="http://justjavac.com/javascript/2013/08/08/asynchronous-in-javascript-3-windjs.html" target="_blank">异步和wind.js参考</a><br>
<a href="https://developer.mozilla.org/zh-CN/docs/Web/API/Canvas_API/Tutorial/Drawing_shapes#%E8%B4%9D%E5%A1%9E%E5%B0%94%EF%BC%88bezier%EF%BC%89%E4%BB%A5%E5%8F%8A%E4%BA%8C%E6%AC%A1%E8%B4%9D%E5%A1%9E%E5%B0%94" target="_blank">贝塞尔曲线参考</a>
</div>
<script src=wind-all-0.7.3.js></script>
<script>
// 获取init dom
var input = document.getElementsByTagName('input')[0]
var span = document.getElementsByTagName('span')[0]
var div = document.getElementsByTagName('div')[0]
var ctx1 = document.getElementsByTagName('canvas')[0].getContext('2d')
var ctx2 = document.getElementsByTagName('canvas')[1].getContext('2d')
var ctx3 = document.getElementsByTagName('canvas')[2].getContext('2d')
// 定义 变量
var points = [], colors = [], running = true, steps = 200, interval = 15, num;
// 设置canvas 属性
ctx1.font = '16px consolas'
ctx1.fillStyle = ctx1.strokeStyle = 'hsl(0, 0%, 50%)'
ctx1.lineWidth = ctx2.lineWidth = 2
ctx3.strokeStyle = 'hsl(0, 90%, 70%)'
// 定义三个 方法 count ;toggle ;draw.
// 获取 初始点数目
function count() {
num = parseInt(input.value)
span.innerHTML = num
}
// 开关
function toggle() {
input.disabled = running = !running
}
// 定义主进程(开始画图).
function draw(per, arr, color) {
var ary = []
var node
ctx2.strokeStyle = ctx2.fillStyle = colors[color]
// 对初始点进行处理 reduce 方法 直线
node = arr.reduce(function(previous, current, index) {
var p = {x: arr[index - 1].x + (arr[index].x - arr[index - 1].x) * per, y: arr[index - 1].y + (arr[index].y - arr[index - 1].y) * per}
if(index > 1) {
ctx2.beginPath()
ctx2.moveTo(previous.x, previous.y)
ctx2.lineTo(p.x, p.y)
ctx2.stroke()
ctx2.closePath()
}
ctx2.beginPath()
ctx2.arc(p.x, p.y, 3, 0, Math.PI * 2, true)
ctx2.fill()
ctx2.closePath()
ary.push(p)
return p
})
// 递归调用直至生成曲线点
if(ary.length > 0) {
draw(per, ary, color + 1)
} else {
ctx3.lineTo(node.x, node.y)
ctx3.stroke()
}
}
// 原生异步回调
// setInterval(function(){draw((i / steps, points, 0))}, 1000)
// 运用wind 的异步
var drawAsync = eval(Wind.compile("async", function () {
toggle() //关毕input功能
ctx3.beginPath()
ctx3.moveTo(points[0].x, points[0].y)
for(var i = 0; i <= steps; i++) {
draw(i / steps, points, 0)
$await(Wind.Async.sleep(interval))
ctx2.clearRect(0, 0, 800, 600)
}
ctx3.closePath()
points = []
toggle()//执行完毕后打开input功能
}))
// 给画板添加点击监听事件,获取初始点.取点完毕后调用异步画图
div.addEventListener('click', function(e) {
if(running) {
return
}
var point = {x: e.pageX - div.offsetLeft, y: e.pageY - div.offsetTop}
if(points.length == 0) {
ctx1.clearRect(0, 0, 800, 600)
ctx2.clearRect(0, 0, 800, 600)
ctx3.clearRect(0, 0, 800, 600)
} else {
ctx1.beginPath()
ctx1.moveTo(point.x, point.y)
ctx1.lineTo(points[points.length - 1].x, points[points.length - 1].y)
ctx1.stroke()
ctx1.closePath()
}
// 显示坐标
ctx1.beginPath()
ctx1.fillText('[' + point.x + ', ' + point.y + ']', 15, 25 * (points.length + 1))
ctx1.arc(point.x, point.y, 4, 0, Math.PI * 2, true)
ctx1.fill()
ctx1.closePath()
points.push(point)
if(points.length == num) {
drawAsync().start()
}
}, false)
// 改变input 获取 值
input.addEventListener('change', count, false)
// 载入时,初始化颜色,点数组,打开开关
window.addEventListener('load', function() {
for(var i = 0; i < parseInt(input.max); i++) {
colors[i] = 'hsl(' + 60*(i + 1) + ', 60%, 60%)'
}
count()
// 初始化开关
toggle()
}, false)
// demo http://justjavac.com/javascript/2013/08/08/asynchronous-in-javascript-3-windjs.html
// var fibo = eval(Wind.compile('async', function(){
// var a = 1, b = 1;
// console.log(a);
// $await(Wind.Async.sleep(1000));
// console.log(b);
// while (true){
// $await(Wind.Async.sleep(1000));
// var c = a + b;
// console.log(c);
// a = b;
// b = c;
// }
// }));
// fibo().start();
</script>
</body>
</html>