I wanted to make a demonstration file for a talk I’m doing this week to show the difference between bitmap and vector performance. So I needed something that drew a whole lot of lines. Came up with this, which I probably won’t use for the presentation, but surprised me with a cool pattern emerging.
[kml_flashembed publishmethod=”static” fversion=”10.0.0″ movie=”http://www.bit-101.com/blog/wp-content/uploads/2010/01/speed_test_vector.swf” width=”400″ height=”400″ targetclass=”flashmovie”]
[/kml_flashembed]
Click to change the pattern. Zoom in once or twice to see what’s actually happening. Here’s the code:
[as3]var angle:Number = 0;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(MouseEvent.CLICK, onClick);
var size:Number = 5;
var xrand:Number = Math.random() * .1 – .05;
var yrand:Number = Math.random() * .1 – .05;
function onEnterFrame(event:Event):void
{
graphics.clear();
graphics.lineStyle(0, 0, 1);
for(var i:int = size; i < 400; i+= size) { for(var j:int = size; j < 400; j+= size) { var xoff:Number = Math.cos(angle + Math.cos(i * xrand)) * size; var yoff:Number = Math.sin(angle + Math.sin(j * yrand)) * size; graphics.moveTo(i, j); graphics.lineTo(i + xoff, j + yoff); } } angle += .1; } function onClick(event:MouseEvent):void { xrand = Math.random() * .12 - .06; yrand = Math.random() * .12 - .06; }[/as3]
Isn’t it amazing that you can mix in a little bit of Sine, and a little bit of Cosine and then stir it up with some random and get wicked cool stuff?