<?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>timwhitlock.info &#187; matrix</title>
	<atom:link href="http://timwhitlock.info/blog/tag/matrix/feed/" rel="self" type="application/rss+xml" />
	<link>http://timwhitlock.info</link>
	<description>Tim Whitlock&#039;s personal site and blog</description>
	<lastBuildDate>Thu, 15 Dec 2011 13:51:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Scale &amp; rotate around an arbitrary centre</title>
		<link>http://timwhitlock.info/blog/2008/04/13/scale-rotate-around-an-arbitrary-centre/</link>
		<comments>http://timwhitlock.info/blog/2008/04/13/scale-rotate-around-an-arbitrary-centre/#comments</comments>
		<pubDate>Sun, 13 Apr 2008 19:22:57 +0000</pubDate>
		<dc:creator>tim</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[matrix]]></category>
		<category><![CDATA[vector]]></category>

		<guid isPermaLink="false">http://web.2point1.com/2008/04/13/scale-rotate-around-an-arbitrary-centre/</guid>
		<description><![CDATA[The mini project I picked as my first dip into AS3 was a short example that I had been planning for this blog, so here it is in AS3, instead of AS2 as I had originally planned. The South Park Chin balls Flash app required that the user drag and zoom a loaded photo. Anyone [...]]]></description>
			<content:encoded><![CDATA[<p>The mini project I picked as my first dip into AS3 was a short example that I had been planning for this blog, so here it is in AS3, instead of AS2 as I had originally planned.</p>
<p>The South Park <a href="http://web.2point1.com/2008/03/23/down-and-dirty/">Chin balls</a> Flash app required that the user drag and zoom a loaded photo. Anyone who&#8217;s ever done this will know that you can&#8217;t just scale the picture around it&#8217;s registration point when you zoom. <em>Why</em>? because the centre point changes as you pan the image around. So the requirement in a nutshell is &#8211; to be able to <strong>scale a <em>MovieClip</em> around an arbitrary centre</strong>.</p>
<p>Sure, you could take the <em>Russian doll</em> approach with multiple clips inside clips, but that just ain&#8217;t cool! I thought I&#8217;d share the way I did it as it&#8217;s pretty concise.<span id="more-25"></span></p>
<p>First of all here&#8217;s the principal in action. Scaling and rotation, both performed around an arbitrary centre point that you can alter by dragging that little marker around. Go on, have a go.</p>
<p>[ Update: due to a <a href="http://web.2point1.com/2009/03/21/lessons-learned-again/">nasty server crash</a> some of these files are missing.. sorry. <a href="/wp-content/uploads/2008/04/virtualcentresprite.html#scaleAround" title="scaleAround method" target="_blank">This source was rescued though</a> ]</p>
<p id="flashcontent" style="border: 1px solid #666666; padding: 0pt; display: block; width: 350px; height: 350px">Loading example SWF&#8230;</p>
<p><script type="text/javascript">  <!--     var so = new SWFObject("/wp-content/uploads/2008/04/virtualcentreexample.swf", "flashobj", "350", "350", "9", "#F0F0F0");  so.write("flashcontent");   so.addParam("wmode","opaque");  //-->   </script></p>
<p><a href="/wp-content/uploads/2008/04/virtualcentreexample.zip" title="VirtualCentreExample">Download the source files for this example</a></p>
<p>Most of the source files just serve to create the example interface. The only class we need to discuss is the one with the scale and rotating logic. The class <code><strong>VirtualCentreSprite</strong></code> is an extension of the <code>Sprite</code> class. In AS2 it would have to be an extension of the <code>MovieClip</code> class, but we don&#8217;t need a timeline. You could use this as a base class for sprites across your project to ensure they all have this capability.</p>
<p>It has two methods as follows:<br />
<code style="display: block; white-space: nowrap"><br />
void <strong>scaleAround</strong>( <strong>offsetX</strong>:Number, <strong>offsetY</strong>:Number, <strong>absScaleX</strong>:Number, <strong>absScaleY</strong>:Number )<br />
void <strong>rotateAround</strong>( <strong>offsetX</strong>:Number, <strong>offsetY</strong>:Number, <strong>toDegrees</strong>:Number )<br />
</code><br />
The scaling method is the simplest. It&#8217;s basically GCSE vector maths. We calculate the position that the clip should be at after scaling, do the scaling (which will move the clip according to the centre that Flash knows about) and then we simply reposition it to where it should be.<br />
<a href="/wp-content/uploads/2008/04/virtualcentresprite.html#scaleAround" title="scaleAround method" target="_blank">Click to see source of method <code>scaleAround</code></a></p>
<p>The rotating method is a bit more complex, because it uses a transformation matrix. But thanks to the <code>flash.geom</code> package this is short and sweet. Again the principal is that we let Flash rotate the sprite and then adjust the position.<br />
<a href="/wp-content/uploads/2008/04/virtualcentresprite.html#rotateAround" title="rotateAround method" target="_blank">Click to see source of method <code>rotateAround</code></a></p>
]]></content:encoded>
			<wfw:commentRss>http://timwhitlock.info/blog/2008/04/13/scale-rotate-around-an-arbitrary-centre/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

