Skip to content

Instantly share code, notes, and snippets.

@enjalot
Created April 22, 2013 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enjalot/5431703 to your computer and use it in GitHub Desktop.
Save enjalot/5431703 to your computer and use it in GitHub Desktop.
verlet-js editable
{"description":"verlet-js editable","endpoint":"","display":"canvas","public":true,"require":[{"name":"verlet-js","url":"http://enjalot.github.io/verlet-js/verlet.js"}],"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},"shape.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":true,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"thumbnail":"http://i.imgur.com/2O2IR0o.png"}
//verlet-js: http://subprotocol.com/2013/04/18/introducing-verlet-js.html
var width = tributary.sw;
var height = tributary.sh;
var sim;
var segment;
tributary.init = function(ctx) {
// simulation
sim = new VerletJS(width, height, tributary.canvas);
//sim.friction = 1;
// entities
var segments = [];
var pins = [];
tributary.shape.forEach(function(p,i) {
segments.push(new Vec2(p.x, p.y))
if(p.pin) pins.push(p.i = i)
});
segment = sim.lineSegments(segments, 0.002);
pins.forEach(function(pin) {
segment.pin(pin);
})
console.log("sim", segment)
}
d3.select("#display").on("mouseup", saveShape);
tributary.run = function(ctx, t) {
if(sim) {
sim.frame(8);
sim.draw();
}
}
function saveShape() {
if(!segment) return;
var particles = segment.particles;
for(var i = 0, l = segment.particles.length; i < l; i++) {
tributary.shape[i].x = particles[i].pos.x;
tributary.shape[i].y = particles[i].pos.y;
}
var cm = tributary.getCodeEditor("shape.json")
cm.setValue(JSON.stringify(tributary.shape));
console.log("saved");
}
[{"pin":1,"i":0,"x":458,"y":50},{"pin":0,"x":412.2410896267671,"y":122.06965245657271},{"pin":1,"i":2,"x":364,"y":55},{"pin":0,"x":316.3113672866083,"y":170.16774306590835},{"pin":1,"i":4,"x":267,"y":51},{"pin":0,"x":174.39341515769698,"y":229.68027212578937},{"pin":1,"i":6,"x":74,"y":54}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment