0

我在哪里可以找到在画布上放置项目的示例?这个想法是从观众的角度来看,污渍/水滴落在画布上。我怎样才能做到这一点?请提供任何帮助。

从哪儿开始?我发现的唯一想法是: http ://threedubmedia.com/code/event/drop然后是 live 选项。

4

1 回答 1

0

以下是如何在画布上添加几个咖啡渍的示例:

在此处输入图像描述

这是咖啡渍和小提琴的代码: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>

这是画布上水波纹的巧妙模拟:

点击画布查看涟漪效果:http ://www.script-tutorials.com/demos/97/index.html

这是一个 splat 动画的例子

这是代码和小提琴: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>
于 2013-03-12T22:20:18.600 回答