Simple Matrix Clock
Simple Matrix Clock
ちょっと見やすくなった。
/**
* Copyright bkzen ( http://wonderfl.net/user/bkzen )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/lAh4
*/
// forked from bkzen's Simple Matrix
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.BlendMode;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.utils.setTimeout;
/**
* Simple Matrix Clock
* @author jc at bk-zen.com
*/
[SWF (backgroundColor = "0x000000", frameRate = "30", width = "465", height = "465")]
public class Test61 extends Sprite
{
private static const BOX_SIZE: int = 10;
private static const TIME_ARR: Array = [
0x1D9DD5, 0x377770,
0x148455, 0x045552,
0x149DDD, 0x371770,
0x149044, 0x151512,
0x1DDDC5, 0x371770
];
private var bmd: BitmapData, bmd2: BitmapData;
private var bmp: Bitmap;
private var colorTf: ColorTransform;
private var rect: Rectangle;
private var wCnt: int;
private var drawMatrix: Matrix;
private var clock: BitmapData;
private var clockLabel: BitmapData;
private var now: Date;
private var drawRect:Rectangle;
private var drawPoint:Point;
private var clock2: BitmapData;
public function Test61()
{
var w: int = stage.stageWidth, h: int = stage.stageHeight;
bmd = new BitmapData(w, h, false, 0);
bmd2 = new BitmapData(w, h + BOX_SIZE * 2, true, 0);
bmp = new Bitmap(bmd, "auto", true);
rect = new Rectangle(0, BOX_SIZE + 1, BOX_SIZE, BOX_SIZE);
colorTf = new ColorTransform(1, 1, 1, 1, -4, -4, -4);
drawMatrix = new Matrix(1, 0, 0, 1, 0, -BOX_SIZE * 2);
wCnt = w / (BOX_SIZE + 1);
clock = new BitmapData(w, h, false, 0);
clock2 = new BitmapData(w, h, false, 0);
var clockBmp: Bitmap = new Bitmap(clock, "auto", true);
clockBmp.blendMode = BlendMode.OVERLAY;
var sp: Sprite = new Sprite();
sp.addChild(bmp);
sp.addChild(clockBmp);
addChild(sp);
clockBmp = new Bitmap(clock2, "auto", true);
clockBmp.blendMode = BlendMode.OVERLAY;
addChild(clockBmp);
makeLabel();
drawClock();
addEventListener(Event.ENTER_FRAME, loop);
}
private function makeLabel(): void
{
var s: uint = (BOX_SIZE + 1) * 2;
var r: Rectangle = new Rectangle(0, 0, s, s);
var i: uint, t: uint, j: uint, c: int;
clockLabel = new BitmapData(s * 44, s * 7, false, 0);
clockLabel.lock();
for (i = 0; i < 10; i++)
{
c = i % 2;
t = TIME_ARR[i];
r.y = s * ((i >> 1) + 1);
for (j = 0; j < 22; j++)
{
r.x = s * (21 - j + c * 22);
if ((t >> j) & 1)
clockLabel.fillRect(r, 0xFFFFFF);
}
}
clockLabel.unlock();
drawRect = new Rectangle(0, 0, s * 4, s * 7);
drawPoint = new Point(0, s * 7);
}
private function drawClock(): void
{
now = new Date();
clock.lock();
clock.fillRect(clock.rect, 0);
var t: int = now.getHours();
drawNum(t / 10, 0);
drawNum(t % 10, 1);
drawNum( 10, 2);
t = now.getMinutes();
drawNum(t / 10, 3);
drawNum(t % 10, 4);
clock2.lock();
clock2.draw(clock);
clock2.unlock();
clock.unlock();
setTimeout(drawClock, (60 - now.getSeconds()) * 1000);
}
private function drawNum(num: int, pos: int): void
{
drawRect.x = drawRect.width * num;
drawPoint.x = drawRect.width * pos;
clock.copyPixels(clockLabel, drawRect, drawPoint);
}
private function loop(e: Event): void
{
bmd.lock(), bmd2.lock();
bmd.colorTransform(bmd.rect, colorTf);
if (Math.random() < 0.7)
{
rect.x = (Math.random() * wCnt | 0) * 11;
bmd2.fillRect(rect, 0xFF30FF30);
}
bmd2.scroll(0, BOX_SIZE + 1);
bmd.draw(bmd2, drawMatrix, null, null, null, true);
bmd.unlock(), bmd2.unlock();
}
}
}