Skip to content
Nov 24 2009 / alecmce

Further Festive Flakes

Some more programmatic snow:


roll-over to activate

This one is more simple, though the logic is exploded over multiple classes. You can get the source files here. I create a bunch of BitmapData objects with semi-transparent circles to represent snowflakes, called FlakeForms. Then, create a bunch of Flake objects which are essentially positions at which those formal flakes should be drawn. Each EnterFrame, wipe the BitmapData clean, update the flake positions and draw the formal flakes into the updated positions.

FlakeForm

/**
 * Copyright (c) 2009 Alec McEachran
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */
package
{
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Shape;

	public class FlakeForm extends BitmapData
	{

		public function FlakeForm(radius:int, alpha:Number)
		{
			var n:int = radius << 1;
			super(n, n, true, 0);

			var shape:Shape = new Shape();
			var graphics:Graphics = shape.graphics;

			graphics.beginFill(0xFFFFFF, alpha);
			graphics.drawCircle(radius, radius, radius);
			graphics.endFill();

			draw(shape);
		}
	}
}

Flake

/**
 * Copyright (c) 2009 Alec McEachran
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */
package
{
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class Flake
	{
		private const ORIGIN:Point = new Point(0, 0);

		private var form:FlakeForm;

		private var bounds:Rectangle;

		private var position:Point;
		private var x:Number;

		private var y:Number;

		private var dx:Number;

		private var dy:Number;

		public function Flake(form:FlakeForm, bounds:Rectangle):void
		{
			this.form = form;
			this.bounds = bounds;
			this.position = new Point();

			x = bounds.left + Math.random() * bounds.width;
			y = bounds.top - 20 + Math.random() * (bounds.height + 20);
			dx = Math.random() * 4.2 - 1.5;
			dy = Math.random() * 2 + 1;
		}

		public function update(bitmapData:BitmapData):void
		{
			x += dx;
			y += dy;

			if (x > bounds.right)
				x = bounds.left + x - bounds.right;
			else if (x < bounds.left)
				x = bounds.right + x - bounds.left;

			if (y > bounds.bottom)
				y = bounds.top - 20;

			position.x = x;
			position.y = y;
			bitmapData.copyPixels(form, form.rect, position, null, ORIGIN, true);
		}
	}
}

Snow

/**
 * Copyright (c) 2009 Alec McEachran
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */
package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.geom.Rectangle;

	public class Snow extends Bitmap
	{

		private var count:int;

		private var flakes:Array;

		public function Snow(width:int, height:int, maxRadius:int, count:int, differentFlakes:int = 20)
		{
			super(new BitmapData(width, height, true, 0x00FFFFFF));
			this.count = count;

			var bounds:Rectangle = new Rectangle(0, 0, width, height);

			var factory:FlakeFactory = new FlakeFactory();
			var forms:Array = factory.generateForms(1, maxRadius, differentFlakes);
			flakes = factory.generateFlakes(count, bounds, forms);

			play();
		}

		public function play():void
		{
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}

		public function stop():void
		{
			removeEventListener(Event.ENTER_FRAME, onEnterFrame);
		}

		private function onEnterFrame(event:Event):void
		{
			var i:int = count;

			bitmapData.fillRect(bitmapData.rect, 0x00FFFFFF);
			while (i--)
			{
				var flake:Flake = flakes[i];
				flake.update(bitmapData);
			}
		}
	}
}
  • http://www.littlerobot.org.uk/ Lindsey

    Nice!

    • http://alecmce.com Alec McEachran

      Aw shucks, thanks Lindsey :)

  • http://www.littlerobot.org.uk/ Lindsey

    Nice!

  • http://alecmce.com alecmce

    Aw shucks, thanks Lindsey :)