<?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>Saturnboy &#187; fxg</title>
	<atom:link href="http://saturnboy.com/tag/fxg/feed/" rel="self" type="application/rss+xml" />
	<link>http://saturnboy.com</link>
	<description>Code, Work, and Life</description>
	<lastBuildDate>Thu, 01 Mar 2012 22:35:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Drag-and-Drop in Flex 4</title>
		<link>http://saturnboy.com/2009/08/drag-and-drop-flex-4/</link>
		<comments>http://saturnboy.com/2009/08/drag-and-drop-flex-4/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 02:40:52 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[drag-and-drop]]></category>
		<category><![CDATA[flex4]]></category>
		<category><![CDATA[fxg]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=672</guid>
		<description><![CDATA[UPDATE: I have another Drag-and-Drop Revisited post that covers even more drag-and-drop functionality available in Flex 4. The Flex 4 gods were kind to us developers when they made the great decision to leave the custom drag-and-drop support unchanged. We just do what we&#8217;ve always done: detect the user is trying to drag something via [...]]]></description>
			<content:encoded><![CDATA[<p><b>UPDATE:</b> I have another <a href="http://saturnboy.com/2010/12/drag-and-drop-revisited/">Drag-and-Drop Revisited</a> post that covers even more drag-and-drop functionality available in Flex 4.</p>
<p>The Flex 4 gods were kind to us developers when they made the great decision to leave the custom drag-and-drop support unchanged.  We just do what we&#8217;ve always done: detect the user is trying to drag something via <code>mouseDown</code> or <code>mouseMove</code> and then add both <code>dragEnter</code> and <code>dragDrop</code> event handlers to the drop target.  So there is nothing in this post that&#8217;s not basically identical to Flex 3, except the coolness of FXG (which you can easily mimic with <a href="http://www.degrafa.org/">Degrafa</a> in Flex 3).</p>
<h5>Simple Drag-and-Drop</h5>
<p class="bottom">I&#8217;ll begin with a basic Flex 4 application.  We have two draggable graphics (a <code>Rect</code> and an <code>Ellipse</code>) in the left panel, and a target panel on the right:</p>

<div class="wp_syntax"><div 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;s:Application</span></span>
<span style="color: #000000;">        xmlns:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span></span>
<span style="color: #000000;">        xmlns:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Script</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;">&lt;!<span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span></span>
<span style="color: #000000;">            imports...</span>
&nbsp;
<span style="color: #000000;">            private function mouseDownHandler<span style="color: #66cc66;">&#40;</span>e:MouseEvent<span style="color: #66cc66;">&#41;</span>:void <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">                DragManager.doDrag<span style="color: #66cc66;">&#40;</span>e.currentTarget as IUIComponent, null, e<span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #000000;">            <span style="color: #66cc66;">&#125;</span></span>
&nbsp;
<span style="color: #000000;">            private function dragEnterHandler<span style="color: #66cc66;">&#40;</span>e:DragEvent<span style="color: #66cc66;">&#41;</span>:void <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">                DragManager.acceptDragDrop<span style="color: #66cc66;">&#40;</span>e.currentTarget as IUIComponent<span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #000000;">            <span style="color: #66cc66;">&#125;</span></span>
&nbsp;
<span style="color: #000000;">            private function dragDropHandler<span style="color: #66cc66;">&#40;</span>e:DragEvent<span style="color: #66cc66;">&#41;</span>:void <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">                e.currentTarget.addElement<span style="color: #66cc66;">&#40;</span>e.dragInitiator<span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #000000;">            <span style="color: #66cc66;">&#125;</span></span>
<span style="color: #000000;">        <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Script</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Panel</span> title=<span style="color: #ff0000;">&quot;src&quot;</span> width=<span style="color: #ff0000;">&quot;100&quot;</span> minHeight=<span style="color: #ff0000;">&quot;133&quot;</span> x=<span style="color: #ff0000;">&quot;10&quot;</span> y=<span style="color: #ff0000;">&quot;10&quot;</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Graphic</span> width=<span style="color: #ff0000;">&quot;80&quot;</span> height=<span style="color: #ff0000;">&quot;80&quot;</span></span>
<span style="color: #000000;">                mouseDown=<span style="color: #ff0000;">&quot;mouseDownHandler(event)&quot;</span><span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Rect</span> ... <span style="color: #7400FF;">&lt;/s:Rect</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Graphic</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Graphic</span> width=<span style="color: #ff0000;">&quot;80&quot;</span> height=<span style="color: #ff0000;">&quot;80&quot;</span></span>
<span style="color: #000000;">                mouseDown=<span style="color: #ff0000;">&quot;mouseDownHandler(event)&quot;</span><span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Ellipse</span> ... <span style="color: #7400FF;">&lt;/s:Ellipse</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Graphic</span><span style="color: #7400FF;">&gt;</span></span>
        ...
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Panel</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Panel</span> title=<span style="color: #ff0000;">&quot;target&quot;</span> width=<span style="color: #ff0000;">&quot;100&quot;</span> minHeight=<span style="color: #ff0000;">&quot;133&quot;</span> x=<span style="color: #ff0000;">&quot;120&quot;</span> y=<span style="color: #ff0000;">&quot;10&quot;</span></span>
<span style="color: #000000;">            dragEnter=<span style="color: #ff0000;">&quot;dragEnterHandler(event);&quot;</span></span>
<span style="color: #000000;">            dragDrop=<span style="color: #ff0000;">&quot;dragDropHandler(event);&quot;</span><span style="color: #7400FF;">&gt;</span></span>
        ...
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Panel</span><span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Application</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>Reading the code above, each draggable <code>Graphic</code> has a <code>mouseDown</code> handler that calls <code>DragManager.doDrag()</code> to initiate dragging.   And the target <code>Panel</code> calls <code>DragManager.acceptDragDrop()</code> on <code>dragEnter</code> and <code>addElement()</code> on <code>dragDrop</code>.  Note that since <code>Panel</code> is a Spark container we must use <code>addElement()</code> to re-parent the dropped graphic (the familiar <code>addChild()</code> is still used for Halo containers).</p>
<p class="bottom">Our simple drag-and-drop app (view source enabled):</p>
<div id="flashcontent-drag-drop">
Flash is required.  <a href="http://www.adobe.com/go/getflashplayer">Get it here!</a>
</div>
<h5>Using a FXG Drag Proxy</h5>
<p>In the example above, the moment you start dragging a component the drag proxy is displayed.  By default, the proxy is just a bounding rectangle with an alpha value of 0.5.  This is particularly noticeable and <i>lame</i> when you try to drag the pink circle.  Thankfully, we can use any display object for the drag proxy (more specifically any component that implements <code>IFlexDisplayObject</code> which includes <code>UIComponent</code> and any of its descendants).  Why not use a FXG graphic?  It is, after all, a major piece of the new hotness that is Flex 4.</p>
<p class="bottom">First, we need to modify the <code>mouseDownHandler</code> to instantiate our FXG graphic.  Then we just add it as the forth parameter to the <code>DragManager.doDrag()</code> call.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> mouseDownHandler<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> star:Star = <span style="color: #000000; font-weight: bold;">new</span> Star<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    DragManager.<span style="color: #006600;">doDrag</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">currentTarget</span> as IUIComponent, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #0066CC;">e</span>, star<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p class="bottom">And here is <code>Star.mxml</code>:</p>

<div class="wp_syntax"><div 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;s:Graphic</span></span>
<span style="color: #000000;">        xmlns:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span></span>
<span style="color: #000000;">        xmlns:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span></span>
<span style="color: #000000;">        width=<span style="color: #ff0000;">&quot;80&quot;</span> height=<span style="color: #ff0000;">&quot;80&quot;</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Path</span> data=<span style="color: #ff0000;">&quot;M 119,0 L 148,86 238,86 166,140 192,226 119,175 46,226 72,140 0,86 90,86 Z&quot;</span></span>
<span style="color: #000000;">            y=<span style="color: #ff0000;">&quot;2&quot;</span> scaleX=<span style="color: #ff0000;">&quot;0.3361345&quot;</span> scaleY=<span style="color: #ff0000;">&quot;0.3361345&quot;</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:fill</span><span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:SolidColor</span> color=<span style="color: #ff0000;">&quot;#FFCC00&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:fill</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Path</span><span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Graphic</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p class="bottom">Bang!  We have a gold star as our drag proxy.  Try it out for yourself (view source enabled):</p>
<div id="flashcontent-drag-drop-star">
Flash is required.  <a href="http://www.adobe.com/go/getflashplayer">Get it here!</a>
</div>
<h5>Dynamic Drag Proxy</h5>
<p>Dragging a gold star is pretty cool, but how can we make the drag proxy look <b>exactly</b> like the drag source?  For that we need a dynamic drag proxy.</p>
<p class="bottom">First, for reasons which will become clear in a moment, we need to revise our app to make each of the drag source graphics into a separate component.  So the left <code>Panel</code> changes to contain these custom components:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Panel</span> title=<span style="color: #ff0000;">&quot;src&quot;</span> width=<span style="color: #ff0000;">&quot;100&quot;</span> minHeight=<span style="color: #ff0000;">&quot;133&quot;</span> x=<span style="color: #ff0000;">&quot;10&quot;</span> y=<span style="color: #ff0000;">&quot;10&quot;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;graphics:Square</span> fillColor=<span style="color: #ff0000;">&quot;#6666FF&quot;</span></span>
<span style="color: #000000;">            mouseDown=<span style="color: #ff0000;">&quot;mouseDownHandler(event)&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;graphics:Circle</span> fillColor=<span style="color: #ff0000;">&quot;#FF66FF&quot;</span></span>
<span style="color: #000000;">            mouseDown=<span style="color: #ff0000;">&quot;mouseDownHandler(event)&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;graphics:Star</span> fillColor=<span style="color: #ff0000;">&quot;#FFCC00&quot;</span></span>
<span style="color: #000000;">            mouseDown=<span style="color: #ff0000;">&quot;mouseDownHandler(event)&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;graphics:Square</span> fillColor=<span style="color: #ff0000;">&quot;#66FF99&quot;</span></span>
<span style="color: #000000;">            mouseDown=<span style="color: #ff0000;">&quot;mouseDownHandler(event)&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:layout</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:VerticalLayout</span> gap=<span style="color: #ff0000;">&quot;10&quot;</span> horizontalAlign=<span style="color: #ff0000;">&quot;center&quot;</span> paddingTop=<span style="color: #ff0000;">&quot;10&quot;</span> paddingBottom=<span style="color: #ff0000;">&quot;10&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:layout</span><span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Panel</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p class="bottom">Next, we again modify the <code>mouseDownHandler</code>, this time we instantiate our drag proxy dynamically.  We use reflection on the incoming drag source to get its name, instantiate it, adjust a few properties, and pass it to the <code>DragManager.doDrag()</code> call.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> mouseDownHandler<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> dragSrc:Graphic = <span style="color: #0066CC;">e</span>.<span style="color: #006600;">currentTarget</span> as Graphic;
&nbsp;
    <span style="color: #808080; font-style: italic;">//create a proxy by creating a new &quot;copy&quot; of the drag src</span>
    <span style="color: #000000; font-weight: bold;">var</span> className:<span style="color: #0066CC;">String</span> = getQualifiedClassName<span style="color: #66cc66;">&#40;</span>dragSrc<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">var</span> klass:<span style="color: #000000; font-weight: bold;">Class</span> = getDefinitionByName<span style="color: #66cc66;">&#40;</span>className<span style="color: #66cc66;">&#41;</span> as <span style="color: #000000; font-weight: bold;">Class</span>;
    <span style="color: #000000; font-weight: bold;">var</span> proxy:<span style="color: #66cc66;">*</span> = <span style="color: #000000; font-weight: bold;">new</span> klass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">//set the proxy's properties to match the src + sexy drop shadow</span>
    proxy.<span style="color: #0066CC;">width</span> = dragSrc.<span style="color: #0066CC;">width</span>;
    proxy.<span style="color: #0066CC;">height</span> = dragSrc.<span style="color: #0066CC;">height</span>;
    proxy.<span style="color: #006600;">fillColor</span> = <span style="color: #66cc66;">&#40;</span>dragSrc as IDraggableGraphic<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">fillColor</span>;
    proxy.<span style="color: #006600;">filters</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #000000; font-weight: bold;">new</span> DropShadowFilter<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
    DragManager.<span style="color: #006600;">doDrag</span><span style="color: #66cc66;">&#40;</span>dragSrc, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #0066CC;">e</span>, proxy<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The real reason for the reflection gymnastics, plus the need to package each drag source as a custom component, is the lack of a <b>deep copy</b> operation on <code>UIComponent</code>.  If we could just <i>clone</i> the drag source, we&#8217;d be golden.  Alas, for many reasons, the most obvious of which is that it is a huge pain in the ass, there is no such thing as deep copy.  In our case, my work around was to package all the visual stuff of each draggable item into a single custom component with a known API (all the custom graphics implement <code>IDraggableGraphic</code>).  Then I reflect, instantiate, configure, and pass to <code>doDrag()</code>.</p>
<p class="bottom">Our dynamic drag proxy now matches the drag source, plus the sexy drop shadow.  Check it out (view source enabled):</p>
<div id="flashcontent-drag-drop-proxy">
Flash is required.  <a href="http://www.adobe.com/go/getflashplayer">Get it here!</a>
</div>
<h5>Files</h5>
<ul>
<li><a href="http://saturnboy.com/proj/flex4/drag_drop/simple/DragDrop.html">DragDrop</a> (<a href="http://saturnboy.com/proj/flex4/drag_drop/simple/srcview/DragDrop.zip">download</a>)</li>
<li><a href="http://saturnboy.com/proj/flex4/drag_drop/star/DragDropStar.html">DragDropStar</a> (<a href="http://saturnboy.com/proj/flex4/drag_drop/star/srcview/DragDropStar.zip">download</a>)</li>
<li><a href="http://saturnboy.com/proj/flex4/drag_drop/proxy/DragDropProxy.html">DragDropProxy</a> (<a href="http://saturnboy.com/proj/flex4/drag_drop/proxy/srcview/DragDropProxy.zip">download</a>)</li>
</ul>
<blockquote><p><b>NOTE:</b> All code was built with Flash Builder 4 Beta 1.</p></blockquote>
<div>
<script type="text/javascript">
swfobject.embedSWF('http://saturnboy.com/proj/flex4/drag_drop/simple/DragDrop.swf', 'flashcontent-drag-drop', '400', '260', '10.0.0', 'expressInstall.swf', false, { bgColor:'#ffffff', base:'.' });
</script><br />
<script type="text/javascript">
swfobject.embedSWF('http://saturnboy.com/proj/flex4/drag_drop/star/DragDropStar.swf', 'flashcontent-drag-drop-star', '400', '260', '10.0.0', 'expressInstall.swf', false, { bgColor:'#ffffff', base:'.' });
</script><br />
<script type="text/javascript">
swfobject.embedSWF('http://saturnboy.com/proj/flex4/drag_drop/proxy/DragDropProxy.swf', 'flashcontent-drag-drop-proxy', '400', '450', '10.0.0', 'expressInstall.swf', false, { bgColor:'#ffffff', base:'.' });
</script></div>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2009/08/drag-and-drop-flex-4/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>SVG Primer for Flex</title>
		<link>http://saturnboy.com/2009/06/svg-primer-for-flex/</link>
		<comments>http://saturnboy.com/2009/06/svg-primer-for-flex/#comments</comments>
		<pubDate>Thu, 11 Jun 2009 04:30:46 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[degrafa]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[flex4]]></category>
		<category><![CDATA[fxg]]></category>
		<category><![CDATA[svg]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=551</guid>
		<description><![CDATA[The dream of SVG was probably born sometime in the late 90&#8242;s. Version 1.0 arrived in 2001, followed by the current version, SVG 1.1, in 2003. For anyone keeping score at home, that&#8217;s over 6 years ago, which is like 120 years ago in internet dog years. Also for those at home, SVG is a [...]]]></description>
			<content:encoded><![CDATA[<p>The dream of SVG was probably born sometime in the late 90&#8242;s.  Version 1.0 arrived in 2001, followed by the <i>current</i> version, <a href="http://www.w3.org/TR/SVG/">SVG 1.1</a>, in 2003.  For anyone keeping score at home, that&#8217;s over 6 years ago, which is like 120 years ago in internet dog years.  Also for those at home, SVG is a portable XML-based vector graphics format (it also does raster graphics), but you&#8217;d probably not be reading this if you didn&#8217;t already know.</p>
<p>Nowadays, I&#8217;m finally seeing some SVG in use.  It works natively in all the real browsers (obviously not IE, which requires a plugin), and even on some mobile devices.  But most importantly to me, I see it in my day-to-day work in <a href="http://www.degrafa.org/">Degrafa</a>, and with the beta release of Flex 4, in <a href="http://labs.adobe.com/technologies/flashcatalyst/">Catalyst</a> and <a href="http://opensource.adobe.com/wiki/display/flexsdk/FXG+1.0+Specification">FXG</a>.</p>
<p>The vast majority of my experience in SVG is with paths (<a href="http://www.w3.org/TR/SVG/paths.html">SVG Spec, &sect; 8</a>) and to a lesser extent Transforms (<a href="http://www.w3.org/TR/SVG/coords.html">&sect; 7</a>) and Filters (<a href="http://www.w3.org/TR/SVG/filters.html">&sect; 15</a>).  Thankfully, these are some of the most useful and important pieces of SVG, and they all have nice one-to-one mappings to components in Degrafa and FXG.</p>
<h5>SVG Path Primer</h5>
<p>In SVG, a <b>path</b> is the outline of some object.  It is described as a series of segments, where each segment can be different, either a line, curve, or arc.  Path data is most often given in shorthand syntax as a series of commands followed by coordinates (we&#8217;ll ignore the long form for now).  Let&#8217;s illuminate the discussion with some examples.</p>
<p class="bottom"><b>SVG</b>:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">standalone</span>=<span style="color: #ff0000;">&quot;no&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #00bbdd;">&lt;!DOCTYPE svg PUBLIC &quot;-//W3C//DTD SVG 1.1//EN&quot;</span>
<span style="color: #00bbdd;">    &quot;http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;svg</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2000/svg&quot;</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.1&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;200&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;200&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;path</span> <span style="color: #000066;">d</span>=<span style="color: #ff0000;">&quot;M 0,0 L 100,0 L 100,100 L 0,100 z&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">fill</span>=<span style="color: #ff0000;">&quot;#EECCEE&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">stroke</span>=<span style="color: #ff0000;">&quot;#FF00FF&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">stroke-width</span>=<span style="color: #ff0000;">&quot;3&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/svg<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p class="bottom"><b>Degrafa</b> in Flex 3:</p>

<div class="wp_syntax"><div 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></span>
<span style="color: #000000;">        xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span></span>
<span style="color: #000000;">        xmlns:Degrafa=<span style="color: #ff0000;">&quot;http://www.degrafa.com/2007&quot;</span></span>
<span style="color: #000000;">        layout=<span style="color: #ff0000;">&quot;absolute&quot;</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;Degrafa:GeometryComposition</span> graphicsTarget=<span style="color: #ff0000;">&quot;{[cnv]}&quot;</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;Degrafa:Path</span> data=<span style="color: #ff0000;">&quot;M 0,0 L 100,0 L 100,100 L 0,100 z&quot;</span><span style="color: #7400FF;">&gt;</span></span>
	        <span style="color: #000000;"><span style="color: #7400FF;">&lt;Degrafa:fill</span><span style="color: #7400FF;">&gt;</span></span>
	            <span style="color: #000000;"><span style="color: #7400FF;">&lt;Degrafa:SolidFill</span> color=<span style="color: #ff0000;">&quot;#EECCEE&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
	        <span style="color: #000000;"><span style="color: #7400FF;">&lt;/Degrafa:fill</span><span style="color: #7400FF;">&gt;</span></span>
	        <span style="color: #000000;"><span style="color: #7400FF;">&lt;Degrafa:stroke</span><span style="color: #7400FF;">&gt;</span></span>
	            <span style="color: #000000;"><span style="color: #7400FF;">&lt;Degrafa:SolidStroke</span> color=<span style="color: #ff0000;">&quot;#FF00FF&quot;</span> weight=<span style="color: #ff0000;">&quot;3&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
	        <span style="color: #000000;"><span style="color: #7400FF;">&lt;/Degrafa:stroke</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;/Degrafa:Path</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/Degrafa:GeometryComposition</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Canvas</span> id=<span style="color: #ff0000;">&quot;cnv&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Application</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p class="bottom"><b>MXML Graphics</b> (aka FXG) in Flex 4:</p>

<div class="wp_syntax"><div 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;s:Application</span></span>
<span style="color: #000000;">        xmlns:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span></span>
<span style="color: #000000;">        xmlns:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span></span>
<span style="color: #000000;">        xmlns:mx=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/halo&quot;</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Graphic</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Path</span> data=<span style="color: #ff0000;">&quot;M 0,0 L 100,0 L 100,100 L 0,100 z&quot;</span><span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:fill</span><span style="color: #7400FF;">&gt;</span></span>
                <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:SolidColor</span> color=<span style="color: #ff0000;">&quot;#EECCEE&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:fill</span><span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:stroke</span><span style="color: #7400FF;">&gt;</span></span>
                <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:SolidColorStroke</span> color=<span style="color: #ff0000;">&quot;#FF00FF&quot;</span> weight=<span style="color: #ff0000;">&quot;3&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:stroke</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Path</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Graphic</span><span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Application</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>The examples above use the shorthand path syntax to draw a 100px square that starts at the coordinate origin (0,0), which is the upper left corner in SVG and Flex.  Beware the coordinate origin when translating <a href="http://saturnboy.com/2009/06/inkscape-svg-degrafa-path/">Inkscape SVG to Flex</a>. The other interesting thing to note is the amazing similarity between Degrafa and FXG.  Who knew all my time learning Degrafa will instantly translate to Flex 4 and FXG?  Awesome!</p>
<h5>SVG Path Shorthand</h5>
<p class="bottom">Here&#8217;s a quick overview of shorthand syntax for SVG path data:</p>
<hr class="bottom" style="height: 3px;" />
<div class="span-3"><b>Move</b></div>
<div class="span-4"><b>M</b> &lt;x,y&gt;</div>
<div class="span-6 last">Move the pen to the given point.</div>
<hr style="background-color:#aaa;" class="bottom" />
<div class="span-3"><b>Line</b></div>
<div class="span-4"><b>L</b> &lt;x,y&gt;+</div>
<div class="span-6 last">Draw a line to given point.  Multiple points may be specified to draw polyline.</div>
<hr style="background-color:#aaa;" class="bottom" />
<div class="span-3"><b>Horizontal Line</b></div>
<div class="span-4"><b>H</b> &lt;x&gt;</div>
<div class="span-6 last">Draw a horizontal line to given coordinate.</div>
<hr style="background-color:#aaa;" class="bottom" />
<div class="span-3"><b>Vertical Line</b></div>
<div class="span-4"><b>V</b> &lt;y&gt;</div>
<div class="span-6 last">Draw a vertical line to given coordinate.</div>
<hr style="background-color:#aaa;" class="bottom" />
<div class="span-3"><b>Quadratic Bezier</b></div>
<div class="span-4"><b>Q</b> &lt;cx,cy x,y&gt;+</div>
<div class="span-6 last">Draw a quadratic Bezier curve to given coordinate using a control point.  Multiple Beziers may be specified to draw polycurve.</div>
<hr style="background-color:#aaa;" class="bottom" />
<div class="span-3"><b>Cubic Bezier</b></div>
<div class="span-4"><b>C</b> &lt;cx1,cy1 cx2,cy2 x,y&gt;+</div>
<div class="span-6 last">Draw a cubic Bezier curve to given coordinate using two control points.  Multiple Beziers may be specified to draw a polycurve.</div>
<hr style="background-color:#aaa;" class="bottom" />
<div class="span-3"><b>Arc</b></div>
<div class="span-4"><b>A</b> &lt;rx,ry rot,lrg,swp x,y&gt;</div>
<div class="span-6 last">Draw elliptical arc to the given point.</div>
<hr style="background-color:#aaa;" class="bottom" />
<div class="span-3"><b>Close</b></div>
<div class="span-4"><b>Z</b></div>
<div class="span-6 last">Close the path.</div>
<hr style="height: 3px;" />
<p>Alas, the beta version of FXG does not support the Arc segment type, which I suspect is due to lack of support for arbitrary arcs in the underlying Flash Player rendering engine but I don&#8217;t know for sure.  Thankfully, Degrafa offers full arc support (<i>thanks Greg!</i>).  If you really need to draw arcs in FXG, for stuff like pie wedges, and are unafraid of getting into a cage match with your trigonometry textbook, you can do a good job approximating arcs with cubic Bezier curves.  Alternately, you can just use Degrafa once it gets ported to Flex 4.  Lastly, using uppercase for the segment type specifies absolute coordinates.  This is the format commonly used by Illustrator and <a href="http://www.inkscape.org/">Inkscape</a> when exporting drawings to SVG.  One can easily switch to relative coordinates by just switching the commands to lowercase, but I would try to avoid it if at all possible as it tends to make one&#8217;s head hurt.</p>
<h5>The Many Shapes of a Square</h5>
<p class="bottom">All of the squares above, use this shorthand data:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">M 0,0 L 100,0 L 100,100 L 0,100 z</pre></div></div>

<p>First, a <b>M</b>ove to set the pen at the origin.  Then, a <b>L</b>ine right to (100,0), followed by a <b>L</b>ine down to (100,100), followed by a <b>L</b>ine left to (0,100).  Then, a close (<b>z</b>) to return to the origin.</p>
<p class="bottom">I can drop the commas if I want:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">M 0 0 L 100 0 L 100 100 L 0 100 z</pre></div></div>

<p class="bottom">Or drop all but the first <b>L</b>ine to make a polyline:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">M 0,0 L 100,0 100,100 0,100 z</pre></div></div>

<p class="bottom">Or use <b>H</b>orizontal Line and <b>V</b>ertical Line:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">M 0,0 H 100 V 100 H 0 z</pre></div></div>

<p class="bottom">Or even use relative coordinate (which makes my head hurt a little):</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">m 0,0 l 100,0 l 0,100 l -100,0 z</pre></div></div>

<h5>Curves</h5>
<p class="bottom">Straight lines are cool, but the real fun in life lies in the curves.  Cubic Bezier curves should be very familiar to anyone who&#8217;s used a vector drawing program.  Let&#8217;s replace the first segment in our square with a cubic Bezier segment.  Now, the SVG shorthand becomes:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">M 0,0 C 25,-25 50,25 100,0 L 100,100 L 0,100 z</pre></div></div>

<p class="bottom">When rendered, we get this:</p>
<div class="span-14 top bottom last" style="min-height:325px;">
<img src="http://saturnboy.com/wp-content/uploads/2009/06/square-funny.png" alt="square-funny" title="square-funny" width="408" height="315" />
</div>
<p>The shorthand command says curve to (100,0), but start out heading towards control point #1 at (25,-25) and end up coming in from control point #2 at (50,25).  </p>
<h5>Arcs</h5>
<p class="bottom">Again, let&#8217;s replace the first segment in our square with an arc segment.  Now, the SVG shorthand becomes:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">M 0,0 A 50,25 0 0,1 100,0 L 100,100 L 0,100 z</pre></div></div>

<p class="bottom">When rendered, we get this:</p>
<div class="span-14 top bottom last" style="min-height:325px;">
<img src="http://saturnboy.com/wp-content/uploads/2009/06/square-arc.png" alt="square-arc" title="square-arc" width="408" height="315" />
</div>
<p>The shorthand command says arc to (100,0), with an x-radius of 50 and a y-radius of 25, with a rotation of 0.  The large-arc and sweep flags are a little confusing so you&#8217;ll want to review the <a href="http://www.w3.org/TR/SVG11/paths.html#PathDataEllipticalArcCommands">SVG Spec, &sect; 8.3.8</a> if you need to get down and dirty with arcs.  </p>
<h5>Conclusion</h5>
<p>I&#8217;m a firm believer in &#8220;right tool for the job.&#8221;  So, when in comes to getting SVG path data into Flex, I&#8217;m definitely going to use Illustrator or Inkscape as much as possible, and in the future I might just use Catalyst for everything.  But there are a few important situations where the Flex developer absolutely must know SVG.  First, if you want to do any kind of path morphing (<a href="http://www.degrafa.org/blog/2009/06/say-hello-to-supershape2d/">like this</a>), you&#8217;ll need precision control over your path segments.   And second, if you want to do any dynamic path generation (like building a multi-level radial menu on the fly &ndash; which sounds like a good topic for a future post), you&#8217;ll need to manually construct your SVG paths.</p>
<h5>Files</h5>
<ul>
<li><a href="http://saturnboy.com/wp-content/uploads/2009/06/square-path.svg">square-path.svg</a></li>
<li><a href="http://saturnboy.com/wp-content/uploads/2009/06/square-curve.svg">square-curve.svg</a></li>
<li><a href="http://saturnboy.com/wp-content/uploads/2009/06/square-arc.svg">square-arc.svg</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2009/06/svg-primer-for-flex/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>

