Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html lang="en">
<head>
	<meta charset="utf-8">
	<title>Canvas particle system for vapour</title>
	<style>
	
	</style>
</head>
<body>
  <canvas id="vapour" style="background:url('http://code.medula.cl/img/content/tetera-canvas.jpg');max-width:100%"></canvas>
<canvas id="dot-buffer"></canvas>
<p>There's an article explaining this: <a href="http://code.medula.cl/article_Animating-vapour-with-Javascript-on-canvas.html">Animating vapour with JavaScript on canvas</a></p>
</body>
</html>
              
            
!

CSS

              
                html, body{
	    padding:0;
	    margin:0;
	    height:100%;
	    width:100%;
	    overflow:hidden;
      font-family: sans-serif;
	}
	#dot-buffer{
	    display:none;
	}
              
            
!

JS

              
                // http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
 
// requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel
 
// MIT license
 
(function() {
    var lastTime = 0;
    var vendors = ['ms', 'moz', 'webkit', 'o'];
    for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
        window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
        window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] 
                                   || window[vendors[x]+'CancelRequestAnimationFrame'];
    }
 
    if (!window.requestAnimationFrame)
        window.requestAnimationFrame = function(callback, element) {
            var currTime = new Date().getTime();
            var timeToCall = Math.max(0, 16 - (currTime - lastTime));
            var id = window.setTimeout(function() { callback(currTime + timeToCall); }, 
              timeToCall);
            lastTime = currTime + timeToCall;
            return id;
        };
 
    if (!window.cancelAnimationFrame)
        window.cancelAnimationFrame = function(id) {
            clearTimeout(id);
        };
}());

/////////////////////////////////////////////////////////////
///////////////////-------------- aquí empieza lo mío
//////////////////////////////////////////////////////////////

var ratePF = 2;
    var particleList = [];
    var originPoint = { x:405, y:391 };
    var canvas = document.getElementById('vapour');
    var cntxt2d = canvas.getContext('2d');
    var running = true;
    var particleLife = 100;
    
    canvas.width = 579;
    canvas.height = 695;
    var bufferCanvas = document.getElementById('dot-buffer');
    var bufcntxt = bufferCanvas.getContext('2d');
    bufferCanvas.width = 10;
    bufferCanvas.height = 10;
    var rGrad = bufcntxt.createRadialGradient(5,5,0,5,5,5);
    rGrad.addColorStop(0,'rgba(252, 194, 18, 1)');
    rGrad.addColorStop(1,'rgba(52, 14, 208, 0)');
    bufcntxt.fillStyle = rGrad;
    bufcntxt.fillRect(0,0,10,10);
    var dotImage = bufcntxt.getImageData(0,0,10,10);
    bufcntxt.putImageData(dotImage,0,0);
    
    function randRange(min,max){
        return Math.random()*(max-min)+min;
    }
    
    function genParticles(){
        var len = ratePF;
        while(len--){
            var oneParticle = {
               x: originPoint.x,
               y: originPoint.y,
               velx: randRange(-1.2,0.8),//range of speed on the x-axis
               vely: randRange(-3,-0.75),//range of speed on the y-axis
               life: particleLife,
               totalLife: particleLife,
               maxSize: randRange(60,100)
            };
            oneParticle.x -= oneParticle.velx;
            oneParticle.y -= oneParticle.vely;
            particleList.push(oneParticle);
        }
    }
    
    function positionParticles(){
        var len = particleList.length;
        while(len--){
            var op = particleList[len];//op = one particle
            op.life--;
            if(op.life < 1){
                particleList.splice(len,len+1);
                continue;
            }
            //if(!len%1000)console.log(op.life);
            op.x += op.velx;
            op.y += op.vely;
        }
    }
    
    function render() {
        cntxt2d.clearRect(0,0,window.innerWidth,window.innerHeight);
        var len = particleList.length;
        while(len--){
            var op = particleList[len];//op = one particle
            ga = op.life / op.totalLife;
            cntxt2d.globalAlpha = ga;
            var size = op.maxSize - op.maxSize * ga + 10;
            cntxt2d.drawImage(bufferCanvas, op.x, op.y, size, size);
        }
    }
    
    function animationLoop(){
        genParticles();
        positionParticles();
        render();
        if(running) {
            requestAnimationFrame(animationLoop);
        }
    }
    
    animationLoop();
              
            
!
999px

Console