Skip to content

Instantly share code, notes, and snippets.

@jeromeetienne
Created July 16, 2012 08:15
Show Gist options
  • Save jeromeetienne/3121489 to your computer and use it in GitHub Desktop.
Save jeromeetienne/3121489 to your computer and use it in GitHub Desktop.
coder fun: chained api going too far :)

The code you see below is a funny building of chained API :) it is a particle emitter for flame throwers. look at the running demo. I love the end of the flame. all in a single chained instruction. Recently i did all my libraries with chained api and i like it. the lib used below is fireworks.js. This particular emitter is a bit too much chained tho :)

but still i look at the size of the source and at the effect on the screen... you got a pretty cool looking effects for 60lines of javascript. maybe i should just break it in several instructions... would be artificial but hey :)

= a coder having fun

var emitter = Fireworks.createEmitter({nParticles : 200})
.useSpawnerSteadyRate(10)
.bindTriggerDomEvents()
.effectsStackBuilder()
.position(Fireworks.createShapeSphere(0, 0, 0, 0.01))
.velocity(Fireworks.createShapeSphere(0, 0, -30, 0.1))
.lifeTime(1.5)
.friction(0.99)
.acceleration({
effectId : 'gravity',
shape : Fireworks.createShapePoint(0, 5, 0)
})
//.randomVelocityDrift(Fireworks.createVector(1,1,0))
.randomVelocityDrift(Fireworks.createVector(0,0,9))
.createEffect('scale', {
origin : 1/20,
factor : 1.01
}).onBirth(function(particle){
var object3d = particle.get('threejsObject3D').object3d;
var scale = this.opts.origin;
object3d.scale.set(scale, scale, scale)
}).onUpdate(function(particle, deltaTime){
var object3d = particle.get('threejsObject3D').object3d;
object3d.scale.multiplyScalar(this.opts.factor);
}).back()
.renderToThreejsObject3D({
container : world,
create : function(){
var object3d = new THREE.Sprite({
//color : 0xffaacc,
useScreenCoordinates : false,
map : texture,
blending : THREE.AdditiveBlending,
transparent : true
});
object3d.opacity= 0.9;
object3d.rotation = Math.floor(Math.random()*Math.PI*2);
object3d.uvScale.set(1, 1/urls.length)
return object3d;
}
})
.createEffect("renderer")
.onUpdate(function(particle, deltaTime){
var object3d = particle.get('threejsObject3D').object3d;
var canonAge = particle.get('lifeTime').normalizedAge();
var imageIdx = Math.floor(canonAge * (urls.length));
var uvOffsetY = imageIdx * 1/urls.length;
object3d.uvOffset.set(0, uvOffsetY)
}).back()
.back()
.start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment