Skip to content

Instantly share code, notes, and snippets.

@nadako
Created May 7, 2013 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nadako/5531775 to your computer and use it in GitHub Desktop.
Save nadako/5531775 to your computer and use it in GitHub Desktop.
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.BlendMode;
import flash.display.DisplayObject;
import flash.display.GradientType;
import flash.display.IBitmapDrawable;
import flash.display.MovieClip;
import flash.display.Shape;
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Matrix;
import flash.net.URLRequest;
import flash.net.navigateToURL;
import flash.ui.Mouse;
public class OKBanner extends Sprite
{
private static const RADIUS:Number = 20;
private static const ERASE_THRESHOLD:Number = 0.6;
private static const TARGET_URL:String = "http://asdf.com/";
private var _button:SimpleButton;
private var _brush:MovieClip;
private var _gloom:BitmapData;
private var _clearCircle:IBitmapDrawable;
private var _drawMatrix:Matrix = new Matrix();
private var _baseGloomAlpha:Number;
public function OKBanner()
{
super();
if (stage != null)
onAddedToStage();
else
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(event:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
setupGloom();
setupBrush();
setupButton();
}
private function setupGloom():void
{
var gloom:DisplayObject = getChildByName("gloom");
_gloom = new BitmapData(gloom.width, gloom.height, true, 0);
_gloom.draw(gloom);
_baseGloomAlpha = _gloom.histogram()[3][255];
addChildAt(new Bitmap(_gloom), getChildIndex(gloom));
removeChild(gloom);
}
private function setupBrush():void
{
Mouse.hide();
_brush = getChildByName("brush") as MovieClip;
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
_brush.mouseEnabled = _brush.mouseChildren = false;
_drawMatrix.createGradientBox(RADIUS * 2, RADIUS * 2);
var circle:Shape = new Shape();
circle.graphics.beginGradientFill(GradientType.RADIAL, [0, 0], [1, 0], [175, 255], _drawMatrix);
circle.graphics.drawCircle(RADIUS, RADIUS, RADIUS);
circle.graphics.endFill();
_clearCircle = circle;
}
private function setupButton():void
{
_button = getChildByName("button") as SimpleButton;
_button.visible = false;
_button.addEventListener(MouseEvent.ROLL_OVER, onButtonRollOver);
_button.addEventListener(MouseEvent.ROLL_OUT, onButtonRollOut);
_button.addEventListener(MouseEvent.CLICK, onButtonClick);
}
private function onButtonRollOver(event:MouseEvent):void
{
Mouse.show();
_brush.visible = false;
}
private function onButtonRollOut(event:MouseEvent):void
{
Mouse.hide();
_brush.visible = true;
}
private function onButtonClick(event:MouseEvent):void
{
navigateToURL(new URLRequest(TARGET_URL));
}
private function onMouseMove(event:MouseEvent):void
{
_brush.x = event.stageX;
_brush.y = event.stageY;
if (event.buttonDown)
erase();
}
private function onMouseDown(event:MouseEvent):void
{
this.getChildByName("orders").visible = false;
erase();
}
private function erase():void
{
_drawMatrix.identity();
_drawMatrix.translate(stage.mouseX - RADIUS, stage.mouseY - RADIUS);
_gloom.draw(_clearCircle, _drawMatrix, null, BlendMode.ERASE);
var value:Number = _gloom.histogram()[3][255] / _baseGloomAlpha;
if (value <= ERASE_THRESHOLD)
complete();
}
private function complete():void
{
_button.visible = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment