In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: Particle Clock

軌跡を表示してみる

これを見て時計化したくなってしまった
* http://level0.kayac.com/2009/08/loopclock.php
Get Adobe Flash player
by demouth 27 Aug 2009
/**
 * Copyright demouth ( http://wonderfl.net/user/demouth )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/dJAa
 */

/**
 * 軌跡を表示してみる
 */
// forked from demouth's Particle Clock
 /**
 * これを見て時計化したくなってしまった
 * http://level0.kayac.com/2009/08/loopclock.php
 */
// forked from demouth's 雑に一筆書き
// forked from egyu2's 一筆書き(鉛筆風)
package  
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.PixelSnapping;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.display.Sprite;
	
	public class Main extends Sprite
	{
		private var vs:Array = [];
		private var d:Drawer;
		private var canvas:Bitmap;
		private var offset:int = 20;
		private var fontSize:int = 50;
		private var drawScale:Number = 4;
		
		public function Main() 
		{
			if (stage) init()
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(event:Event = null):void
		{
			this.stage.align = StageAlign.TOP_LEFT;
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
			
			//座標の計算
			for (var i:int = 0; i < 11; i++)
			{
				var t:TextField = new TextField();
				var write:String = (i == 10)? ":" : i.toString();
				t.htmlText = "<font size='"+this.fontSize.toString()+"'>" + write + "</font>";
				this.vs[i] = this.checkColor(t);
			}
			
			//キャンバスの初期化
			var bitmapData:BitmapData = new BitmapData((this.offset*5+this.fontSize)*this.drawScale, this.fontSize*this.drawScale, true,0x00000000);			this.canvas = new Bitmap(bitmapData,PixelSnapping.ALWAYS , true);
			this.addChild(this.canvas);
			this.canvas.x = 10;
			this.canvas.y = 100;
			
			//描画処理の初期化
			for (var j:int = 0; j < 30; j++) 
			{
				var d:Drawer = new Drawer(this.createTimePointArray(), this.drawScale, this.canvas );
				d.ac = (Math.random() * 1) + 0.1;
				d.fr = (Math.random() * 0.6) + 0.05;
				d.zatsu = (Math.random() * 20) + 1;
				this.addChild(d);
				d.x = this.canvas.x;
				d.y = this.canvas.y;
				d.addEventListener(Event.COMPLETE , this.drawCompleteHandler);
				d.start();
			}
			this.addEventListener(Event.ENTER_FRAME , this.enterFrameHandler);
			
		}
		
		private function enterFrameHandler(event:Event):void
		{
			this.canvas.bitmapData.colorTransform(new Rectangle(0,0,this.canvas.bitmapData.width,this.canvas.bitmapData.height),new ColorTransform(1,1,1,20,1.005,1.1,1.1));
		}
		
		private function drawCompleteHandler(event:Event):void
		{
			event.target.v = this.createTimePointArray();
			event.target.ac = (Math.random() * 1) + 0.1;
			event.target.start();
		}
		
		/**
		 * 現在時刻の座標の配列を返す。
		 * @return
		 */
		private function createTimePointArray():Vector.<Point>
		{
			var date:Date = new Date();
			var count:int = 0;
			var time:Number
			var v:Vector.<Point> = new Vector.<Point>();
			for (var i:int = 0; i < 2; i++) 
			{
				if (i == 0)
				{
					time = date.hours;//時間を使用
				}
				else
				{
					time = date.minutes;//分を使用
					v = v.concat(this.shiftPoint(this.vs[10] as Vector.<Point> , count++ * this.offset , 0));//「:」を追加
				}
				if (time < 10)
				{
					v = v.concat(this.shiftPoint(this.vs[0] as Vector.<Point> , count++ * this.offset , 0));
					v = v.concat(this.shiftPoint(this.vs[time.toString().charAt(0)] as Vector.<Point> , count++ * this.offset , 0));
				}
				else
				{
					v = v.concat(this.shiftPoint(this.vs[time.toString().charAt(0)] as Vector.<Point> , count++ * this.offset , 0));
					v = v.concat(this.shiftPoint(this.vs[time.toString().charAt(1)] as Vector.<Point> , count++ * this.offset , 0));
				}
			}
			return v;
		}
		
		private function shiftPoint(v:Vector.<Point> , x:Number , y:Number):Vector.<Point>
		{
			var returnV:Vector.<Point> = new Vector.<Point>();
			for (var i:int = 0; i < v.length; i++) 
			{
				returnV.push(new Point(v[i].x + x , v[i].y + y));
			}
			return returnV;
		}
		
		/**
		 * 描画するドットを決定
		 */
		private function checkColor(displayObject:DisplayObject):Vector.<Point>
		{
			var bd:BitmapData = this.createBitmap(displayObject);
			var v:Vector.<Point> = new Vector.<Point>();
			var w:uint = bd.width;
			var h:uint = bd.height;
			var r:uint = 0;
			var g:uint = 0;
			var b:uint = 0;
			
			for (var i:int = 0; i < h; i++) 
			{
				for (var j:int = 0; j < w; j++) 
				{
					var color:uint = bd.getPixel(j, i);
					r = ( color >> 16 ) & 0xFF;
					g = ( color >> 8 ) & 0xFF;
					b = ( color ) & 0xFF;
					if ( (r + g + b) < 10 )v.push(new Point(j, i));//閾値以下なら描画用の座標に格納
				}
			}
			v = this.sort(v);
			return v;
		}
		
		private function createBitmap(displayObject:DisplayObject):BitmapData
		{
			var bd:BitmapData = new BitmapData(displayObject.width , displayObject.height , false , 0xFFFFFF);
			bd.draw(displayObject);
			return bd;
		}
		
		private function sort(v:Vector.<Point>):Vector.<Point>
		{
			var length:int = v.length;
			var tmp:Vector.<Point> = new Vector.<Point>();
			var nowPoint:Point = new Point();
			for (var i:int = 0; i < length; i++) 
			{
				var num:int = this.check(nowPoint ,v);
				nowPoint = v[num];
				tmp.push(v.splice(num, 1)[0]);
			}
			return tmp;
		}
		
		private function check(p:Point ,v:Vector.<Point>):int
		{
			var near:uint;
			var nearDiff:int;
			var length:uint = v.length;
			
			var diffX:int = 0;
			var diffY:int = 0;
			var diff:int = 0;
			for (var i:int = 0; i < length; i++) 
			{
				diffX = v[i].x - p.x;
				diffY = v[i].y - p.y;
				diff = Math.sqrt((diffX * diffX) + (diffY * diffY));
				
				if (diff < nearDiff || (i == 0))
				{
					nearDiff = diff;
					near = i;
				}
			}
			return near;
		}
		
	}
	
}

import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
class Drawer extends Sprite
{
	public var ac:Number = 0.3;
	public var fr:Number = 0.6;
	public var zatsu:Number = 20;//雑具合。高いと雑になる。
	public var v:Vector.<Point>;
	private var bitmap:Bitmap;
	private var brush:Point = new Point();
	private var w:Number;
	private var h:Number;
	private var num:int = 0;
	private var vX:Number = 0;
	private var vY:Number = 0;
	private var scale:Number = 1;
	
	public function Drawer(v:Vector.<Point> , scale:Number , bitmap:Bitmap = null) 
	{
		this.v = v;//座標
		this.scale = scale;//描画するサイズ
		this.bitmap = bitmap;
	}
	
	public function start():void
	{
		this.num = 0;
		var length:int = this.v.length;
		for (var i:int = 0; i < length; i++) 
		{
			this.v[i].x *= this.scale;
			this.v[i].y *= this.scale;
		}
		this.addEventListener(Event.ENTER_FRAME , enterFrameHandler);
	}
	
	private function draw():void
	{
		if (this.num < this.v.length) {
			this.graphics.clear();
			var p:Point = this.v[this.num];
			if (this.num == 0) this.brush.x = p.x;
			if (this.num == 0) this.brush.y = p.y;
			var oldP:Point = this.brush.clone();
			this.graphics.moveTo(this.brush.x , this.brush.y);
			this.vX += (p.x - this.brush.x) * this.ac;
			this.vY += (p.y - this.brush.y) * this.ac;
			this.vX *= this.fr;
			this.vY *= this.fr;
			this.brush.x += this.vX;
			this.brush.y += this.vY;
			var d:Number = int(Point.distance(oldP, this.brush) * 20);
			d = (d > 220)?220:d;
			this.graphics.lineStyle(0.1, d<<16|d<<8|d);
			this.graphics.lineTo(this.brush.x , this.brush.y);
			if(this.bitmap)this.bitmap.bitmapData.draw(this);
			var diffX:int = p.x - this.brush.x;
			var diffY:int = p.y - this.brush.y;
			var diff:Number = Math.sqrt((diffX * diffX) + (diffY * diffY));
			if(diff < this.zatsu)this.num++;
		} else {
			this.removeEventListener(Event.ENTER_FRAME , enterFrameHandler);
			this.dispatchEvent(new Event(Event.COMPLETE ));
		}
	}
	
	private function enterFrameHandler(event:Event):void
	{
		this.draw();
	}
	
}