Could someone please help, I am trying to scale / zoom an image
from it's center point, instead the following code scales / zooms from
the (0,0) point of the image. Any help is greatly appreciated.

Thanks,

Calo

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";
layout="horizontal" creationComplete="initApp()">
        <mx:Script>
            <![CDATA[
                import mx.core.UIComponent;
                        import flash.display.BitmapData;
                                                
                        private function initApp():void
                        {
                                var md:BitmapData = 
createMaskData(UIComponent(drawCanvas));
                                var imageMask:Bitmap = new Bitmap(md);
                                
                                var spriteMask:Sprite = new Sprite();
                                spriteMask.addChild(imageMask);
                                
                                drawCanvas.mask = spriteMask;
                                
                                photoToScale.x = 
Math.round((drawCanvas.width-photoToScale.width)
/ 2);
                                photoToScale.y =
Math.round((drawCanvas.height-photoToScale.height) / 2);
                        }
                        
                        private function 
createMaskData(target:UIComponent):BitmapData
                        {
                                var maskData:BitmapData = new 
BitmapData(target.width, target.height);
                                var x:Matrix = new Matrix();
                                maskData.draw(maskData,x);
                                return maskData;
                        }
                        
                        private function zoom(event:Event):void
                        {
                           var w:Number = photoToScale.width;
               var h:Number = photoToScale.height;
               
              
               photoToScale.scaleX = imgslider.value;
               photoToScale.scaleY = imgslider.value;
                 
               w = photoToScale.width - w;
               h = photoToScale.height - h;
               
               photoToScale.x -= w/2;
               photoToScale.y -= h/2;
                        }
                ]]>
        </mx:Script>
        <mx:Panel id="imagePanel" title="Image" width="100%" height="100%"
backgroundColor="#000000" clipContent="true" horizontalAlign="center"
verticalAlign="middle" horizontalScrollPolicy="off"
verticalScrollPolicy="off">
                <mx:Canvas id="drawCanvas" width="100%" height="100%"
backgroundAlpha="0">
                    <mx:Image id="photoToScale" source="images/scenic.jpg"/>
                    <mx:Canvas id="drawscreen" width="100%" height="100%"
backgroundAlpha="0"/>
                </mx:Canvas>
        </mx:Panel>
        <mx:Panel id="imageControls" title="Zoom Image" height="100%"
width="10%">
                <mx:VSlider id="imgslider" value=".7" maximum="1.7" minimum=".1"
enabled="true" allowTrackClick="true" liveDragging="true"
change="zoom(event)"/>
        </mx:Panel>
</mx:Application>

Reply via email to