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.
- Load an external image
- Create a new instance of BitmapData (bitmap_data) and assign it to bitmapData property of the external bitmap
- Create a new instance of BitmapData (bitmap_data_copy) with same size of external bitmap
- Copy the pixels of external bitmap in the new instance bitmap_data_copy
- Use the dispose method on bitmap_data to free memory
- Create a mask form using a Shape
- Create a new instance of BitmapData (bitmap_data_shape) the same size of external image and use draw method over shape
- bitmap_data_copy uses the method copyChannel on bitmap_data_shape
- Give bitmap_data_copy to property bitmapData of external image
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | 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 ; } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述