In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

FillBitmap

Bitmap#scale9Grid をパターン画像で塗りつぶす FillBitmap のテストです。

9スライス指定した画像の四隅を固定表示して、残りの可変部分を bitmapFillRect のようにリピートで埋めます。

http://d.hatena.ne.jp/nenjiru/20110824/fillbitmap
Get Adobe Flash player
by nenjiru 24 Aug 2011
    Embed
/**
 * Copyright nenjiru ( http://wonderfl.net/user/nenjiru )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/mnCc
 */

package {
    import flash.display.Shape;
    import flash.display.Bitmap;
    import flash.display.Loader;
    import flash.display.LoaderInfo;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.geom.Rectangle;
    import flash.net.URLRequest;
    import flash.system.LoaderContext;
    
    /**
     * ...
     * @author nenjiru
     */
    public class Main extends Sprite
    {
        
        private static const URL:String = "http://assets.wonderfl.net/images/related_images/9/9b/9bb0/9bb0eaaa9f3d3eecab329d418ef61febadb1ba11";
        
        public function Main()
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
           
            var loader:Loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE , complete);
            loader.load(new URLRequest(URL), new LoaderContext(true));
        }
        
        private function complete(event:Event):void 
        {
            var loaderInfo:LoaderInfo = event.target as LoaderInfo;
            loaderInfo.removeEventListener(Event.COMPLETE , complete);
            var bitmap:Bitmap = loaderInfo.content as Bitmap;
            
            var bmp:FillBitmap = new FillBitmap(bitmap.bitmapData, new Rectangle(6, 6, 12, 12));
            bmp.x = 12;
            bmp.y = 12;
            bmp.setSize(90, 90);
            addChild(bmp);
            
            var bmp2:FillBitmap = new FillBitmap(bitmap.bitmapData, new Rectangle(6, 6, 12, 12));
            bmp2.x = 12;
            bmp2.y = 108;
            bmp2.setSize(90, 270);
            addChild(bmp2);
            
            var bmp3:FillBitmap = new FillBitmap(bitmap.bitmapData, new Rectangle(6, 6, 12, 12));
            bmp3.x = 108;
            bmp3.y = 12;
            bmp3.setSize(348, 366);
            addChild(bmp3);
            
            var bmp4:FillBitmap = new FillBitmap(bitmap.bitmapData, new Rectangle(6, 6, 12, 12));
            bmp4.x = 12;
            bmp4.y = 384;
            bmp4.setSize(444, 72);
            addChild(bmp4);
        }
    }
}

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;

/**
 * ...
 * @author nenjiru
 */
class FillBitmap extends Bitmap 
{
    protected var _source:BitmapData;
    protected var _scale9Grid:Rectangle;
    
    override public function get scale9Grid():Rectangle 
    {
        return _scale9Grid;
    }
    
    override public function set scale9Grid(value:Rectangle):void 
    {
        _scale9Grid = value.clone();
    }
    
    override public function get bitmapData():BitmapData 
    {
        return super.bitmapData;
    }
    
    override public function set bitmapData(value:BitmapData):void 
    {
        var w:Number = super.width;
        var h:Number = super.height;
        _source.dispose();
        _source = super.bitmapData  = value.clone();
        setSize(w, h);
    }
    
    override public function set width(value:Number):void 
    {
        if (width != value)
        {
            setSize(value, height);
        }
    }
    
    override public function set height(value:Number):void 
    {
        if (height != value)
        {
            setSize(width, value);
        }
    }
    
    public function FillBitmap(bitmapData:BitmapData, scale9Grid:Rectangle = null) 
    {
        _source = bitmapData.clone();
        if (scale9Grid)
        {
            _scale9Grid = scale9Grid.clone();
        }
        super(bitmapData);
    }
    
    public function setSize(width:Number, height:Number):void 
    {
        if (_scale9Grid)
        {
            _boundChange(Math.max(_source.width, width), Math.max(_source.height, height))
        }
        else
        {
            super.width = width;
            super.height = height;
        }
    }
    
    private function _boundChange(width:Number, height:Number):void 
    {
        super.bitmapData = new BitmapData(width, height);
        
        var inner:Rectangle = new Rectangle(_scale9Grid.x, _scale9Grid.y, width, height);
        inner.width -= _source.width - _scale9Grid.width;
        inner.height -= _source.height - _scale9Grid.height;
        
        var fillX:Array = [0, inner.left, inner.right];
        var fillY:Array = [0, inner.top, inner.bottom];
        var fillW:Array = [inner.left, inner.width, inner.right];
        var fillH:Array = [inner.top, inner.height, inner.bottom];
        
        var cropX:Array = [0, _scale9Grid.left, _scale9Grid.right];
        var cropY:Array = [0, _scale9Grid.top, _scale9Grid.bottom];
        var cropW:Array = [_scale9Grid.left, _scale9Grid.width, _scale9Grid.right];
        var cropH:Array = [_scale9Grid.top, _scale9Grid.height, _scale9Grid.bottom];
        
        for (var j:int = 0; j < 3; j++)
        {
            for (var i:int = 0; i < 3; i++)
            {
                _fillRect(fillW[i], fillH[j], new Point(fillX[i], fillY[j]), new Rectangle(cropX[i], cropY[j], cropW[i], cropH[j]));
            }
        }
    }
    
    private function _fillRect(width:Number, height:Number, destPoint:Point, sourceRect:Rectangle):void 
    {
        var point:Point = destPoint.clone();
        var clip:Rectangle = sourceRect.clone();
        var row:Number = height / sourceRect.height;
        var col:Number = width / sourceRect.width;
        
        for (var i:int = 0; i < row; i++) 
        {
            point.x = destPoint.x;
            clip.width = sourceRect.width;
            if ((row - i) < 1)
            {
                clip.height = height %= sourceRect.height;
            }
            
            for (var j:int = 0; j < col; j++) 
            {
                if ((col - j) < 1)
                {
                    clip.width = width %= sourceRect.width;
                }
                
                bitmapData.copyPixels(_source, clip, point);
                point.x += sourceRect.width;
            }
            point.y += sourceRect.height;
        }
    }
}