Flash sine wave fun

Simple but periodically pleasing. Click to randomly generate a new scene.


After Pause Talk vol. 29 last night when talking with just-arrived-in-Tokyo Brian Thomas the topic of algorithmically generated art came up. I spent five minutes and cooked this up. It was definitely fun and I want to explore it more. I need to go ahead and learn the Processing language which I’ve heard is great for this kind of stuff.

Full source code is below:

package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;

	public class SinDraw extends Sprite
	{
		private var sinWaveData:Array;

	    [SWF(width=500,height=300)]
		public function SinDraw()
		{
			sinWaveData = new Array(200);
			onClick(null);

			stage.addEventListener(MouseEvent.CLICK, onClick);
		}

		private function generateWave():void
		{
			var amplitude:Number = 100 * Math.random();
			var frequency:Number = 0.0174532925 * 2; // 2 degrees in radians
			var phase:Number = 100 * Math.random();

			// intentionally goofy
			var r:uint = (255 * Math.random()) << 16;
			var g:uint = (255 * Math.random()) << 8;
			var b:uint = (255 * Math.random()) << 24;
			var color:uint = 0xff00000000 + r + g + b;

			for (var i:int = 0; i < 200; i++)
			{
				sinWaveData[i] = amplitude * Math.sin((i * frequency) + phase);
			}

			for (i = 0; i < 200; i++)
			{
				var index:Number = i * (500 / 200);

				graphics.beginFill(color, 1);
				graphics.drawCircle(index, sinWaveData[i] + 170, Math.random() * 5);
				graphics.endFill();
			}
		}

		private function onClick(e:MouseEvent):void
		{
			graphics.clear();

			var matrix:Matrix = new Matrix();
			matrix.createGradientBox(500, 300, 90);
			trace(this.width);

			// intentionally goofy
			var r:uint = (255 * Math.random()) << 16;
			var g:uint = (255 * Math.random()) << 8;
			var b:uint = (255 * Math.random()) << 24;
			var color:uint = 0xff00000000 + r + g + b;

			graphics.beginGradientFill("linear", [color, (color - 0xff222222)], [1, 1], [0, 0xff], matrix);
			graphics.drawRect(0, 0, 500, 300);
			graphics.endFill();

			generateWave();
			generateWave();
			generateWave();
			generateWave();
			generateWave();
		}
	}
}

One Response to “Flash sine wave fun”

  1. nasim says:

    hi
    thank u
    i ‘ m new in flash i should work on wave , could u give me some advice , how to start
    thank u

Leave a Reply