package { import flash.display.BitmapData; import flash.geom.Point; import flash.geom.Rectangle; /** * Attempt at making the most CPU efficient image scrolling class * * Usage: * Either set scrollX and scrollY directly, or use the utility methods below * * scroller.panToAngle(scrollX); * scroller.tiltToAngle(scrollY); * scroller.scroll(); * * This version uses 4 rects that allows for scrolling in both X and Y direction. * Revealed some very interesting speed limitations in the Flash plugin... * **/ public class ImageScroller { public var scrollX:Number = 0; public var scrollY:Number = 0; private var bdW:Number = 0; private var bdH:Number = 0; private var bd:BitmapData; // Original bitmap public var data:BitmapData; // offet bitmap private var rect:Rectangle; private var point:Point; private var scrollW:Number; private var scrollH:Number; public function ImageScroller(image:BitmapData) { // Store the bitmap bd = image; bdW = bd.width; bdH = bd.height; data = new BitmapData(bdW,bdH,false,0xff7777); // Instantiate objects here (since that's 'expensive') and rather update properties later rect = new Rectangle(); point = new Point(); } public function panToAngle(angle:Number):void { scrollX = Math.floor(bdW * (angle/360)); } public function tiltToAngle(angle:Number):void { scrollY = Math.floor(bdW * (angle/360)); } public function scroll():void { // make sure we are within bounds and check width scrollX = scrollX%bdW; scrollY = scrollY%bdH; scrollW = bdW-scrollX; scrollH = bdH-scrollY; // create the four rects point.x = scrollW; point.y = scrollH; rect.x = 0; rect.y = 0; rect.width = scrollX; rect.height = scrollY; data.copyPixels(bd, rect, point ); point.x = scrollW; point.y = 0; rect.x = 0; rect.y = scrollY; rect.width = scrollX; rect.height = scrollH; data.copyPixels(bd, rect, point ); point.x = 0; point.y = scrollH; rect.x = scrollX; rect.y = 0; rect.width = scrollW; rect.height = scrollY; data.copyPixels(bd, rect, point ); point.x = 0; point.y = 0; rect.x = scrollX; rect.y = scrollY; rect.width = scrollW; rect.height = scrollH; data.copyPixels(bd, rect, point ); } } }