<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>David Flatley &#187; center</title>
	<atom:link href="http://www.davidflatley.com/tag/center/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.davidflatley.com</link>
	<description>A RIA developer's late night ramblings and other nonsense</description>
	<lastBuildDate>Mon, 22 Mar 2010 15:04:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Center Registration Point &#8211; Another Recipe for the Flex CookBook</title>
		<link>http://www.davidflatley.com/2007/12/27/center-registration-point-another-recipe-for-the-flex-cookbook/</link>
		<comments>http://www.davidflatley.com/2007/12/27/center-registration-point-another-recipe-for-the-flex-cookbook/#comments</comments>
		<pubDate>Thu, 27 Dec 2007 06:25:37 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Actual RIA info]]></category>
		<category><![CDATA[Adobe Flex]]></category>
		<category><![CDATA[adobe]]></category>
		<category><![CDATA[center]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[flex cook book]]></category>
		<category><![CDATA[left]]></category>
		<category><![CDATA[point]]></category>
		<category><![CDATA[registration]]></category>
		<category><![CDATA[top]]></category>

		<guid isPermaLink="false">http://www.davidflatley.com/?p=28</guid>
		<description><![CDATA[Here&#8217;s a little recipe for scaling an image from a center registration point. Flash always allowed you to bounce the registration point wherever you wanted, but Flex expects it on the top left. If you want to resize an image from the center evenly, with a horizontal slider for example, you&#8217;ll like this one. First [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a little <!-- google_ad_section_start -->recipe for scaling an image from a center registration point. Flash<!-- google_ad_section_end --> always allowed you to bounce the registration point wherever you wanted, but Flex expects it on the top left. If you want to resize an image from the center evenly, with a horizontal slider for example, you&#8217;ll like this one.</p>
<p>First the <!-- google_ad_section_start -->MXML<!-- google_ad_section_end -->. This layout is simple, it basically just has the small script with a function to accept a SilderEvent when the user changes the value of the horizontal slider. This function then uses the static functions (calcX() and calcY()) in the ImageResizer class to calculate the new math and tell your image how to resize properly from the center.</p>
<p><span id="more-28"></span></p>
<p><strong>Your layout</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Application</span> xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> layout=<span style="color: #ff0000;">&quot;absolute&quot;</span><span style="color: #7400FF;">&gt;</span></span>
	<span style="color: #339933;">&lt;mx:Script&gt;</span>
<span style="color: #339933;">		&lt;![CDATA[</span>
<span style="color: #339933;">			import mx.events.SliderEvent;</span>
<span style="color: #339933;">			import utils.DefaultValues;</span>
<span style="color: #339933;">			import utils.ImageResizer;			</span>
&nbsp;
<span style="color: #339933;">			public function doChangeSize(event:SliderEvent):void{</span>
<span style="color: #339933;">				// get the value of the hSlider, round it and divide by 100 for consistency</span>
<span style="color: #339933;">				var multiplier:Number = Math.round(hSlider.value) / 100;</span>
<span style="color: #339933;">				//trace(multiplier);</span>
<span style="color: #339933;">				// new height, width, x, and y for image</span>
<span style="color: #339933;">				img.width = DefaultValues.maxImageWidth * multiplier;</span>
<span style="color: #339933;">				img.height = DefaultValues.maxImageHeight * multiplier;				</span>
<span style="color: #339933;">				img.x = ImageResizer.calcX(container.x,container.width,img.width);</span>
<span style="color: #339933;">				img.y = ImageResizer.calcY(container.y,container.height,img.height);</span>
&nbsp;
<span style="color: #339933;">				// to avoid a &quot;jump in position&quot;, find out where the full image's x,y coords are when the slider is</span>
<span style="color: #339933;">				// at 100% and place the image at those coords within it's parent container</span>
<span style="color: #339933;">				trace(img.x + &quot; &quot; + img.y);</span>
<span style="color: #339933;">			}</span>
<span style="color: #339933;">		]]&gt;</span>
<span style="color: #339933;">	&lt;/mx:Script&gt;</span>
&nbsp;
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Canvas</span> width=<span style="color: #ff0000;">&quot;500&quot;</span> horizontalScrollPolicy=<span style="color: #ff0000;">&quot;off&quot;</span> verticalScrollPolicy=<span style="color: #ff0000;">&quot;off&quot;</span> height=<span style="color: #ff0000;">&quot;500&quot;</span> id=<span style="color: #ff0000;">&quot;container&quot;</span><span style="color: #7400FF;">&gt;</span></span>
		<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Image</span> id=<span style="color: #ff0000;">&quot;img&quot;</span> source=<span style="color: #ff0000;">&quot;@Embed('assets/images/pxl_white_logo.jpg')&quot;</span> x=<span style="color: #ff0000;">&quot;161&quot;</span> y=<span style="color: #ff0000;">&quot;199&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
		<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:HSlider</span> id=<span style="color: #ff0000;">&quot;hSlider&quot;</span></span>
<span style="color: #000000;">			minimum=<span style="color: #ff0000;">&quot;0&quot;</span> maximum=<span style="color: #ff0000;">&quot;100&quot;</span> </span>
<span style="color: #000000;">			tickColor=<span style="color: #ff0000;">&quot;black&quot;</span> </span>
<span style="color: #000000;">			value=<span style="color: #ff0000;">&quot;100&quot;</span></span>
<span style="color: #000000;">			liveDragging=<span style="color: #ff0000;">&quot;true&quot;</span> </span>
<span style="color: #000000;">			thumbDrag=<span style="color: #ff0000;">&quot;doChangeSize(event)&quot;</span> width=<span style="color: #ff0000;">&quot;339&quot;</span> x=<span style="color: #ff0000;">&quot;80&quot;</span> y=<span style="color: #ff0000;">&quot;335&quot;</span> </span>
<span style="color: #000000;">			showTrackHighlight=<span style="color: #ff0000;">&quot;true&quot;</span> </span>
<span style="color: #000000;">			themeColor=<span style="color: #ff0000;">&quot;#ff8c00&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Canvas</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Application</span><span style="color: #7400FF;">&gt;</span></span></pre></td></tr></table></div>

<p>The comments should be self explanatory enough. The &#8220;view&#8221; consists of a Canvas to hold your components, the image itself and the horizontal slider. When the slider thumb changes, it calls the doChangeSize() function which will make use of the ImageResizer class&#8217; static functions.</p>
<p><strong>ImageResizer class</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;">package utils
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> utils.<span style="color: #006600;">DefaultValues</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ImageResizer <span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">/**
		 *  Calculate your new center registration point. calcX wants x, containerWidth, and w.
		 *  Those are your image's parent container x, and width, and then your image's width.
		 *
		 * */</span>
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> calcX <span style="color: #66cc66;">&#40;</span>x:<span style="color: #0066CC;">Number</span>,containerWidth:<span style="color: #0066CC;">Number</span>,w:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">Number</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// half of image's parent containers width</span>
			<span style="color: #000000; font-weight: bold;">var</span> newRegX:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">round</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>w <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">// parent container's x minus your new registration X</span>
			<span style="color: #000000; font-weight: bold;">var</span> newX:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">round</span><span style="color: #66cc66;">&#40;</span>x - newRegX<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">// value for the default parent container's width divided by 2</span>
			<span style="color: #000000; font-weight: bold;">var</span> center:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">round</span><span style="color: #66cc66;">&#40;</span>DefaultValues.<span style="color: #006600;">innerCanvasWidth</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">// return your new centered X</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">round</span><span style="color: #66cc66;">&#40;</span>newX += center<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> calcY <span style="color: #66cc66;">&#40;</span>y:<span style="color: #0066CC;">Number</span>,containerHeight:<span style="color: #0066CC;">Number</span>,h:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">Number</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> newRegY:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">round</span><span style="color: #66cc66;">&#40;</span>h <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> newY:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">round</span><span style="color: #66cc66;">&#40;</span>y - newRegY<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> center:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">round</span><span style="color: #66cc66;">&#40;</span>containerHeight <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">round</span><span style="color: #66cc66;">&#40;</span>newY += center<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>calcX() and calcY() will accept the image&#8217;s parent container&#8217;s x, y, width and height, as well as the image&#8217;s width and height. The functions will then calculate a new registration point (x and y) based on the information given them. The comments in the code should be clear enough.</p>
<p>In this recipe, we make use of the DefaultValues class to hold some constants for us, like the image&#8217;s container width, and the image&#8217;s full width and height. It&#8217;s not imperative that we maintain this convention, but it does keep things separated and clean. This class does nothing more than maintain these values for us.</p>
<p><strong>DefaultValues class</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;">package utils <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">class</span> DefaultValues <span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const innerCanvasWidth:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">500</span>;<span style="color: #808080; font-style: italic;">// width of your image's parent container</span>
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const maxImageHeight:<span style="color: #0066CC;">int</span>=<span style="color: #cc66cc;">102</span>;<span style="color: #808080; font-style: italic;">//height of full image</span>
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const maxImageWidth:<span style="color: #0066CC;">int</span>=<span style="color: #cc66cc;">177</span>;<span style="color: #808080; font-style: italic;">//width of full image</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><strong>Sample</strong></p>
<p>You can now play with the slider and resize the image from it&#8217;s new centered registration point. Because we set the horizontal slider&#8217;s liveDragging property to true, and the thumbDrag property to update every change in the thumb&#8217;s position, we can resize the image in real time. There are many other uses for the ImageResizer class, this is just one quick and simple little recipe I thought I&#8217;d share. Manga!</p>
<p><strong>Download source code</strong><br />
<a href="http://www.davidflatley.com/wp-content/uploads/2007/12/resizefromcenter.zip" title="resizefromcenter.zip">resizefromcenter.zip</a></p>
<p>[flash http://www.davidflatley.com/wp-content/uploads/2007/12/resizefromcenter.swf o={height="500" width="500"}]</p>
<p></p>

<!-- start wp-tags-to-technorati 1.01 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/adobe' rel='tag' target='_self'>adobe</a>, <a class='technorati-link' href='http://technorati.com/tag/center' rel='tag' target='_self'>center</a>, <a class='technorati-link' href='http://technorati.com/tag/flex' rel='tag' target='_self'>flex</a>, <a class='technorati-link' href='http://technorati.com/tag/flex+cook+book' rel='tag' target='_self'>flex cook book</a>, <a class='technorati-link' href='http://technorati.com/tag/left' rel='tag' target='_self'>left</a>, <a class='technorati-link' href='http://technorati.com/tag/point' rel='tag' target='_self'>point</a>, <a class='technorati-link' href='http://technorati.com/tag/registration' rel='tag' target='_self'>registration</a>, <a class='technorati-link' href='http://technorati.com/tag/top' rel='tag' target='_self'>top</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.davidflatley.com/2007/12/27/center-registration-point-another-recipe-for-the-flex-cookbook/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
