Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 04-05-2012, 10:10 PM   #1
fohanlon
Registered User
 
Join Date: May 2007
Posts: 19
Default Scale an Image

Hi Guys

I have coded as3 to allow for an image to be uploaded and portionally resized, smoothed etc into a movieclip of a fixed size say 150 x 150.

Once the image is uploaded I have buttons to scale the image + or - using scaleX and scaleY.

Here is where I have the problem.

I want to do a couple of things:

1. Ensure the image uploaded when being scaled up cannot go beyond the 150 pixels size and also when being scaled down it cannot go below a certain size.

2. When scaling how can I ensure the scaled image will always be centred vertical and horizontal in the movieclip.

Thanks for your advice.

F.
fohanlon is offline   Reply With Quote
Old 04-05-2012, 11:17 PM   #2
-:)lauri
Senior Member
 
Join Date: Aug 2008
Location: Helsinki, Finland
Posts: 1,184
Default

Maybe something like this would help:
ActionScript Code:
// To enlarge by pixel function scaleUpImage():void{     if(image.width < 150 && image.height < 150){         image.width += 1;         image.height += 1;         centerImage();     } } // To shrink by pixel function scaleDownImage():void{     if(image.width > certainSize && image.height > certainSize){         image.width -= 1;         image.height -= 1;         centerImage();     } } // To center the image function centerImage():void{     image.x = (movieclip.width - image.width) / 2;     image.y = (movieclip.height - image.height) / 2; }
-:)lauri is offline   Reply With Quote
Old 04-05-2012, 11:20 PM   #3
svenjoypro
DontJustEnjoyIt,SvEnjoyIt
 
svenjoypro's Avatar
 
Join Date: Sep 2005
Location: California
Posts: 1,389
Default

I attached a fla (CS4), here's the code:
ActionScript Code:
import flash.events.MouseEvent; increase_btn.addEventListener(MouseEvent.CLICK, onIncrease); decrease_btn.addEventListener(MouseEvent.CLICK, onDecrease); function onIncrease(evt:MouseEvent):void {     var inc:Number = .2;     var max:int = 150;         img_mc.scaleX += inc;     img_mc.scaleY += inc;     if (img_mc.width > max) {         img_mc.scaleX = img_mc.scaleY = 1;         img_mc.scaleX = img_mc.scaleY = max/img_mc.width;     }     if (img_mc.height > max) {         img_mc.scaleX = img_mc.scaleY = 1;         img_mc.scaleX = img_mc.scaleY = max/img_mc.height;         if (img_mc.width > max) {             img_mc.scaleX = img_mc.scaleY = 1;             img_mc.scaleX = img_mc.scaleY = max/img_mc.width;         }     }     centerImage(); } function onDecrease(evt:MouseEvent):void {     var dec:Number = .2;     var min:int = 50;         img_mc.scaleX -= dec;     img_mc.scaleY -= dec;     if (img_mc.width < min) {         img_mc.scaleX = img_mc.scaleY = 1;         img_mc.scaleX = img_mc.scaleY = min/img_mc.width;     }     if (img_mc.height < min) {         img_mc.scaleX = img_mc.scaleY = 1;         img_mc.scaleX = img_mc.scaleY = min/img_mc.height;         if (img_mc.width < min) {             img_mc.scaleX = img_mc.scaleY = 1;             img_mc.scaleX = img_mc.scaleY = min/img_mc.width;         }     }     centerImage(); } function centerImage():void {     img_mc.x = (stage.stageWidth-img_mc.width)/2;     img_mc.y = (stage.stageHeight-img_mc.height)/2; }
Attached Files
File Type: zip scale.zip (10.0 KB, 22 views)
svenjoypro is offline   Reply With Quote
Old 04-06-2012, 07:51 AM   #4
fohanlon
Registered User
 
Join Date: May 2007
Posts: 19
Default

Many thanks Lauri your code worked perfectly for my solution.
fohanlon is offline   Reply With Quote
Old 04-06-2012, 07:52 AM   #5
fohanlon
Registered User
 
Join Date: May 2007
Posts: 19
Default

Many thanks svenjoypro. I combined some of your code with Lauri's.
fohanlon is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:26 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.