Canvarticles Library Demo
January 30th, 2006
Canvarticles makes including particle effects in your canvases easy as pie. Take any canvas, and simply create an instance of the Emitter object in javascript to use the system. This group of object classes is what drive Canvarticles.com.
This example shows you how to create this:
First Download canvarticles.js
Then include the .js file in the HEAD section of your HTML:
<script type="text/javascript" src="prototype.js"></script>
<script type="text/javascript" src="canvarticles.js"></script>
Then put a script tag inside the body of your document:
<script type="text/javascript">
var emitter;
var ctx = $('canvas').getContext('2d');
emitter = new Emitter({
pos:new Vector(150, 270),
randomColors:0,
qty:25,
lifespan:20,
maxSpeed:15,
minSpeed:10,
angle:[270, 30],
gravity:0.5,
colors:[ new Color({r:255,g:0,b:0}), new Color({r:255,g:255,b:0}) ],
});
function draw() {
ctx.clearRect(0,0,300,300);
emitter.update();
emitter.draw(ctx);
}
setInterval('draw()', 50);
</script>
There are 3 main pieces to the above code:
- new Emitter(params): This creates the particle emitter. You can pass in a hash of properties to set the parameters on creation of the object. This is done in this format
emitter = new Emitter({qty:50, minRadius:5, maxRadius:25}) - emitter.update(): This processes 1 frame of the particles. It moves each particle along its velocity and aplies forces such as gravity. This should be called during the preparation of each frame of the animation.
- emitter.draw(ctx): This draws the particles to the canvas. Pass in the canvas context so the particles know where to draw themselves to.
The latest version of the library can always be found here: Download canvarticles.js
That should be all you need to know for now. Enjoy your canvarticles!
June 28th, 2007 at 11:05 PM
How do you establish if a browser supports canvas?
Using Safari 2.0.3 your example falsely tells me “Your browser has no canvas support”, while at the same time showing a perfect picture!
June 28th, 2007 at 11:05 PM
That is one thing I havent quite figured out yet, and its hard to test since I dont own a mac. In this solution i simply put some content between the <canvas> and </canvas>, and browsers are supposed to show the content if the tag is invalid, but is hidden if the canvas is parsed properly.
Apparently there is a tad more to it, I am still relatively new to this canvas thing.
June 28th, 2007 at 11:05 PM
Hi Alex, I love your particle engine. I’d like to use it in a Dashboard Widget I’m writing and want to make sure that this is OK with you. See http://metabang.com/widgets/stop-it/stop-it.html for details about the widget (note that this page is currently reachable by robots and won’t be until I hear from you (or a few days go by with no response since I’ll assume that silence gives consent <smile>)).
June 28th, 2007 at 11:05 PM
Of course you can use it. I give this away free for anyone that wants it.
Enjoy it!