By Zevan | October 31, 2008
Actionscript:
-
for (var i:int = 0; i<4; i++){
-
this["phase"+i]();
-
}
-
-
function phase0():void{
-
trace("phase 0");
-
}
-
function phase1():void{
-
trace("phase 1");
-
}
-
function phase2():void{
-
trace("phase 2");
-
}
-
function phase3():void{
-
trace("phase 3");
-
}
-
-
/*
-
will output:
-
phase 0
-
phase 1
-
phase 2
-
phase 3
-
*/
-
-
/*
-
WARNING: This code was written for fun. Use at your own risk.
-
*/
I was pleasantly surprised back in the AS1 days... when I discovered that associative array syntax worked along with function calls (why wouldn't it?). There are some interesting tricks you can do with this technique.... will post some later....
By Zevan | October 30, 2008
Actionscript:
-
var canvas:BitmapData=new BitmapData(280,280,false,0x000000);
-
addChild(new Bitmap(canvas, PixelSnapping.AUTO, true));
-
-
var color:uint;
-
-
// anchor x1, anchor y1,
-
// control-handle x2, control-handle y2,
-
// anchor x3, anchor y3, control-handle x4,
-
// control-handle y4, [resolution incremental value between 0-1]
-
function cubicBezier(x1:Number, y1:Number, x2:Number, y2:Number,
-
x3:Number, y3:Number, x4:Number, y4:Number, resolution:Number=.03):void {
-
-
var b:Number,pre1:Number,pre2:Number,pre3:Number,pre4:Number;
-
for (var a = 0; a <1; a+=resolution) {
-
b=1-a;
-
pre1=(a*a*a);
-
pre2=3*(a*a)*b;
-
pre3=3*a*(b*b);
-
pre4=(b*b*b);
-
canvas.setPixel(pre1*x1 + pre2*x2 + pre3*x4 + pre4*x3 ,
-
pre1*y1 + pre2*y2 + pre3*y4 + pre4*y3, color);
-
}
-
}
-
-
// draw a few
-
-
color = 0xFFFFFF;
-
-
cubicBezier(100,100,
-
150, 50,
-
200, 100,
-
150, 150);
-
-
color = 0xFF0000;
-
-
cubicBezier(10,10,
-
150, 10,
-
200, 200,
-
250, 150,
-
.01);
-
/*
-
WARNING: This code was written for fun. Use at your own risk.
-
*/
This draws cubic bezier curves with setPixel. I'm pretty sure I picked up the original math from somewhere on the processing forums in '03 or '04...
By Zevan | October 28, 2008
Actionscript:
-
var size:Number = 400;
-
var halfSize:Number = size / 2;
-
var canvas:BitmapData = new BitmapData(size, size,false, 0x000000);
-
addChild(new Bitmap(canvas, PixelSnapping.AUTO, true));
-
-
var rings:Number = 40;
-
var color:uint, theta:Number, dx:Number, dy:Number;
-
-
for (var i:int = 0; i<canvas.width; i++){
-
for (var j:int = 0; j<canvas.height; j++){
-
// distance from center of canvas to current x, y coord
-
dx = i - halfSize;
-
dy = j - halfSize;
-
theta = Math.sqrt((dx * dx)+(dy * dy)) * rings;
-
color = (100 + 100 * Math.sin(theta * Math.PI/180)) <<16;
-
canvas.setPixel(i, j, color);
-
}
-
}
-
-
/*
-
WARNING: This code was written for fun. Use at your own risk.
-
*/
This is the previous post written in a readable way. Draws a radial gradient using pythagorean theorem and sine.
By Zevan | October 28, 2008
Actionscript:
-
// add a BitmapData object to the stage in one line:
-
addChild(new Bitmap(new BitmapData(200,200,false,0x00000)));
-
-
// draw a radial gradient
-
for (var i:int = 0; i<200; i++) for (var j:int = 0; j<200; j++)
-
Bitmap(getChildAt(0)).bitmapData.setPixel( i, j, 100 + 100 * Math.sin(Math.sqrt(Math.pow(i - 100, 2)+Math.pow(j - 100, 2)) * 40 * Math.PI/180) <<16 );
-
-
/*
-
WARNING: This code was written for fun. Use at your own risk.
-
*/
Next I'll post a readable version, just wanted to see how compact I could get this...
This was inspired by all the pixel bender stuff that I've seen. I did the same thing with setVector() - will post about setVector() in the future.