forked from jlgw/julia-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bezier.jl
37 lines (26 loc) · 1005 Bytes
/
bezier.jl
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
const nctrlpts = 7
const maxdeg = 60
include("curves.jl")
const curvestyle = Dict(:stroke=>"red", :fill=>"none", :strokeWidth=>"2")
bernstein_(v, n) = binomial(n,v)*Poly([0.,1.])^v*Poly([1.,-1.])^(n-v)
const bernrec = (n->bernstein_.(0:n, n)).(0:maxdeg)
bernstein(v,n) = bernrec[n+1][v+1]
bezier(ctrl) = sum([ctrl[i+1,:] .* bernstein(i,size(ctrl)[1]-1) for i in 0:size(ctrl)[1]-1])
poly = bezier(ctrlpts)
bcurve = curve(poly)
line = dom"svg:polyline"(attributes=Dict("id"=>"line", "points"=>coordsvgformat(bcurve)), style=curvestyle)
addstatic!(canvas, line)
addmovable!.(canvas, pts)
function update(c)
rawpts = getpts(c)
coordsvgformat(curve(bezier(rawpts)))
end
for i in 1:nctrlpts
push!(canvas["pt-$i"].listeners,
(x) -> evaljs(canvas.w, js""" (function() {
document.getElementById("line").setAttribute("points",
$(update(canvas)))})()"""
)
)
end
webio_serve(page("/", req->canvas()))