Actionscript:
-
[SWF(width = 600, height = 700, frameRate=24)]
-
var canvas:BitmapData = new BitmapData(stage.stageWidth,stage.stageHeight,false, 0xFFFFFF);
-
addChild(new Bitmap(canvas));
-
-
var maxBranches:int = 600;
-
var branches:int = 0;
-
var startX:Number = 300
-
makeBranch(startX,690,30,-60, 60);
-
-
function makeBranch(xp:Number, yp:Number, step:Number, min:Number, max:Number):void {
-
var vectors:Shape = Shape(addChild(new Shape()));
-
var cX:Number, cY:Number, eX:Number, eY:Number
-
var dcX:Number=xp, dcY:Number=yp, deX:Number=xp, deY:Number=yp;
-
var theta:Number = (min + Math.random()*(max-min) - 90) * Math.PI / 180;
-
cX = xp + step * Math.cos(theta);
-
cY = yp + step * Math.sin(theta);
-
theta = (min + Math.random()*(max-min)-90) * Math.PI / 180;
-
eX = cX + step * Math.cos(theta);
-
eY = cY + step * Math.sin(theta);
-
var run:Function = function():void{
-
dcX += (cX - dcX) / 2;
-
dcY += (cY - dcY) / 2;
-
deX += (eX - deX) / 8;
-
deY += (eY - deY) / 8;
-
with(vectors.graphics){
-
clear();
-
beginFill(0xFFFFFF,0.8);
-
lineStyle(0,0x000000,0.8);
-
moveTo(startX, yp);
-
lineTo(xp, yp);
-
curveTo(dcX, dcY, deX, deY);
-
lineTo(startX, deY);
-
}
-
if (Math.abs(dcX - cX) <1 && Math.abs(deX - eX) <1 && Math.abs(dcY - cY) <1 && Math.abs(deY - eY) <1){
-
canvas.draw(vectors);
-
removeChild(vectors);
-
if (branches <maxBranches){
-
setTimeout(makeBranch, 10, deX, deY, step - Math.random(), -90, 90);
-
branches++;
-
if (int(Math.random()*2) == 1){
-
setTimeout(makeBranch, 10, deX, deY, step - Math.random()*3, -90, 90);
-
branches++;
-
}
-
}
-
}else{
-
setTimeout(arguments.callee, 1000 / 24);
-
}
-
}();
-
}
This snippet uses a technique similar to what you might use to create a recursive tree. A bit of additional logic is added for bezier branches, filled shapes and animation.
WARNING: may run slow on older machines
Have a look at the swf...
7 Comments
Good work there as always Zevan. You might wanna put a warning on snippets with high processor usage. This one uses recursion and can cause the browser or the system to hang.
Thanks Mohammad. I’ll add a little warning - did it hang on your machine? On my macbook pro 2.4 ghz the flash player goes to 50% cpu usage…
It did on mine at home, if you don’t have dual core, 50% equals 100% lol
This is beautiful Zevan! Went quite fast on mine iMac 2Ghz.
thanks Og2t
I have a Corei7 920 Processor and a super-clocked GTX 260 running on my machine with Windows 7. I tried running this using IE, Firefox and Safari (on my hackmac) and all of the browsers hung. 50% of the processor was dedicated to the process. IE 8 was the only browser that stopped the script after giving me a warning.
I’d suggest two things:
- Put a limit on the recursion.
- Put Idle cycles in the loop, which would allow the processor to cool down.
That’s odd… just tested it on my old 1.2 ghz windows machine and it ran slow, but it didn’t hang. What version of the flash player are you using?
There is a limit on the recursion and it can be tweaked in the code by changing the maxBranches. The idle cycles aren’t necessary as you can just change the delay on line 41 from 10 ms to 100ms… or you could even use a slightly random ms so that not all the branches are running at the same time.
Anyone else experience a hang like the one Mohammad describes?