zoukankan      html  css  js  c++  java
  • Alpha mask using Actionscript 3.0

    Have you ever tried to create an alpha mask using Actionscript 3.0?
    The thing is reachable with the method copyChannel of the BitmapData class.

    1. Load an external image
    2. Create a new instance of BitmapData (bitmap_data) and assign it to bitmapData property of the external bitmap
    3. Create a new instance of BitmapData (bitmap_data_copy) with same size of external bitmap
    4. Copy the pixels of external bitmap in the new instance bitmap_data_copy
    5. Use the dispose method on bitmap_data to free memory
    6. Create a mask form using a Shape
    7. Create a new instance of BitmapData (bitmap_data_shape) the same size of external image and use draw method over shape
    8. bitmap_data_copy uses the method copyChannel on bitmap_data_shape
    9. Give bitmap_data_copy to property bitmapData of external image

    package
    {
        import flash.display.*;
        import flash.events.*;
        import flash.net.*;
        import flash.geom.*;
        import flash.filters.*;
    
        public class Main extends MovieClip
        {
            private var loaded_bitmap:Bitmap;
            private var bitmap_data:BitmapData;
            private var bitmap_data_copy:BitmapData;
            private var bitmap_data_shape:BitmapData;
    
            private var shape:Shape;
    
            public function Main()
            {
                addEventListener(Event.ADDED_TO_STAGE,init);
                addEventListener(Event.REMOVED_FROM_STAGE,destroy);
            }
    
            private function init(evt:Event):void
            {
                removeEventListener(Event.ADDED_TO_STAGE,init);
    
                loadImage();
            }
    
            private function loadImage():void
            {
                var request:URLRequest=new URLRequest("tiger.jpg");
                var loader:Loader=new Loader();
                loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onImageLoaded);
                loader.load(request);
            }
    
            private function onImageLoaded(evt:Event):void
            {
                evt.target.removeEventListener(Event.COMPLETE,onImageLoaded);
    
                loaded_bitmap=evt.target.loader.content as Bitmap;
    
                createAlphaMask();
            }
    
            private function createAlphaMask():void
            {
                bitmap_data=loaded_bitmap.bitmapData;
                var w:Number=bitmap_data.width;
                var h:Number=bitmap_data.height;
    
                bitmap_data_copy=new BitmapData(w,h,true,0x00FFFFFF);
                bitmap_data_copy.copyPixels(bitmap_data,bitmap_data.rect,new Point());
                bitmap_data.dispose();
    
                shape=createMask(w,h);
                bitmap_data_shape=new BitmapData(w,h,true,0x00FFFFFF);
                bitmap_data_shape.draw(shape);
    
                bitmap_data_copy.copyChannel(bitmap_data_shape,bitmap_data_shape.rect,new Point(),BitmapDataChannel.ALPHA,BitmapDataChannel.ALPHA);
                loaded_bitmap.bitmapData=bitmap_data_copy;
                addChild(loaded_bitmap);
            }
    
            private function createMask(w:Number,h:Number):Shape
            {
                var new_shape:Shape=new Shape();
                new_shape.graphics.beginFill(0xFFFFFF);
                new_shape.graphics.drawEllipse(50,50,w-100,h-100);
                new_shape.graphics.endFill();
    
                var blur_filter:BlurFilter=new BlurFilter(50,50,BitmapFilterQuality.HIGH);
                new_shape.filters=[blur_filter];
                addChild(new_shape);
    
                return new_shape;
            }
    
            private function destroy(evt:Event):void
            {
                removeEventListener(Event.REMOVED_FROM_STAGE,destroy);
    
                bitmap_data_copy.dispose();
                bitmap_data_shape.dispose();
    
                removeChild(loaded_bitmap);
                removeChild(shape);
    
                loaded_bitmap=null;
                shape=null;
            }
        }
    }
    

  • 相关阅读:
    最长公共上升子序列
    最长公共子序列
    3847: Mowing the Lawn (单调队列)
    A/B(扩展欧几里得)
    One Person Game(扩展欧几里得)
    Substring with Concatenation of All Words, 返回字符串中包含字符串数组所有字符串元素连接而成的字串的位置
    Divide two numbers,两数相除求商,不能用乘法,除法,取模运算
    Merge k Sorted Lists, k路归并
    二路归并排序,利用递归,时间复杂度o(nlgn)
    StrStr,判断一个字符串是不是另一个字符串的字串,并返回子串的位置
  • 原文地址:https://www.cnblogs.com/sevenyuan/p/1817236.html
Copyright © 2011-2022 走看看