dimanche 4 décembre 2016

Saving customized Sprite as an image to local disk

I have a problem with my Action Script 3 code. I am to let the user to draw whatever he/she wants in spBoard and then I want to make him/her able to save it in the local disk. this is the code but this code can only save the spBoard without anything drown by the user in it.

import com.adobe.images.JPGEncoder;
import com.adobe.images.PNGEncoder;

 var lineSize: Number = 7;
 var currentColor: Number;

 var spBoard: Sprite = new Sprite();
 this.addChild(spBoard);
 spBoard.x = 280;
 spBoard.y = 23;
 drawBoard();

 var shDrawing: Shape = new Shape();
 this.addChild(shDrawing);
 shDrawing.x = 280;
 shDrawing.y = 23;

 var shMask: Shape = new Shape();
 this.addChild(shMask);
 shMask.x = 280;
 shMask.y = 23;
 drawMask();
 shDrawing.mask = shMask;
 var doDraw: Boolean = false;

 function drawBoard(): void {
 spBoard.graphics.lineStyle(1, 0x000000);
 spBoard.graphics.beginFill(0xFFFFFF);
 spBoard.graphics.drawRect(0, 0, 250, 250);
 spBoard.graphics.endFill();
 spBoard.filters = [new DropShadowFilter()];
 }

function drawMask(): void {
shMask.graphics.lineStyle(1, 0x000000);
shMask.graphics.beginFill(0xFFFFFF);
shMask.graphics.drawRect(1, 1, 249, 249);
shMask.graphics.endFill();
}


spBoard.addEventListener(MouseEvent.MOUSE_DOWN, boardDown);
spBoard.addEventListener(MouseEvent.MOUSE_UP, boardUp);

function boardDown(e: MouseEvent): void {
var curX: Number = shDrawing.mouseX;
var curY: Number = shDrawing.mouseY;
doDraw = true;
shDrawing.graphics.lineStyle(5, 0x000066);

shDrawing.graphics.lineStyle(lineSize, currentColor);
shDrawing.graphics.moveTo(curX, curY);
}

function boardUp(e: MouseEvent): void {
doDraw = false;
}

spBoard.addEventListener(MouseEvent.MOUSE_MOVE, boardMove);

function boardMove(e: MouseEvent): void {
var curX: Number = shDrawing.mouseX;
var curY: Number = shDrawing.mouseY;
if (doDraw) {
    shDrawing.graphics.lineTo(curX, curY);
}
e.updateAfterEvent();
}


save_image.addEventListener(MouseEvent.CLICK, save_image_function);
function save_image_function(event:MouseEvent):void
{
var bmd:BitmapData = new BitmapData(50,50);
bmd.draw(spBoard);
var encorder:PNGEncoder   = new PNGEncoder();
var bytes:ByteArray = PNGEncoder.encode( bmd );
var file:FileReference    = new FileReference();
file.save( bytes, "Image.png" );
}

Aucun commentaire:

Enregistrer un commentaire