Actionscript:
-
[SWF(width=600,height=650)]
-
var canvas:BitmapData=Bitmap(addChild(new Bitmap(new BitmapData(600,300,false,0xCCCCCC),"auto",true))).bitmapData;
-
var stills:BitmapData=Bitmap(addChild(new Bitmap(new BitmapData(600,380,false,0xAAAAAA),"auto",true))).bitmapData;
-
getChildAt(1).y=300;
-
-
var c:Shape = new Shape();
-
var m:Matrix = new Matrix();
-
m.createGradientBox(40, 40, 0, 0, 0);
-
c.graphics.beginGradientFill(GradientType.RADIAL, [0xCC0000, 0xCC0000], [1, 0], [0, 255], m);
-
c.graphics.drawCircle(20,20,20);
-
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
function onLoop(evt:Event):void {
-
c.x=mouseX-c.width/2;
-
c.y=mouseY-c.height/2;
-
canvas.draw(c, c.transform.matrix);
-
}
-
-
var spacing:Number = 10;
-
var cols:Number = 4;
-
var max:Number = cols * cols;
-
var size:Number = 1/(canvas.width/((canvas.width / cols) - spacing));
-
var st:Matrix = new Matrix();
-
st.scale(size, size);
-
var w:Number = canvas.width * st.d + spacing;
-
var h:Number = canvas.height * st.d + spacing;
-
var timer:Timer=new Timer(500);
-
timer.start();
-
timer.addEventListener(TimerEvent.TIMER, onCapture);
-
function onCapture(evt:TimerEvent):void {
-
var inc:int = timer.currentCount - 1;
-
st.tx = (inc% cols) * w+ spacing / 2;
-
st.ty = int(inc / cols) * h + spacing;
-
stills.draw(canvas, st);
-
if (timer.currentCount==max) {
-
timer.reset();
-
timer.start();
-
}
-
}
Take snapshots of a given BitmapData and arrange them in a grid. I wrote this snippet quickly in response to a question so it could probably use a little clean up...
You'll need to move your mouse over the large canvas bitmap (drawing to it) to see anything...
Actionscript:
-
var canvas:BitmapData = new BitmapData(400,400,false, 0xCCCCCC);
-
addChild(new Bitmap(canvas));
-
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
function onLoop(evt:Event):void {
-
canvas.fillRect(canvas.rect, 0xCCCCCC);
-
-
var r:Number = Math.abs(200 - mouseX);
-
var r2:Number = r * r;
-
var inc:Number = 1 / r;
-
var xp:Number = .00000001;
-
var yp:Number = -r;
-
while(yp<r){
-
var y:Number = 200 + yp;
-
yp += xp * inc;
-
xp = Math.sqrt(r2 - yp * yp);
-
canvas.setPixel(200 + xp, y, 0x000000);
-
canvas.setPixel(200 - xp, y, 0x000000);
-
}
-
}
A slow way to draw circles using setPixel().
Actionscript:
-
var canvas:BitmapData = new BitmapData(400,400,false, 0xCCCCCC);
-
addChild(new Bitmap(canvas));
-
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
function onLoop(evt:Event):void {
-
canvas.fillRect(canvas.rect, 0xCCCCCC);
-
var r:Number = Math.abs(200 - mouseX);
-
var r2:Number = r * r;
-
var inc:Number = 1 / r;
-
var xp:Number = .000001;
-
var yp:Number = -r;
-
while(yp <= 0){;
-
var x1:Number = 200 + xp;
-
var y1:Number = 200 + yp
-
var x2:Number = 200 - xp;
-
var y2:Number = 200 - yp;
-
canvas.setPixel(x1, y1, 0x000000);
-
canvas.setPixel(x1, y2, 0x000000);
-
canvas.setPixel(x2, y1, 0x000000);
-
canvas.setPixel(x2, y2, 0x000000);
-
yp += xp * inc;
-
xp = Math.sqrt(r2 - yp * yp);
-
}
-
}
Little better, still slow.
I was brainstorming about a few things today and somehow these two slow circle drawing algorithms popped out. These are pretty useless compared to some of the famous algorithms I've posted in the past. Kind of interesting nevertheless.
Actionscript:
-
package {
-
import flash.display.Sprite;
-
public class Chaining extends Sprite{
-
public function Chaining(){
-
print(n(100).divide(2).plus(2));
-
// outputs 52
-
print(n(100).plus(n(10).multiply(2)));
-
// outputs 120
-
}
-
}
-
}
-
-
function print(n:Num):void{
-
trace(n.getValue());
-
}
-
function n(n:Number):Num{
-
return new Num(n);
-
}
-
-
class Num{
-
private var value:Number;
-
-
public function Num(n:Number):void{
-
value = n;
-
}
-
private function convert(n:*):Number{
-
if (n is Num) n = n.getValue();
-
return n;
-
}
-
public function getValue():Number{
-
return value;
-
}
-
public function plus(n:*):Num{
-
n = convert(n);
-
value += n;
-
return this;
-
}
-
public function minus(n:*):Num{
-
n = convert(n);
-
value -= n;
-
return this;
-
}
-
public function multiply(n:*):Num{
-
n = convert(n);
-
value *= n;
-
return this;
-
}
-
public function divide(n:*):Num{
-
n = convert(n);
-
value /= n;
-
return this;
-
}
-
}
This snippet is meant to be run as a document class. It shows how one might go about designing a class to make extensive use of function chaining.
Actionscript:
-
import com.adobe.images.JPGEncoder;
-
-
var jpgEncoder:JPGEncoder = new JPGEncoder(80);
-
var file:FileReference = new FileReference();
-
var id:int = 0;
-
-
var bit:BitmapData = new BitmapData(400, 400, false, 0x000000);
-
addChild(new Bitmap(bit));
-
-
stage.addEventListener(MouseEvent.CLICK, onClick);
-
function onClick(evt:MouseEvent):void{
-
// draw some perlin noise
-
bit.perlinNoise(200,200, 2, Math.random()*100, true, false,0, true);
-
//bit.draw(someClip);
-
//bit.draw(someVideo);
-
id++;
-
file.save(jpgEncoder.encode(bit), "image_" +id+".jpg");
-
}
This snippet uses the as3corelib which you can download on this page. It uses the JPGEncoder class to save a jpeg image to a users HD. It also makes use of the flash player 10 FileReference.save() method.
I used BitmapData.perlinNoise() to quickly create an arbitrary image.... as the code comments suggest you could easily snapshot a video or other DisplayObject.