Archive for April, 2008

Threadless Print of my Blog Header

My blog header image “Yawn” originally started life as a submission to the http://www.threadless.com t-shirt design contest.

Well, they actually seemed to like it, so its been printed. You can have a look at it here: http://www.threadless.com/product/1203/Yawn

ActionScript 3 Full Browser Tiled Background

ActionScript 3 Tiled Background class.

Simple class for creating a tiled background for your projects. Takes 3 arguments,
the first is either a bitmapData object or URL to an image, a Boolean indicating if you want to multiply blend your image. Which is useful if you
want to overlay the tile over other display objects. And finally, a string representing the repeat mode of your tile. Class also supports repeat modes, in the same vein as what CSS allows for.

Class Features

  • External Images or internal bitmap for tile
  • Get/set tile blend mode (Multiply)
  • CSS background repeat modes, ‘repeat’, ‘repeaty’, repeatx’, ‘norepeat’
  • TileFast Mode - a performance tile setting

Class is an extension of this class; http://blog.efnx.com/?p=25

View Example
Here is an example; http://www.noponies.com/dev/as3_tiled_bg/

Source Files Here are the examples source files; as3_tiled_bg

Dependencies None

Updates 05 July, 2008: Performance tuning.

Images courtesy http://www.lydiabradbury.com/

Class ActionScript

/* AS3
*Copyright 2008 noponies.
*/


package noponies.display {
  import flash.display.Bitmap;
  import flash.display.BitmapData;
  import flash.display.Loader;
  import flash.display.Sprite;
  import flash.display.Stage;
  import flash.display.StageAlign;
  import flash.display.StageScaleMode;
  import flash.display.Graphics
  import flash.events.Event;
  import flash.events.IOErrorEvent;
  import flash.net.URLRequest;
  import flash.display.BlendMode;
  import flash.system.Capabilities;
 
  /**
  * The NpTileBg class is for creating a tiled bitmap background for your projects. It attempts to emulate the way the CSS background property works in HTML documents. You can pass in an existing library
  * bitmap asset, or load an external bitmap asset at runtime.
  * @langversion ActionScript 3.0
  * @Flash Player 9.0.28.0
  * @author noponies - 2008
  *   @version 1.0
   */

  public class NpTileBg extends Sprite {

    //--------------------------------------
    // PRIVATE INSTANCE PROPERTIES
    //--------------------------------------
    private var bitmap:Bitmap = new Bitmap();
    private var stageInstance:Stage;
    private var bgBlendMode:Boolean = false;
    private var repeatDir:String;
    private var sg:Graphics
    private var performanceMode:Boolean = false
    private var obj:*
    //--------------------------------------
    // GETTERS/SETTERS
    //--------------------------------------
    //set a new tile image at runtime
    /**
    * @langversion ActionScript 3.0
    * @playerversion Flash 9
     *  Set a new image at runtime. Useful for changing background patterns say between sections of your site.
     *  @param newTile Either a URL to a bitmap or a bitmap object.
     *  @return void
     */

    public function set newBgTileImage(newTile:*):void {
      init(newTile);
    }
    /**
    * @langversion ActionScript 3.0
    * @playerversion Flash 9
     *  Get / Set the tiles blend mode. Which will make a tile semi transparent over another display object. Note, that performance can suffer when using this mode.
     *  @default false
     *  @return Boolean representing whether or not to enable MULTIPLY blendmode. A value of <code>true</code> enables MULTIPLY blendmode. A value of <code>false</code> disables MULTIPLY blendmode.
     */

    public function get tileBlendMode():Boolean {
      return bgBlendMode;
    }
    /**
    * @private
    */

    public function set tileBlendMode(toggleMode:Boolean):void {
      bgBlendMode = toggleMode;
      if (toggleMode) {
        this.blendMode=BlendMode.MULTIPLY;
      } else {
        this.blendMode=BlendMode.NORMAL;
      }
    }
    /**
    * @langversion ActionScript 3.0
    * @playerversion Flash 9
     *  Get / Set the tiles repeat mode. Operates similar to the CSS background repeat mode.
     *  @default "repeat"
     *  @return String representing tiles repeat mode. A value of <code>repeat</code> repeats x and y. A value of <code>repeatx</code> repeats on the x axis.
     *  A value of <code>repeaty</code> repeats on the y axis. A value of <code>norepeat</code> turns off repeating altogether.
     */

    public function get bgRepeat():String {
      return repeatDir
    }
    /**
    * @private
    */

    public function set bgRepeat(newRepeat:String):void {
      validateParams(newRepeat)
    }
   
    /**
    * @langversion ActionScript 3.0
    * @playerversion Flash 9
     *  Get / Set the tiles tiling drawing mode. In tileFastMode the class will not actually redraw the background with each resize event. Rather it will simply draw a massive
     *  background tile, that extends beyond the stage boundaries. This increases peformance when resizing but also increases memory footprint. Only use if you
     *  really need to have super smooth resize performance. Bit of a hack....
     *  <p>You can turn this property on or off as you see fit</p>
     *  @default false
     *  @return Boolean representing tileFastMode. A value of true will make the class draw a large background tiled image that is not resized with the browser. The default is to draw this
     *  image 1.25x the users screenResolutionX and screenResolutionY.
     */

    public function get tileFastMode():Boolean {
      return performanceMode
    }
    /**
    * @private
    */

    public function set tileFastMode(newPerformanceMode:Boolean):void {
      performanceMode = newPerformanceMode
    }

    //---------------------------------------
    // CONSTRUCTOR
    //---------------------------------------
    /**
     *  The NpTileBg class is for creating a tiled background for your projects. Takes 3 arguments, the first is either a bitmapData object or URL to an image,
     *  a boolean indicating if you want to multiply blend your image and finally a string representing the repeat direction of your background tile. Using the MULTIPLY mode can make performance suffer!
     *  <p>The two constructor arguments for <code>bgBlendMode</code> which sets the MULITPLY property of the tile and the <code>repeatDir</code> which sets the repeat direction of the tile are
     *  optional. If you do not pass either of these two parameters the class defaults to drawing the tile in non mulitplied repeat mode.</p>
     *  <p>Original Source http://blog.efnx.com/?p=25 Few Edits, to include a setter, and a public destroy method. Along, with a slight rejig of the way the class works to better support the new methods.</p>
     *  @param obj UnTyped, either Bitmap or a URL to an external image asset you would like to load.
     *  @param bgBlendMode Boolean represents whether or not to overlay the pattern over other display objects.
     *  @param repeatDir String representing what repeat pattern to use when repeating the tile. Operates in the same manner as a CSS background repeat. A value of <code>repeat</code> repeats x and y. A value of <code>repeatx</code> repeats on the x axis.
     *  A value of <code>repeaty</code> repeats on the y axis. A value of <code>norepeat</code> turns off repeating altogether.
     *  @see #tileFastMode
     *
     *  @example Demo of class constructor using tileFastMode which is set via the tileFastMode setter.
     *  <listing version="3.0">
     *  import noponies.display.NpTileBg;
     *  var bg:NpTileBg = new NpTileBg(new arrow(0, 0), true,"repeat");
     *  bg.tileFastMode = true
     *  addChild(bg)
     *  </listing>
     *
     * @example Demo of class constructor using library bitmap asset.
     *  <listing version="3.0">
     *  import noponies.display.NpTileBg;
     *  var bg:NpTileBg = new NpTileBg(new tileBitmap(0, 0),true,"repeatx");
     *  addChild(bg)
     *  </listing>
     *
     * @example Demo of class constructor using http bitmap asset.
     *  <listing version="3.0">
     *  import noponies.display.NpTileBg;
     *  var bg:NpTileBg = new NpTileBg("http://www.somesite.com/someimage.jpg",true,"repeat");
     *  addChild(bg)
     *  </listing>
     */


    public function NpTileBg(obj:*, bgBlendMode:Boolean=false, repeatDir:String = "repeat"){
     
      //error checking for repeat param
      validateParams(repeatDir)
      //set the all important blend mode
      if (bgBlendMode) {
        this.blendMode=BlendMode.MULTIPLY;
      }
      this.mouseEnabled= false;
      this.obj = obj
      addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
    }
   
    private function addedToStageHandler(event:Event):void{
      //set stage align and scale modes, could possibly test for this and throw and error, but it should
      //be common knowledge to set stage to no scale and top/left align
      stageInstance = stage;
      stageInstance.scaleMode = StageScaleMode.NO_SCALE;
      stageInstance.align = StageAlign.TOP_LEFT;
      stageInstance.addEventListener(Event.RESIZE, resize);
      stageInstance.addEventListener(Event.REMOVED_FROM_STAGE, killBg);
      sg = this.graphics
      init(obj);
    }
   
    //validate the passed in repeat modes.
    private function validateParams(repeatDirection:String):void{
      switch (repeatDirection) {
        case "repeat" :
        this.repeatDir = repeatDirection
        break;
        case  "repeaty" :
        this.repeatDir = repeatDirection
        break
        case  "repeatx" :
        this.repeatDir = repeatDirection
        case  "norepeat" :
        this.repeatDir = repeatDirection
        break
        default:
        throw new Error("Problem :The repeat parameter passed "+ repeatDirection+ " does not match the allowed repeat modes, which are: \"repeat\", \"repeatx\", \"repeaty\", \"norepeat\"");
      }
    }
    //--------------------------------------
    // PRIVATE INSTANCE METHODS
    //--------------------------------------
    private function init(obj):void {
      if (obj is BitmapData) {
        bitmap.bitmapData = obj;
        if(performanceMode){
          drawBigBg()
        }else{
          resize();
        }
      } else if (obj is String) {
        var url:URLRequest = new URLRequest(obj);
        var loader:Loader = new Loader();
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderComplete);
        loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loaderError);
        loader.load(url);
      } else {
        throw new Error("Problem : First parameter obj must be a string \"url path_to_image\" or bitmapData.");
      }
    }
    private function loaderComplete(event:Event):void {
      bitmap.bitmapData = Bitmap(event.target.content).bitmapData;
        if(performanceMode){
          drawBigBg()
        }else{
          resize();
        }
    }

    private function loaderError(event:Event):void {
      throw new Error("Problem : There was an error accessing the image file.");
    }
    private function drawBigBg():void{
      sg.clear()
      sg.beginBitmapFill(bitmap.bitmapData);
      sg.drawRect(0, 0, Capabilities.screenResolutionX*1.25, Capabilities.screenResolutionY*1.25);
      sg.endFill();
    }

    //--------------------------------------
    // MAIN RESIZE METHOD
    //--------------------------------------
    private function resize(event:Event = null):void {
      if(performanceMode){
        return;
      }
      switch (repeatDir) {
        case "repeat" :
          sg.clear()
          sg.beginBitmapFill(bitmap.bitmapData);
          sg.drawRect(0, 0, stageInstance.stageWidth, stageInstance.stageHeight);
          sg.endFill();
          break;
        case "repeatx" :
          sg.clear()
          sg.beginBitmapFill(bitmap.bitmapData);
          sg.drawRect(0, 0, stageInstance.stageWidth, bitmap.height);
          sg.endFill();
          break;
        case "repeaty" :
          sg.clear()
          sg.beginBitmapFill(bitmap.bitmapData);
          sg.drawRect(0, 0, bitmap.width, stageInstance.stageHeight);
          sg.endFill();
          break;
        case "norepeat" :
          sg.clear()
          sg.beginBitmapFill(bitmap.bitmapData);
          sg.drawRect(0, 0, bitmap.width, bitmap.height);
          sg.endFill();
          break;
        default :
          sg.clear()
          sg.beginBitmapFill(bitmap.bitmapData);
          sg.drawRect(0, 0, stageInstance.stageWidth, stageInstance.stageHeight);
          sg.endFill();
          break;
      }
      this.x = this.y = 0;
    }
    //--------------------------------------
    // PUBLIC INSTANCE METHODS
    //--------------------------------------
    /**
      * This function will remove any current tiles and remove the Event.RESIZE event listener. This function is called automatically when the class detects a
      * Event.REMOVED_FROM_STAGE event.
      * @return void
      */

    public function killBg(event:Event = null):void {
      stageInstance.removeEventListener(Event.RESIZE, resize);
      stageInstance.removeEventListener(Event.REMOVED_FROM_STAGE, killBg);
      bitmap.bitmapData.dispose();
    }
  }

}

ActionScript 3 Full Browser Background Video

An Full Browser Background Video Demonstration

Supports either progressive or streaming video files (streaming tested using Red 5 and FMS). The demo consists of 3 classes, the main NpVideoProvider Class handles acquiring video, a NetConnect Class handles all netConnection duties, and an event class handles all the necessary events. Originally I wrote a class to handle testing BandWidth on streaming servers, but I didn’t think people would want that. If I’m wrong, I can add that functionality back in?

Class allows for loading in seperate files. So it would be pretty simple to turn this into a video slideshow. Class also, dispatches progress events along with events for each relevant netStatus event.

Class Features.

    External Video files.
    Either .flv, or .mp4 (h264) files.
    Get / Set video looping.
    Dispatches Progress events.
    Get / Set a load point at which point video plays.
    Get / Set video source files.
    Play, Stop, Pause Methods.
    Get / Set Seek Points.
    Get / Set Sound volume.
    Get / Set Sound Pan.
    Get / Set Video Smoothing.
    Get Video MetaData.
    Get Video CuePoints.
    Definable Min scale size at which point the videos are clipped.
    Supports FullScreen Events.

The demo contains two examples, one similar to what is online and one much more basic version. When I have time I’ll finish off a 3rd demo for using the NpVideoProvider Class as the base for a FLV player.

Updates18 July: Minor update to progress handling, changed an int to an Number.

Class Finished 16 July, 2008

In this demo, I’m also using a tiled Background class, as a pattern overlay. This decreases performance notably. There is an option to turn this off in the demo.

View Example
Here is an example; http://www.noponies.com/dev/as3_fullscreen_bg_vid/

Source Files
Donate if you want these source files

Dependencies
None.