Fast Metaball Effect in AS3


var container:Sprite = new Sprite();
var balls:Vector. = new Vector.();
var m:Matrix;
var s:Shape;
var r:Number;
var c:uint;
for(var i:int = 0; i < 10; i++){
r = Math.random() * 100 + 100;
m = new Matrix();
m.createGradientBox(r, r, 0, -r * 0.5, -r * 0.5);
s = container.addChild(new Shape()) as Shape;
c = Math.random() * 0xFFFFFF;
s.graphics.beginGradientFill(GradientType.RADIAL, [c, c], [1, 0], [0x00, 0xFF], m);
s.graphics.drawRect(-r * 0.5, -r * 0.5, r, r);
s.x = Math.random() * 400;
s.y = Math.random() * 300;
moveBall(s);
balls.push(s);
}
function moveBall(s:Shape):void
{
//whatever easing to move the spheres
}
var s2:Shape = container.addChild(new Shape()) as Shape;
m = new Matrix();
m.createGradientBox(200, 200, 0, -100, -100);
c = Math.random() * 0xFFFFFF;
s2.graphics.beginGradientFill(GradientType.RADIAL, [c, c], [1, 0], [0x00, 0xFF], m);
s2.graphics.drawRect(-100, -100, 200, 200);
var bmpData:BitmapData = new BitmapData(400, 300, true, 0x00000000);
var bmp:Bitmap = addChild(new Bitmap(bmpData)) as Bitmap;
addEventListener(Event.ENTER_FRAME, render);
function render(e:Event):void
{
s2.x = mouseX;
s2.y = mouseY;
bmpData.applyFilter(bmpData, bmpData.rect, new Point(0, 0), new BlurFilter(2, 2, 2));
bmpData.draw(container);
var i:int, l:int;
var aArr:Array = [];
var c:int;
for(i = 0; i <= 0xFF; i++)
{
c = (i - 0x80) << 1;
if(c < 0) c = 0;
aArr[i] = c << 24;
}
bmpData.paletteMap(bmpData, bmpData.rect, new Point(0, 0), null, null, null, aArr);
}

Vote in HexoSearch Vote

4 Comments »

  1. Tweets that mention labs.hellokeita.com » Fast Metaball Effect in AS3 -- Topsy.com said,

    August 2, 2010 @ 11:30 pm

    [...] This post was mentioned on Twitter by Gabriel Laet, keita kuroki, Marcelo Guarnieri Fº, Gareth Parker, qanta and others. qanta said: RT @keitakun: fast metaball in AS3. No crazy math needed http://labs.hellokeita.com/2010/08/02/fast-metaball-effect-in-as3/ [...]

  2. antonio brandao said,

    October 19, 2010 @ 12:38 am

    pretty sexy efect you got there. one of the best I’ve seen.

    I’m looking for vectorial metaballs though, which seems to be much more complex to develop.

    have you seen the vectorial metaballs by Mathieu Badimon ? You can see it in his experience “Liquid Form” in http://lab.mathieu-badimon.com/

    surprisingly it was done using AS2 in 2006 :D

  3. hinson parsifal said,

    April 4, 2011 @ 3:42 am

    I am getting this error when I try to run it in Flash CS3
    (I placed your code in the first frame of the main timeline)

    1084: Syntax error: expecting identifier before assign.
    –> var balls:Vector. = new Vector.();

  4. mohamadmahd said,

    June 26, 2011 @ 10:50 pm

    hello
    what can HighLight text ltf flash as3 + scrollpanel
    But only a few words
    Thanks

RSS feed for comments on this post · TrackBack URI

Leave a Comment