Código ActionScript :
Stage.scaleMode = 'Scale';
/*
This examples shows how bitmaps can be used to erase or redraw
another bitmap using its alpha channel.
*/
var tool; // used to determine the current tool
var toolsize = 40; // size used for tools (width = height)
var tooloffset = toolsize/2; // offset for cursor placement in using tool
var basepoint = new flash.geom.Point(0, 0); // 0,0 point
// two bitmaps are used with the image being manipulated
// the first, origbmp contains the original image, unaltered
// the second, drawbmp is the working image seen and
// altered on the screen
var origbmp = flash.display.BitmapData.loadBitmap("dragon_bmp");
var drawbmp = new flash.display.BitmapData(origbmp.width, origbmp.height, true, 0);
// drawbmp starts with a copy of the original
drawbmp.draw(origbmp);
// two bitmaps are used for the tools, one for erasing
// and one for redrawing, both same size and settings
var erasebmp = new flash.display.BitmapData(toolsize, toolsize, true, 0);
var redrawbmp = erasebmp.clone();
// create a movie clip, image_mc, to hold the
// image being erased and redrawn
this.createEmptyMovieClip("image_mc", 1);
// position the image to the left
image_mc._x = 0;
// attach the drawing bitmap data object to image_mc
image_mc.attachBitmap(drawbmp, 1);
// -------------------------------------
// TOOLS
// select erase tool
erase_btn.onPress = function(){
// tell button movie clips to navigate to appropriate state
this.gotoAndStop(2);
redraw_btn.gotoAndStop(1);
// assign tool to equal "erase"
// this is checked when drawing starts
tool = "erase";
}
// select redraw tool
redraw_btn.onPress = function(){
// tell button movie clips to navigate to appropriate state
this.gotoAndStop(2);
erase_btn.gotoAndStop(1);
// assign tool to equal "redraw"
// this is checked when drawing starts
tool = "redraw";
}
// -------------------------------------
// BRUSHES
// select circle brush
circle_btn.onPress = function(){
// tell button movie clips to navigate to appropriate state
this.gotoAndStop(2);
line_btn.gotoAndStop(1);
// call setToolShape with circle_mc to set
// the tool(s) shape(s) to a circle
setToolShape(circle_mc);
}
// select line brush
line_btn.onPress = function(){
// tell button movie clips to navigate to appropriate state
this.gotoAndStop(2);
circle_btn.gotoAndStop(1);
// call setToolShape with circle_mc to set
// the tool(s) shape(s) to a line
setToolShape(line_mc);
}
function setToolShape(shape_mc){
// erasebmp needs to be transparent where it is supposed to
// be erasing drawbmp. First fill it with solid white
erasebmp.fillRect(erasebmp.rectangle, 0xFFFFFFFF);
// then draw the shape (black) into the bitmap
erasebmp.draw(shape_mc);
// copyChannel is used to convert the shape of the tool
// just added into erasebmp into its alpha channel
erasebmp.copyChannel(erasebmp, erasebmp.rectangle, basepoint, 1, 8);
// redrawbmp needs to be solid where it redraws drawbmp
// fill redrawbmp with invisible black (0x00000000)
redrawbmp.fillRect(redrawbmp.rectangle, 0);
// add the shape (solid color)
redrawbmp.draw(shape_mc);
}
// -------------------------------------
// WORKING WITH IMAGE
// pressing image to erase or redraw
image_mc.onPress = function(){
// depending on selected tool, assign onMouseMove event handler
if (tool == "erase") this.onMouseMove = useEraserMouseMove;
else if (tool == "redraw") this.onMouseMove = useRedrawMouseMove;
// initiate onMouseMove handler to work with onPress
this.onMouseMove();
}
// releasing after erase or redraw
image_mc.onRelease = image_mc.onReleaseOutside = function(){
// remove onMouseMove handler
delete this.onMouseMove;
}
// onMouseMove event handler for erasing
function useEraserMouseMove(){
// devise an offset based on the mouse's
// position in image_mc and tooloffset
var offset = new flash.geom.Point(image_mc._xmouse - tooloffset, image_mc._ymouse - tooloffset);
// devise a drawing rectangle the size of the
// the drawing tools at the offset's location
var drawRect = new flash.geom.Rectangle(offset.x, offset.y, toolsize, toolsize);
// next, copy pixels from the drawbmp back onto itself
// (makes for no change) but use erasebmp as the alphaBitmap
// this will add the alpha channel information from erasebmp
// into drawbmp erasing pixels from drawbmp in the shape of erasebmp
drawbmp.copyPixels(drawbmp, drawRect, offset, erasebmp, basepoint, false);
// update after each mouse movement
updateAfterEvent();
}
// onMouseMove event handler for redrawing
function useRedrawMouseMove(){
// devise an offset based on the mouse's
// position in image_mc and tooloffset
var offset = new flash.geom.Point(image_mc._xmouse - tooloffset, image_mc._ymouse - tooloffset);
// devise a drawing rectangle the size of the
// the drawing tools at the offset's location
var drawRect = new flash.geom.Rectangle(offset.x, offset.y, toolsize, toolsize);
// next, copy pixels from the original bitmap back
// onto drawbmp to update the image. redrawbmp is
// used as the alphaBitmap to give the tool shape
drawbmp.copyPixels(origbmp, drawRect, offset, redrawbmp, basepoint, true);
// update after each mouse movement
updateAfterEvent();
}
// -------------------------------------
// initialize
erase_btn.onPress(); // start with erase tool
circle_btn.onPress(); // start with circle brush
