Jan 25, 2009

Smooth Dynamic JPG’s in Flash

You can use the following function in order to load external jpg and smooth scaling them:

_global.smoothImageLoad = function(imgURL, targetMovie) {
     var i=0
     do { i++ } while (eval("_root.smoothImageLoadTemp"+i) != undefined)
          tmc = _root.createEmptyMovieClip("smoothImageLoadTemp"+i, _root.getNextHighestDepth())
          tmc.createEmptyMovieClip("ti", tmc.getNextHighestDepth())
          tmc.tm = targetMovie
          with(tmc) {
               tmcl = new MovieClipLoader()
               tmcl.onLoadComplete = function() {
                    ti.onEnterFrame = function() {
                         pixelData = new flash.display.BitmapData(ti._width, ti._height);
                         pixelData.draw(ti);
                         tm.attachBitmap(pixelData, 1, true, true);
                         tm.smoothImageLoadComplete()
                         removeMovieClip(ti._parent)
                    }
               }
          tmcl.loadClip(imgURL, tmc.ti)
     }
}

Just take this chunk of code and slap it on your first keyframe. This way it's available to your entire movie. Then to use it:

smoothImageLoad("image.jpg", mytargetmc)

Also, if you want something to happen just as the image is loaded, you can set a smoothImageLoadComplete function on you target mC. As the loading and polishing ends, my script tries to trigger that function on the target.

mytargetmc.smoothImageLoadComplete = function() {
     trace("yeah baby yeah!!!")
}

SOURCE

No comments: