Skip to content

Instantly share code, notes, and snippets.

@enjalot
Last active August 29, 2015 14:25
Show Gist options
  • Save enjalot/1aacdf74119f7e0c502b to your computer and use it in GitHub Desktop.
Save enjalot/1aacdf74119f7e0c502b to your computer and use it in GitHub Desktop.
phat path draw mod
{"description":"phat path draw mod","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"style.css":{"default":true,"vim":false,"emacs":false,"fontSize":12},"points.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"pingpong","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"ajax-caching":true,"thumbnail":"http://i.imgur.com/P5CwNyd.png","inline-console":false,"controls":{"Amplitude Top":100,"tang":100,"tangent":52,"tangent at":50,"tangent at ":50,"tangent at percent":59}}
var height = 150;
var cm = tributary.getCodeEditor("points.json");
var points = JSON.parse(cm.getValue())
// the amount to phatten the path by
var vectorScale = 30;
// the number of samples
var numSamples = 142
var pathWidth = 21;
var sampleWidth = 24;
var circleRadius = 18;
var line = d3.svg.line()
.x(function(d) { return d.x })
.y(function(d) { return d.y })
.interpolate("cardinal")
//.interpolate("basis")
var svg = d3.select("svg")
var path = svg.append("path")
.style({
fill: "none",
stroke: "none",
"stroke-width": pathWidth
})
var drag = d3.behavior.drag()
.on("drag", function(d) {
d.x = d3.mouse(this)[0];
d.y = d3.mouse(this)[1];
draw()
})
.on("dragend", function() {
save();
})
svg.on("dblclick", function() {
points.push({x: d3.mouse(this)[0], y: d3.mouse(this)[1] });
draw();
save();
})
function draw() {
path.attr("d", line(points));
var node = path.node()
var samples = getSamples(node, numSamples);
var slines = svg.selectAll("line.perp")
.data(samples);
slines.exit().remove();
slines.enter()
.append("line").classed("perp", true)
slines
.attr({
x1: function(d,i) { return d.point.x - d.perp.x * scale(i) },
y1: function(d,i) { return d.point.y - d.perp.y * scale(i) },
x2: function(d,i) { return d.point.x + d.perp.x * scale(i) },
y2: function(d,i) { return d.point.y + d.perp.y * scale(i) },
//stroke: "#5f55e1",
stroke: "#000000",
"stroke-width": sampleWidth,
//"stroke-opacity": 0.5,
//"stroke-linecap": "round"
})
var circles = svg.selectAll("circle")
.data(points);
circles.exit().remove();
circles
.enter().append("circle")
circles
.attr({
cx: function(d) { return d.x },
cy: function(d) { return d.y },
r: circleRadius,
fill: "#de9090",
"fill-opacity": 0.0,
//stroke: "#c9c9c9"
}).call(drag)
.on("dblclick", function(d) {
var index = points.indexOf(d);
console.log(index);
points.splice(index, 1);
d3.event.stopPropagation();
save();
draw();
})
}
function scale(i) {
return 1.1708928 * Math.sin(3 * i/numSamples * 2 * Math.PI);
}
function save() {
setTimeout(function() {
var newpoints = [];
points.forEach(function(d) { newpoints.push(d) });
cm.setValue(JSON.stringify(newpoints));
}, 10)
}
draw();
function getSamples(path, num) {
var len = path.getTotalLength()
var p, t;
var result = []
for(var i = 0; i < num; i++) {
p = path.getPointAtLength(i * len/num);
t = getTangent(path, i/num * 100);
t.v.x *= vectorScale
t.v.y *= vectorScale
result.push({
point: p,
tangent: t,
perp: rotate2d(t.v, 90)
});
}
return result
}
function getTangent(path, percent) {
// returns a normalized vector that describes the tangent
// at the point that is found at *percent* of the path's length
var fraction = percent/100;
if(fraction < 0) fraction = 0;
if(fraction > 0.99) fraction = 1;
var len = path.getTotalLength();
var point1 = path.getPointAtLength(fraction * len - 0.1);
var point2 = path.getPointAtLength(fraction * len + 0.1);
var vector = { x: point2.x - point1.x, y: point2.y - point1.y }
var magnitude = Math.sqrt(vector.x*vector.x + vector.y*vector.y);
vector.x /= magnitude;
vector.y /= magnitude;
return {p: point1, v: vector };
}
function rotate2d(vector, angle) {
//rotate a vector
angle *= Math.PI/180; //convert to radians
return {
x: vector.x * Math.cos(angle) - vector.y * Math.sin(angle),
y: vector.x * Math.sin(angle) + vector.y * Math.cos(angle)
}
}
[{"x":444,"y":80},{"x":230,"y":172},{"x":260,"y":306},{"x":207,"y":412},{"x":272,"y":484},{"x":279,"y":387},{"x":348,"y":377},{"x":310,"y":221},{"x":417,"y":180},{"x":381,"y":265},{"x":412,"y":385},{"x":350,"y":504},{"x":465,"y":449},{"x":495,"y":361},{"x":441,"y":287},{"x":521,"y":173},{"x":472,"y":133},{"x":579,"y":130},{"x":606,"y":187},{"x":504,"y":281},{"x":574,"y":309},{"x":582,"y":462},{"x":476,"y":530}]
#display {
background: white
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment