Skip to content

Instantly share code, notes, and snippets.

@tmshv
Created November 6, 2012 16:21
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 tmshv/4025783 to your computer and use it in GitHub Desktop.
Save tmshv/4025783 to your computer and use it in GitHub Desktop.
Image Action Sctipt Class
package ru.gotoandstop.display{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.BlendMode;
import flash.display.IBitmapDrawable;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Rectangle;
/**
*
* @author Roman Timashev (roman@tmshv.ru), @tmshv
**/
public class Image extends Bitmap{
public static const VERSION:String = '2.2.0';
public function Image(bitmapData:BitmapData=null, pixelSnapping:String='auto', smoothing:Boolean=false){
super(bitmapData, pixelSnapping, smoothing);
}
public function draw(source:IBitmapDrawable, matrix:Matrix=null, colorTransform:ColorTransform=null, blendMode:String=null, clipRect:Rectangle=null, smoothing:Boolean=false):void{
if(!matrix) matrix = new Matrix();
if(source['width'] >= 2880 || source['height'] >= 2880){
const ratio_width:Number = 2880 / source['width'];
const ratio_height:Number = 2880 / source['height'];
const scale:Number = (ratio_width<=ratio_height) ? ratio_width : ratio_height;
matrix.a = scale;
matrix.d = scale;
}
if(!super.bitmapData){
const bmp_width:uint = source['width'] * matrix.a;
const bmp_height:uint = source['height'] * matrix.d;
super.bitmapData = new BitmapData(bmp_width, bmp_height, true);
}
super.bitmapData.draw(source, matrix, colorTransform, blendMode, clipRect, smoothing);
}
public function stretch(width:uint, height:uint, offset:int=-1, colorTransform:ColorTransform=null, smothing:Boolean=false):void{
if(!super.bitmapData) return;
var current_width:Number = super.bitmapData.width;
var current_height:Number = super.bitmapData.height;
var ratio_width:Number = width / current_width;
var ratio_height:Number = height / current_height;
var greater_width:Boolean = ratio_width >= ratio_height
var new_width:Number = greater_width ? current_width : (width/height * current_height);
var new_height:Number = !greater_width ? current_height : (height/width * current_width);
var scale_width:Number = width / new_width;
var scale_height:Number = height / new_height;
var offset_x:Number = Math.round((new_width -current_width) * scale_width / 2);
var offset_y:Number = Math.round((new_height-current_height) * scale_height / 2);
var pos_x:Number = !offset_x ? 0 : (offset<0 ? offset_x : -offset);
var pos_y:Number = !offset_y ? 0 : (offset<0 ? offset_y : -offset);
var matrix:Matrix = new Matrix(scale_width, 0, 0, scale_height, pos_x, pos_y);
var clone:BitmapData = super.bitmapData.clone();
super.bitmapData.dispose();
super.bitmapData = new BitmapData(new_width*scale_width, new_height*scale_height, clone.transparent);
super.bitmapData.draw(clone, matrix, colorTransform, BlendMode.NORMAL, null, smoothing);
clone.dispose();
}
public function scale(width:uint, height:uint, colorTransform:ColorTransform=null, smothing:Boolean=false):void{
if(!super.bitmapData) return;
var current_width:Number = super.bitmapData.width;
var current_height:Number = super.bitmapData.height;
var ratio_width:Number = width / current_width;
var ratio_height:Number = height / current_height;
var greater_width:Boolean = ratio_width >= ratio_height;
var new_width:Number = !greater_width ? width : ratio_height*current_width;
var new_height:Number = greater_width ? height : ratio_width*current_height;
var scale_width:Number = new_width / current_width;
var scale_height:Number = new_height / current_height;
var matrix:Matrix = new Matrix(scale_width, 0, 0, scale_height, 0, 0);
var clone:BitmapData = super.bitmapData.clone();
super.bitmapData.dispose();
super.bitmapData = new BitmapData(new_width, new_height, clone.transparent);
super.bitmapData.draw(clone, matrix, colorTransform, BlendMode.NORMAL, null, smoothing);
clone.dispose();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment