Skip to content

Instantly share code, notes, and snippets.

@rockinghelvetica
Last active October 3, 2022 22:31
Show Gist options
  • Save rockinghelvetica/b8158c3fde10f4692669f30e24e0e883 to your computer and use it in GitHub Desktop.
Save rockinghelvetica/b8158c3fde10f4692669f30e24e0e883 to your computer and use it in GitHub Desktop.
Old ActionScript code for Dancejam header effect
//Regarding https://twitter.com/nicholasmacias/status/1577057407112314881
import DJNavigationControl;
import DJNavigationModel;
import flash.display.BitmapData;
import flash.geom.Matrix;
import flash.geom.ColorTransform;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.filters.DropShadowFilter;
import flash.geom.Transform;
class DJNavigation extends MovieClip {
//------ Pretending we have constants.
public static var WIDTH:Number = 900;
public static var UNIT:Number = 2;
public static var SPACING:Number = 3;
public static var CONTROL_SPACING:Number = 10;
public static var COLOR_STATIC:Number = 0xFFFFFFFF;
public static var COLOR_HOVER:Number = 0xffff5a60;
public static var COLOR_ACTIVE:Number = 0xFFCC0000;
//------ Instance variables.
public var background:MovieClip;
public var effects:MovieClip;
public var grid:MovieClip;
public var controls:MovieClip;
public var info:DJNavigationModel;
public var colors:Array;
public var fx_transform:Transform;
public var fx_color_transform:ColorTransform;
public var swatch_alpha:BitmapData;
public var swatch_grid:BitmapData;
public var controls_offset:Number;
public var filter:DropShadowFilter;
//------ Stubs for making nice with the compiler.
public var addListener:Function;
public var removeListener:Function;
public var broadcastMessage:Function;
public function DJNavigation() {
//------ Dispatching to for state management. Eventually...
AsBroadcaster.initialize(this);
//------ Default color settings.
this.colors = new Array(0xFF5d5b52, 0xFF29281d);
this.filter = new DropShadowFilter(0, 0, 0x000000, 1.0, 5, 5, 1, 3, false, false, false);
}
public function onLoad():Void {
Stage.align = "TL";
Stage.scaleMode = "noScale";
Stage.addListener(this);
this.info = new DJNavigationModel();
//------ This should be deferred if the data isn't immediately returned.
this.setupViews();
}
public function setupViews():Void {
//------ Create a sprite for each of the layers.
this.background = this.createEmptyMovieClip("background_mc", 0);
this.background.cacheAsBitmap = true;
this.grid = this.createEmptyMovieClip("grid_mc", 2);
this.grid.cacheAsBitmap = true;
this.controls = this.createEmptyMovieClip("controls_mc", 1);
this.controls.cacheAsBitmap = true;
this.effects = this.createEmptyMovieClip("effects_mc", 3);
this.effects.cacheAsBitmap = true;
//------ Setup the visual properties of the hover state layer.
this.fx_transform = new Transform(this.effects);
this.fx_color_transform = new ColorTransform();
this.fx_color_transform.rgb = DJNavigation.COLOR_HOVER;
this.fx_transform.colorTransform = this.fx_color_transform;
this.effects.blendMode = 8;
this.controls.filters = [this.filter];
//------ Generate the bitmaps and fills.
this.createSwatches();
this.onResize();
//------ Setup the mouse trail. An 8-bit mask only works if cacheAsBitmap is true for both clips.
this.attachMovie("Focus", "focus_mc", 100);
this["focus_mc"].cacheAsBitmap = true;
this.effects.setMask(this["focus_mc"]);
this.onMouseMove = function() {
this["focus_mc"]._x = this._xmouse;
this["focus_mc"]._y = this._ymouse;
}
//------ Instantiate all the controls.
this.controls_offset = 0;
var t:String;
var i:Number;
for(i = 0; i < this.info.items.length; i++) {
t = this.info.items[i].title;
//------ Attach the control instance & setup/position.
this.controls.attachMovie("DJNavigationControl", t, this.controls.getNextHighestDepth(), { controller: this });
this.controls[t].label = t;
this.controls[t]._x = this.controls_offset;
this.controls_offset += this.controls[t].bounds.width + this.controls[t].bounds.x + DJNavigation.CONTROL_SPACING;
}
//------ Rag right.
this.effects._x = this.controls._x = DJNavigation.WIDTH - this.controls_offset;
}
public function createSwatches():Void {
//------ TO DO: Technically this means generating from the grid values but we cheat for now.
this.swatch_alpha = BitmapData.loadBitmap("grid_pattern");
}
public function onResize():Void {
var w:Number = Stage.width;
var h:Number = Stage.height;
var m:Number;
//------ Anchor to page left (based on fixed width design).
this._x = Math.floor(Stage.width/2 - DJNavigation.WIDTH/2);
//------ Adjust the backgrounds to cover the margin, but maintain the grid alignment.
m = this._x % DJNavigation.SPACING;
this.background._x = 0 - this._x;
this.grid._x = 0 - this._x + m;
//------ Draw background gradient.
var gradient_matrix:Matrix = new Matrix();
gradient_matrix.createGradientBox(w, h, Math.PI/2, 0, 0);
this.background.clear();
this.background.beginGradientFill("linear", this.colors, [100,100], [0,215], gradient_matrix);
this.background.moveTo(0, 0);
this.background.lineTo(w, 0);
this.background.lineTo(w, h);
this.background.lineTo(0, h);
this.background.lineTo(0, 0);
this.background.endFill();
//------ Toss the old grid data.
this.swatch_grid.dispose();
//------ Copy the gradient fill to a new texture using a multiply operation.
this.swatch_grid = new BitmapData(w, h, true, 0xffcacaca);
this.swatch_grid.draw(this.background, new Matrix(), new ColorTransform(), 3);
//------ Flood the grid clip with the mask pattern.
this.grid.clear();
this.grid.beginBitmapFill(this.swatch_alpha, new Matrix(), true);
this.grid.moveTo(0, 0);
this.grid.lineTo(w, 0);
this.grid.lineTo(w, h);
this.grid.lineTo(0, h);
this.grid.lineTo(0, 0);
this.grid.endFill();
//------ Flatten down as an alpha mask.
this.swatch_grid.draw(this.grid, new Matrix(), new ColorTransform(), 11);
//------ Clear for good measure, then put texture on screen.
this.grid.clear();
this.grid.attachBitmap(this.swatch_grid, this.getNextHighestDepth());
}
}
//Regarding https://twitter.com/nicholasmacias/status/1577057407112314881
import DJNavigation;
import flash.display.BitmapData;
import flash.filters.BlurFilter;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.geom.ColorTransform;
import flash.geom.Transform;
import mx.transitions.Tween;
import mx.transitions.easing.Strong;
class DJNavigationControl extends MovieClip {
//------ Pretend constants.
public static var FILTER:BlurFilter = new BlurFilter(4, 4, 3);;
//------ Shared instance.
public static var factory:MovieClip;
public static var formatting:TextFormat;
public static var color_deltas:Object;
//------ Instance variables.
private var _label:String;
public var texture:BitmapData;
public var hover_texture:BitmapData;
public var hover_transform:Transform;
public var hover_color:ColorTransform;
public var effect:BitmapData;
public var bounds:Rectangle;
public var hotspot:MovieClip;
public var controller:DJNavigation;
public var hover_tween:Tween;
public var opacity_tween:Tween;
public function DJNavigationControl() {
//------ Create color objects for use in transition effects.
this.hover_transform = new Transform(this);
this.hover_color = new ColorTransform();
this.hover_color.rgb = DJNavigation.COLOR_HOVER;
this.hover_transform.colorTransform = this.hover_color;
this.hover_tween = new Tween(this, "transition", Strong.easeOut, 0, 0, 16, false);
this.opacity_tween = new Tween(this, "_alpha", Strong.easeOut, 10, 100, 24, false);
//------ The first instance caches the hover state's RGB component values.
if(DJNavigationControl.color_deltas == undefined) {
//------ Unwind this if you need to set hover colors independently.
DJNavigationControl.color_deltas = this.getRGBDeltas(DJNavigation.COLOR_HOVER);
}
}
public function onLoad():Void {
//------ The hit area is dynamically created based on the label size.
this.hotspot = this.createEmptyMovieClip("hotspot_mc", 0);
this.hotspot._visible = false;
this.hitArea = this.hotspot;
this.label = this._label;
//------ FIX: set via infoObject. Might want to make less fragile?
this.controller.addListener(this);
//------ FIX: Install events; move into enable/disable methods.
this.onRollOver = this.onDragOver = function() {
//------ FIX: And of course, don't directly manipulate the controller this way.
this.controller.broadcastMessage("onHover", this);
}
this.onRollOut = this.onDragOut = function() {
this.controller.broadcastMessage("onRestore", this);
}
}
public function set transition(completion:Number):Void {
var rgb:Object = DJNavigationControl.color_deltas;
var r:Number = 255 - (rgb.r * completion);
var g:Number = 255 - (rgb.g * completion);
var b:Number = 255 - (rgb.b * completion);
var filter = DJNavigationControl.FILTER.clone();
//------ Find the current gradient and apply the interpolated color.
this.hover_color.rgb = (r << 16) + (g << 8) + b;
this.hover_transform.colorTransform = this.hover_color;
//------ Setup the blur effect and set instance opacity, each with interpolation.
filter.blurX = filter.blurY = DJNavigationControl.FILTER.blurX*completion;
this.controller.effects[this._name]._alpha = completion * 100;
//------ This is where the bitmap gets drawn.
this.hover_texture.applyFilter(this.texture, this.bounds, new Point(0, 0), filter);
}
public function getRGBDeltas(hex:Number):Object {
//------ Break the color difference (from white) into RGB components for animation.
var r:Number = (hex & 0xFF0000) >> 16;
var g:Number = (hex & 0x00FF00) >> 8;
var b:Number = hex & 0x0000FF;
return {
r: 255-r,
g: 255-g,
b: 255-b
};
}
public function onRestore():Void {
this.hover_tween.continueTo(0);
this.opacity_tween.continueTo(100);
}
public function onHover(mc:MovieClip):Void {
if(mc == this) {
this.opacity_tween.continueTo(100);
this.hover_tween.continueTo(1.0);
return;
}
this.opacity_tween.continueTo(70);
}
public function installHoverTexture():Void {
//------ Error if controller reference is unavailable.
if(!this.controller.effects) return;
if(typeof this.controller.effects[this._name] != "movieclip") {
//------ Create a new movieclip in the effects layer, using the same name.
this.controller.effects.createEmptyMovieClip(this._name, this.controller.effects.getNextHighestDepth());
this.controller.effects[this._name].cacheAsBitmap = true;
}
else {
//------ Release any old bitmaps.
this.hover_texture.dispose();
}
//------ Enlarge the bounds to fit the effect.
var r = this.texture.generateFilterRect(this.bounds, DJNavigationControl.FILTER);
//------ Create a new hover texture, attach and align.
this.hover_texture = new BitmapData(r.width, r.height, true, 0x00000000);
this.controller.effects[this._name].attachBitmap(this.hover_texture, 0);
this.controller.effects[this._name]._x = this._x + this.bounds.x;
this.controller.effects[this._name]._y = this._y + this.bounds.y;
}
public function set label(label:String):Void {
var factory:MovieClip;
//------ Toss the old data if present.
this.texture.dispose();
//------ Set factory string and draw into a new texture;
factory = DJNavigationControl.getFactoryInstance();
factory.texture_tf.text = this._label = label;
this.texture = new BitmapData(300, Stage.height, true, 0x00000000);
this.texture.draw(factory);
this.attachBitmap(this.texture, this.getNextHighestDepth());
//------ Measure the rendered text area (as getTextExtents is broken).
this.bounds = this.texture.getColorBoundsRect(0xff000000, 0x00000000, false);
//------ Setup a bitmap copy in the effects layer.
this.installHoverTexture();
//------ Create hit area.
this.hotspot.clear();
this.hotspot.beginFill(0x00000000);
this.hotspot.moveTo(bounds.x, 0);
this.hotspot.lineTo(bounds.x, Stage.height);
this.hotspot.lineTo(bounds.x + bounds.width, Stage.height);
this.hotspot.lineTo(bounds.x + bounds.width, 0);
this.hotspot.lineTo(bounds.x, 0);
this.hotspot.endFill();
}
public static function getFactoryInstance():MovieClip {
if(typeof DJNavigationControl.factory == "movieclip") return DJNavigationControl.factory;
//------ Attach the rendering textfield offstage.
DJNavigationControl.factory = _root.attachMovie("NavTextFactory", "factory_mc", _root.getNextHighestDepth());
DJNavigationControl.factory._visible = false;
return DJNavigationControl.factory;
}
}
class DJNavigationInfo {
public var title:String;
public var url:String;
public function DJNavigationInfo(title:String, url:String) {
//------ Utility to keep our data neat & paired. Strip spaces in titles?
this.title = title;
this.url = url
}
}
import DJNavigation;
import DJNavigationInfo;
class DJNavigationModel extends Object {
public var controller:DJNavigation;
public var current:Number;
public var items:Array;
function DJNavigationModel(controller:DJNavigation) {
//------ To facilitate initialization/update notification.
this.controller = controller;
this.initData();
}
function initData():Void {
//------ This can be embedded as FlashVars, compiled in a SWF or sent as XML.
this.items = new Array();
//------ For now, we're cheating; it's just fixed in.
this.items[0] = new DJNavigationInfo("people","");
this.items[1] = new DJNavigationInfo("battles","");
this.items[2] = new DJNavigationInfo("hot","");
this.items[3] = new DJNavigationInfo("freshness","");
this.items[4] = new DJNavigationInfo("hyphy","");
}
}
@rockinghelvetica
Copy link
Author

There was also a class for the disco ball mirror logo effect but some tricks can never be revealed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment