Draw a (progressive) paint splash on a canvas

11,294

You can use illusion to create a nice splat effect.

Since objects “grow” as they approach, you can animate an increasing size plus a little motion to create your splat effect.

You can use context.drawImage to handle the resizing:

context.drawImage(splashImg, 0 ,0, splashImg.width, splashImg.height, 
                  newX, newY, newWidth, newHeight);

Here is code and a Fiddle: http://jsfiddle.net/m1erickson/r8Grf/

<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>

<style>
    body{ background-color: ivory; }
    canvas{border:1px solid red;}
</style>

<script>
    $(function(){

        var canvas=document.getElementById("canvas");
        var ctx=canvas.getContext("2d");

        window.requestAnimFrame = (function(callback) {
          return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
          function(callback) {
            window.setTimeout(callback, 1000 / 60);
          };
        })();

        $("go").html("Loading...");

        var count=80;
        var win=new Image();
        var splash;
        win.onload=function(){
            splash=new Image();
            splash.onload=function(){
              ctx.drawImage(win,0,0);
            }
            splash.src="http://dl.dropbox.com/u/139992952/splash2.svg";
        }
        win.src="http://dl.dropbox.com/u/139992952/window.png";

        $("#go").click(function(){ count=80; animate(); });

        function animate() {
          // drawings
          if(--count>1){
              ctx.clearRect(0, 0, canvas.width, canvas.height);
              ctx.save();
              ctx.drawImage(win,0,0);
              ctx.globalCompositeOperation = 'destination-over';
              ctx.drawImage(splash,0,0,splash.width,splash.height,25,25,splash.width/count,splash.height/count);
              ctx.restore();
          }

          // request new frame
          requestAnimFrame(function() {
              animate();
          });
        }

    }); // end $(function(){});
</script>

</head>

<body>
    <br/><button id="go">Splash!</button><br/><br/>
    <canvas id="canvas" width=326 height=237></canvas>
</body>
</html>
Share:
11,294
Razouille
Author by

Razouille

Updated on June 15, 2022

Comments

  • Razouille
    Razouille almost 2 years

    I'm looking for a simple method which can able me to draw in a canvas a paint splash like this on : paint splash

    One method will be to fire a lot of small particles which will paint a little circle, but I don't want to manage a lot of particle objects.

    EDIT : example here: jsfiddle.net/MK73j/4/

    A second method will be to have few images and manipulate scale and rotation, but I want to have a good random on the effect.

    A third method will be to make some random litte points, join them with bezier curves and fill the content, but I will have only a single mark.

    Well I don't know if there is a better method to have an effect which looks like this image or if I have to choose on of the 3 I thought about.