简体   繁体   中英

How to drop items on canvas from viewer perspective?

Where can I find an example of dropping items on a canvas? The idea is that from viewer perspective stains/drops fall on the canvas. How can I achieve this? Any help please.

Where to start? the only thing I found what came near the idea was: http://threedubmedia.com/code/event/drop and then the live option.

Here is an example of how to add a couple of coffee stains to the canvas:

在此处输入图片说明

This is code for the coffee stain and a Fiddle: http://jsfiddle.net/m1erickson/jkK7L/

<!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(){

    // get hold of the canvas
    var canvas=document.getElementById("canvas");
    // get hold of the canvas's context, which we use to draw with
    var context=canvas.getContext("2d");

    // ask for a new image
    var img=new Image();
    // wait for the image to load 
    img.onload=function(){
      // draw the coffee stains on the canvas using the context
      context.drawImage(img,0,0,img.width,img.height,50,50,100,100);
    }
    // tell the new image we asked for above where to find the image file
    // note: strangely, this must come after img.onload
    img.src="http://dl.dropbox.com/u/139992952/coffee-stain.png";

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

</head>

<body>
    <canvas id="canvas" width=300 height=300></canvas>
</body>
</html>

Here is a neat simulation of water rippling on the canvas:

Click on the canvas to see the ripple effect: http://www.script-tutorials.com/demos/97/index.html

And here is an example of a splat animation

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(){ alert("go"); 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM