root/trunk/as2/com/jeroenwijering/utils/ImageLoader.as @ 1

Revision 1, 4.5 kB (checked in by jeroen, 18 months ago)

initial commit of old repository into public one

  • Property svn:executable set to *
Line 
1/**
2* Class for loading, scaling and smoothing images to a given MovieClip.
3*
4* @example
5* var myLoader = new ImageLoader(this,"true",400,300);
6* myLoader.loadImage("somephoto.jpg");
7*
8* @author       Jeroen Wijering
9* @version      1.11
10**/
11
12
13class com.jeroenwijering.utils.ImageLoader {
14
15
16        /** MovieClip Loader Instance **/
17        private var mcLoader:MovieClipLoader;
18        /** Target MovieClip **/
19        private var targetClip:MovieClip;
20        /** Target Width **/
21        private var targetWidth:Number;
22        /** Target Height **/
23        private var targetHeight:Number;
24        /** Source URL **/
25        private var sourceURL:String;
26        /** Source Width **/
27        private var sourceWidth:Number;
28        /** Source Height **/
29        private var sourceHeight:Number;
30        /** Source Length (for SWF) **/
31        private var sourceLength:Number;
32        /** Overstretch Boolean **/
33        private var overStretch:String = "none";
34        /** Boolean that checks whether an SWF is loaded **/
35        private var useSmoothing:Boolean;
36        /** Interval for SWF meta checking **/
37        private var metaInt:Number;
38
39
40        /** Constructor for the ImageLoader **/
41        function ImageLoader(tgt:MovieClip,ost:String,wid:Number,hei:Number) {
42                targetClip = tgt;
43                arguments.length > 1 ? overStretch = String(ost): null;
44                if(arguments.length > 2) {
45                        targetWidth = wid;
46                        targetHeight = hei;
47                }
48                mcLoader = new MovieClipLoader();
49                mcLoader.addListener(this);
50        };
51
52
53        /** Switch image with bitmaparray if possible. **/
54        public function onLoadInit(inTarget:MovieClip):Void {
55                if(useSmoothing  == 'true') {
56                        var bmp = new flash.display.BitmapData(targetClip.mc._width,
57                                targetClip.mc._height, true, 0x000000);
58                        bmp.draw(targetClip.mc);
59                        var bmc:MovieClip = targetClip.createEmptyMovieClip("smc",
60                                targetClip.getNextHighestDepth());
61                        bmc.attachBitmap(bmp, bmc.getNextHighestDepth(),"auto",true);
62                        targetClip.mc.unloadMovie();
63                        targetClip.mc.removeMovieClip();
64                        delete targetClip.mc;
65                        scaleImage(targetClip.smc);
66                        onLoadFinished();
67                } else {
68                        targetClip.mc.forceSmoothing = true;
69                        if(sourceURL.toLowerCase().indexOf(".swf") == -1) {
70                                scaleImage(targetClip.mc);
71                        }
72                        onLoadFinished();
73                }
74        };
75
76
77        /** Scale the image while maintaining aspectratio **/
78        private function scaleImage(tgt:MovieClip):Void {
79                targetClip._xscale = targetClip._yscale = 100;
80                var tcf = tgt._currentframe;
81                tgt.gotoAndStop(1);
82                sourceWidth = tgt._width;
83                sourceHeight = tgt._height;
84                sourceLength = tgt._totalframes/20;
85                var xsr = targetWidth/sourceWidth;
86                var ysr = targetHeight/sourceHeight;
87                if (overStretch == "fit" || Math.abs(xsr-ysr) < 0.1) {
88                        tgt._width = targetWidth;
89                        tgt._height = targetHeight;
90                } else if ((overStretch == "true" && xsr > ysr) ||
91                        (overStretch == "false" && xsr < ysr)) {
92                        tgt._xscale = tgt._yscale = xsr*100;
93                } else if(overStretch == "none") {
94                        tgt._xscale = tgt._yscale = 100;
95                } else {
96                        tgt._xscale = tgt._yscale = ysr*100;
97                }
98                if(targetWidth != undefined) {
99                        tgt._x = targetWidth/2 - tgt._width/2;
100                        tgt._y = targetHeight/2 - tgt._height/2;
101                }
102                tgt.gotoAndPlay(tcf);
103                onMetaData();
104        };
105
106
107        /** Start loading an image. **/
108        public function loadImage(img:String):Void {
109                sourceURL = img;
110                targetClip.mc.clear();
111                targetClip.smc.unloadMovie();
112                targetClip.smc.removeMovieClip();
113                delete targetClip.smc;
114                checkSmoothing(img);
115                var raw:MovieClip = targetClip.createEmptyMovieClip("mc",1);
116                mcLoader.loadClip(img,raw);
117                if(img.toLowerCase().indexOf(".swf") > -1) {
118                        metaInt = setInterval(this,"setSWFMeta",200);
119                }
120        };
121
122
123        /** Check whether smoothing can be enabled. **/
124        private function checkSmoothing(img:String):Void {
125                var idx:Number = _root._url.indexOf("/",8);
126                var rot:String = _root._url.substring(0,idx);
127                if(System.capabilities.version.indexOf("7,0,") > -1 ||
128                        img.toLowerCase().indexOf(".swf") > -1 ||
129                        _root._url.indexOf("file://") > -1 ||
130                        (img.indexOf(rot) == -1 && img.indexOf('http://') == 0)) {
131                        useSmoothing = false;
132                } else {
133                        useSmoothing = true;
134                }
135        };
136
137
138        /** Check when to set the SWF metadata **/
139        private function setSWFMeta() {
140                if(targetClip.mc._currentframe > 0) {
141                        clearInterval(metaInt);
142                        scaleImage(targetClip.mc);
143                }
144        };
145
146
147        /** Event handler; invoked when loading. **/
148        public function onLoadProgress(tgt:MovieClip,btl:Number,btt:Number) {};
149
150
151        /** Event handler; invoked when image is completely loaded. **/
152        public function onLoadFinished() {};
153
154
155        /** Event handler; invoked when metadata is received. **/
156        public function onMetaData() {};
157
158
159}
Note: See TracBrowser for help on using the browser.