在画布上绘制(渐进式)油漆飞溅

2022-01-17 00:00:00 canvas drawing javascript html html5-canvas

我正在寻找一种简单的方法,它可以让我在画布上绘制 paint splash 像这样:

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

一种方法是发射很多小粒子,这会画一个小圆圈,但我不想管理很多粒子对象.

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.

编辑:示例: 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.

嗯,我不知道是否有更好的方法来产生看起来像这张图片的效果,或者我是否必须从我想到的 3 个中进行选择.

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.

推荐答案

您可以使用错觉来创建漂亮的 splat 效果.

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.

您可以使用 context.drawImage 来处理大小调整:

You can use context.drawImage to handle the resizing:

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

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

相关文章