Last active
August 13, 2016 10:03
-
-
Save pm5/983e3aef8b21be2d04571654343ff8be to your computer and use it in GitHub Desktop.
Experimental Bézier curve control
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Approximating arcs with Bézier curves</title> | |
<script src="https://d3js.org/d3-selection.v1.min.js"></script> | |
<script src="https://d3js.org/d3-path.v1.min.js"></script> | |
<script src="https://d3js.org/d3-shape.v1.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.1/immutable.min.js"></script> | |
<style> | |
html, body { | |
padding: 0; | |
} | |
.canvas .curve { | |
stroke: black; | |
stroke-width: 2px; | |
fill: none; | |
} | |
.canvas .controlPoint, | |
.canvas .center { | |
stroke: #999; | |
stroke-width: 1px; | |
fill: white; | |
} | |
.canvas .controlPoint.focused, | |
.canvas .center.focused { | |
fill: #f4cc00; | |
} | |
.canvas .controlLine { | |
stroke: #900; | |
stroke-width: 1px; | |
fill: none; | |
} | |
.canvas .tangentLine, | |
.canvas .radiusLine { | |
stroke: #ccc; | |
stroke-width: 1px; | |
stroke-dasharray: 5 5; | |
fill: none; | |
} | |
.canvas .tip { | |
display: none; | |
stroke: #ccc; | |
stroke-width: 1px; | |
fill: white; | |
} | |
</style> | |
</head> | |
<body> | |
<svg class="canvas"></svg> | |
<script> | |
var margin = { | |
top: 0, | |
right: 0, | |
bottom: 0, | |
left: 0 | |
}; | |
var width = 960; | |
var height = 500; | |
var canvas = d3.select(".canvas") | |
.attr("width", width + margin.left + margin.right) | |
.attr("height", height + margin.top + margin.bottom); | |
var Point = Immutable.Record({ index: 0, x: 0, y: 0 }); | |
var Monad = Immutable.Record({ x: 0, y: 0, P: Point() }); | |
var controlPoints = Immutable.List.of( | |
Point({ index: 0, x: 300, y: 100 }), | |
Point({ index: 1, x: 190, y: 100 }), | |
Point({ index: 2, x: 100, y: 190 }), | |
Point({ index: 3, x: 100, y: 300 }) | |
); | |
var userState = false; | |
init(); | |
function controlLines(controlPoints) { | |
return [ | |
controlPoints.slice(0, 2), controlPoints.slice(2) | |
]; | |
} | |
function tangentLines(controlPoints) { | |
const multiply = 10 | |
const p0 = controlPoints.get(0); | |
const p1 = controlPoints.get(1); | |
const p2 = controlPoints.get(2); | |
const p3 = controlPoints.get(3); | |
return [ | |
Immutable.List.of( | |
M(p0).sub(p1).scale(multiply).add(p0).P, | |
M(p1).sub(p0).scale(multiply).add(p1).P | |
), | |
Immutable.List.of( | |
M(p2).sub(p3).scale(multiply).add(p2).P, | |
M(p3).sub(p2).scale(multiply).add(p3).P | |
), | |
]; | |
} | |
function radiusLines(controlPoints) { | |
if (!center(controlPoints)) { | |
return []; | |
} | |
return [ | |
Immutable.List.of( | |
controlPoints.get(0), | |
center(controlPoints) | |
), | |
Immutable.List.of( | |
center(controlPoints), | |
controlPoints.get(3) | |
) | |
]; | |
} | |
function tip(controlPoints) { | |
const p0 = controlPoints.get(0); | |
const p1 = controlPoints.get(1); | |
const p2 = controlPoints.get(2); | |
const p3 = controlPoints.get(3); | |
const t = -((p2.y - p3.y) * (p0.x - p3.x) - (p2.x - p3.x) * (p0.y - p3.y)) | |
/ ((p2.y - p3.y) * (p1.x - p0.x) - (p2.x - p3.x) * (p1.y - p0.y)); | |
return M(p1).sub(p0).scale(t).add(p0).P; | |
} | |
function tips(controlPoints) { | |
const t = tip(controlPoints); | |
if (!t || isNaN(t.x) || isNaN(t.y)) { | |
return []; | |
} | |
return [t]; | |
} | |
function center(controlPoints) { | |
const p0 = controlPoints.get(0); | |
const p1 = controlPoints.get(1); | |
const p2 = controlPoints.get(2); | |
const p3 = controlPoints.get(3); | |
const p0p1 = M(p1).sub(p0); | |
const p0p3 = M(p3).sub(p0); | |
const p3p2 = M(p2).sub(p3); | |
const p3p0 = M(p0).sub(p3); | |
const p0c = p0p3.sub(proj(p0p3, p0p1)); | |
const p3c = p3p0.sub(proj(p3p0, p3p2)); | |
const t = tip(controlPoints); | |
if (!t || isNaN(t.x) || isNaN(t.y)) { | |
return; | |
} | |
const c = tip(Immutable.List.of(p0, p0c.add(p0).P, p3c.add(p3).P, p3)); | |
if (dist(c, p0) !== dist(c, p3)) { | |
return; | |
} | |
return c; | |
} | |
function centers(controlPoints) { | |
c = center(controlPoints); | |
if (!c) { | |
return []; | |
} | |
return [c]; | |
} | |
function M(p) { | |
var m = Monad({ | |
x: p.x, | |
y: p.y, | |
P: Point(p) | |
}); | |
m.sub = q => M({ x: p.x - q.x, y: p.y - q.y }), | |
m.add = q => M({ x: p.x + q.x, y: p.y + q.y }), | |
m.round = () => M({ x: Math.round(p.x), y: Math.round(p.y) }), | |
m.scale = r => M({ x: p.x * r, y: p.y * r }), | |
m.length = () => Math.sqrt(p.x * p.x + p.y * p.y), | |
m.unit = () => M(p).scale(1/M(p).length()) | |
return m; | |
} | |
function length(p) { | |
return Math.sqrt(p.x * p.x + p.y * p.y); | |
} | |
function dist(p, q) { | |
return length(M(p).sub(q)); | |
} | |
function dot(p, q) { | |
return p.x * q.x + p.y * q.y; | |
} | |
function proj(v, w) { | |
return w.unit().scale(dot(v, w.unit())); | |
} | |
function mouseOver(d) { | |
d3.select(this) | |
.classed("focused", true); | |
} | |
function mouseOut(d) { | |
if (userState) return; | |
d3.select(this) | |
.classed("focused", false); | |
} | |
function mouseDownOnCenter(d) { | |
userState = "dragging"; | |
const c = center(controlPoints); | |
const t = tip(controlPoints); | |
const p0 = controlPoints.get(0); | |
const p1 = controlPoints.get(1); | |
const p2 = controlPoints.get(2); | |
const p3 = controlPoints.get(3); | |
const p0p1ratio = dist(t, p1) / dist(t, p0); | |
const p3p2ratio = dist(t, p2) / dist(t, p3); | |
canvas.on("mousemove", () => { | |
const m = Point({ | |
x: d3.mouse(this)[0], | |
y: d3.mouse(this)[1] | |
}); | |
const p0p1 = M(p1).sub(p0).unit(); | |
const p3p2 = M(p2).sub(p3).unit(); | |
const axis = p0p1.add(p3p2).scale(1/1.414); | |
const move = proj(M(m).sub(c), axis); | |
controlPoints = Immutable.List.of( | |
M(p0).add(proj(move, p0p1)).P.merge({ index: 0 }), | |
M(p1).add(proj(move, p0p1).scale(p0p1ratio)).P.merge({ index: 1 }), | |
M(p2).add(proj(move, p3p2).scale(p3p2ratio)).P.merge({ index: 2 }), | |
M(p3).add(proj(move, p3p2)).P.merge({ index: 3 }) | |
); | |
update(); | |
}); | |
} | |
function mouseDownOnControlPoint(d) { | |
userState = "dragging"; | |
const p0 = controlPoints.get(0); | |
const p1 = controlPoints.get(1); | |
const p2 = controlPoints.get(2); | |
const p3 = controlPoints.get(3); | |
canvas.on("mousemove", () => { | |
var m = Point({ | |
x: d3.mouse(this)[0], | |
y: d3.mouse(this)[1] | |
}); | |
controlPoints = controlPoints.set(d.index, m.merge({ index: d.index })); | |
if (d.index === 0) { | |
controlPoints = controlPoints.set(1, M(p1).add(m).sub(p0).round().P.merge({ index: 1 })); | |
} else if (d.index === 3) { | |
controlPoints = controlPoints.set(2, M(p2).add(m).sub(p3).round().P.merge({ index: 2 })); | |
} | |
update(); | |
}); | |
} | |
function init() { | |
canvas.on("mouseup", function () { | |
canvas.on("mousemove", null); | |
userState = false; | |
d3.select(".focused") | |
.classed("focused", false); | |
}); | |
canvas.selectAll("path.curve") | |
.data([controlPoints]) | |
.enter().append("path") | |
.classed("curve", true) | |
.attr("d", d => `M${d.get(0).x},${d.get(0).y}C${d.slice(1).map(p => p.x + ',' + p.y).join(",")}`); | |
canvas.selectAll("line.controlLine") | |
.data(controlLines(controlPoints)) | |
.enter().append("line") | |
.classed("controlLine", true) | |
.attr("x1", d => d.get(0).x) | |
.attr("y1", d => d.get(0).y) | |
.attr("x2", d => d.get(1).x) | |
.attr("y2", d => d.get(1).y); | |
canvas.selectAll("line.tangentLine") | |
.data(tangentLines(controlPoints)) | |
.enter().append("line") | |
.classed("tangentLine", true) | |
.attr("x1", d => d.get(0).x) | |
.attr("y1", d => d.get(0).y) | |
.attr("x2", d => d.get(1).x) | |
.attr("y2", d => d.get(1).y); | |
canvas.selectAll("line.radiusLine") | |
.data(radiusLines(controlPoints)) | |
.enter().append("line") | |
.classed("radiusLine", true) | |
.attr("x1", d => d.get(0).x) | |
.attr("y1", d => d.get(0).y) | |
.attr("x2", d => d.get(1).x) | |
.attr("y2", d => d.get(1).y); | |
canvas.selectAll("circle.tip") | |
.data(tips(controlPoints)) | |
.enter().append("circle") | |
.classed("tip", true) | |
.attr("cx", d => d.x) | |
.attr("cy", d => d.y) | |
.attr("r", 4); | |
canvas.selectAll("circle.center") | |
.data(centers(controlPoints)) | |
.enter().append("circle") | |
.classed("center", true) | |
.attr("cx", d => d.x) | |
.attr("cy", d => d.y) | |
.attr("r", 4) | |
.on("mousedown", mouseDownOnCenter) | |
.on("mouseover", mouseOver) | |
.on("mouseout", mouseOut); | |
canvas.selectAll("circle.controlPoint") | |
.data(controlPoints.toArray(), d => d.index) | |
.enter().append("circle") | |
.classed("controlPoint", true) | |
.attr("cx", d => d.x) | |
.attr("cy", d => d.y) | |
.attr("r", 4) | |
.on("mousedown", mouseDownOnControlPoint) | |
.on("mouseover", mouseOver) | |
.on("mouseout", mouseOut); | |
} | |
function update() { | |
canvas.selectAll("path.curve") | |
.data([controlPoints]) | |
.attr("d", d => `M${d.get(0).x},${d.get(0).y}C${d.slice(1).map(p => p.x + ',' + p.y).join(",")}`); | |
canvas.selectAll("line.controlLine") | |
.data(controlLines(controlPoints)) | |
.attr("x1", d => d.get(0).x) | |
.attr("y1", d => d.get(0).y) | |
.attr("x2", d => d.get(1).x) | |
.attr("y2", d => d.get(1).y); | |
canvas.selectAll("line.tangentLine") | |
.data(tangentLines(controlPoints)) | |
.attr("x1", d => d.get(0).x) | |
.attr("y1", d => d.get(0).y) | |
.attr("x2", d => d.get(1).x) | |
.attr("y2", d => d.get(1).y); | |
radiusLineElements = canvas.selectAll("line.radiusLine") | |
.data(radiusLines(controlPoints)) | |
.attr("x1", d => d.get(0).x) | |
.attr("y1", d => d.get(0).y) | |
.attr("x2", d => d.get(1).x) | |
.attr("y2", d => d.get(1).y); | |
radiusLineElements.enter().append("line") | |
.classed("radiusLine", true) | |
.attr("x1", d => d.get(0).x) | |
.attr("y1", d => d.get(0).y) | |
.attr("x2", d => d.get(1).x) | |
.attr("y2", d => d.get(1).y); | |
radiusLineElements.exit() | |
.remove(); | |
canvas.selectAll("circle.tip") | |
.data(tips(controlPoints)) | |
.attr("cx", d => d.x) | |
.attr("cy", d => d.y); | |
centerElements = canvas.selectAll("circle.center") | |
.data(centers(controlPoints)) | |
.attr("cx", d => d.x) | |
.attr("cy", d => d.y); | |
centerElements.enter().append("circle") | |
.classed("center", true) | |
.attr("cx", d => d.x) | |
.attr("cy", d => d.y) | |
.attr("r", 4) | |
.on("mousedown", mouseDownOnCenter) | |
.on("mouseover", mouseOver) | |
.on("mouseout", mouseOut); | |
centerElements.exit() | |
.remove(); | |
canvas.selectAll("circle.controlPoint") | |
.data(controlPoints.toArray(), d => d.index) | |
.attr("cx", d => d.x) | |
.attr("cy", d => d.y); | |
} | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The MIT License (MIT) | |
Copyright © 2016 Pomin Wu | |
Permission is hereby granted, free of charge, to any person obtaining a copy of | |
this software and associated documentation files (the “Software”), to deal in | |
the Software without restriction, including without limitation the rights to | |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | |
of the Software, and to permit persons to whom the Software is furnished to do | |
so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
Fork this project to create your own MIT license that you can always link to. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment