﻿<?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>blog.mattiasnorell.com &#187; class</title>
	<atom:link href="http://blog.mattiasnorell.com/tag/class/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mattiasnorell.com</link>
	<description>Otukt i otakt</description>
	<lastBuildDate>Tue, 07 Feb 2012 22:58:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Pure ActionScript 3 inverted mask</title>
		<link>http://blog.mattiasnorell.com/2009/10/21/pure-actionscript-3-inverted-mask/</link>
		<comments>http://blog.mattiasnorell.com/2009/10/21/pure-actionscript-3-inverted-mask/#comments</comments>
		<pubDate>Wed, 21 Oct 2009 18:01:59 +0000</pubDate>
		<dc:creator>Mattias</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[In english]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[inverted mask]]></category>
		<category><![CDATA[pure as3]]></category>

		<guid isPermaLink="false">http://blog.mattiasnorell.com/?p=204</guid>
		<description><![CDATA[Update: I&#8217;ve updated the InvertedMask-class with a few new features. You can find the new and updated version here. I love masks. I really do. I use them alot and I&#8217;m proud of it. But a lot of times I &#8230; <a href="http://blog.mattiasnorell.com/2009/10/21/pure-actionscript-3-inverted-mask/">Läs mer <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<blockquote><p>Update: I&#8217;ve updated the InvertedMask-class with a few new features. You can find the new and updated version <a href="http://blog.mattiasnorell.com/2010/08/21/pure-actionscript-3-inverted-mask-2/" target="_self">here</a>.</p></blockquote>
<p>I love masks. I really do. I use them alot and I&#8217;m proud of it. But a lot of times I need to make something I think Adobe should put into Flash as a native alternative to the regular mask. And that is the inverted mask.</p>
<p>I know more people than me miss this feature and I for one would love to see something like <em>DisplayObject.invertedMask = MaskObject;</em> but its no where to be found unless you want to make your own AS3-solution, which I think is more or less a workaround for something that should have been there from the start.</p>
<p>I&#8217;ve seen some people using blendmodes to achive the same effect like the one from Rachel Diesel (<a href="http://dieselation.com/2009/04/11/inverting-a-mask-in-actionscript-3/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/dieselation.com/2009/04/11/inverting-a-mask-in-actionscript-3/?referer=');">http://dieselation.com/2009/04/11/inverting-a-mask-in-actionscript-3/</a>) but most of the time they are not pure as3 so I modified her solution into a more reusable piece of code. And I also added the function moveMask() which allowes you to move the inverted mask after it has been added to the stage with out any hazzle.</p>
<p>It&#8217;s not perfect and I bet that a lot of improvements can be done but you can <a rel="shadowbox;height=600;width=400;" href="/wp-content/uploads/2009/10/invertedmask.swf">try out the demo here</a>. And yes, that is a pink elephant in a fake Guccibag.</p>
<pre class="brush: actionscript3; font-size: 50%">package com.mattiasnorell.bitmap
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	/**
	 * ...
	 * Original author:	Rachel Diesel
	 * 					http://dieselation.com/2009/04/11/inverting-a-mask-in-actionscript-3/
	 *
	 * Pure AS3 code	Mattias Norell
	 * 					http://blog.mattiasnorell.com/2009/10/21/pure-actionscript-3-inverted-mask/
	 *
	 */

	public class InvertedMask extends Sprite{

		public function InvertedMask($parent:DisplayObject,$mask:DisplayObject,$maskPos:Object):void {
			var inverted:Sprite = new Sprite();
			inverted.x = $parent.x;
			inverted.y = $parent.y;

			var dup:BitmapData = new BitmapData($mask.width, $mask.height, true, 0xffffff);
			dup.draw($mask);

			var newAlpha:Bitmap = new Bitmap(dup, "auto", true);
			newAlpha.x = $maskPos.x;
			newAlpha.y = $maskPos.y;
			newAlpha.blendMode = BlendMode.ERASE;

			$mask.parent.removeChild($mask);

			inverted.addChild($parent);
			$parent.x=$parent.y = 0;
			inverted.addChild(newAlpha);
			inverted.blendMode = BlendMode.LAYER;

			addChild(inverted);
		}

		public function moveMask($target:Object,$maskPos:Object):void {
			$target.getChildAt(0).getChildAt(1).x  = $maskPos.x;
			$target.getChildAt(0).getChildAt(1).y  = $maskPos.y;
		}
	}
}</pre>
<p>To activate it you just copy/paste this into your project.<br />
The first parameter is the object you want to mask, the second is the mask itself and in the third parameter you tell the mask where along the x and y-axis it should go.</p>
<pre class="brush: actionscript3; font-size: 50%">var newMask:InvertedMask = new InvertedMask(OBJECT_TO_BE_MASKED, THE_MASK, { x:0, y:0 } );
addChild(newMask);</pre>
<p><strong>What about moving the mask?</strong><br />
If you add an eventListener to the InvertedMask-object and call the function below it will move your mask in anyway you want.</p>
<pre class="brush: actionscript3; font-size: 50%">newMask.addEventListener(MouseEvent.MOUSE_MOVE, moveMask);

private function moveMask(e:MouseEvent):void {
    e.currentTarget.moveMask(e.currentTarget, { x:mouseX-10, y:mouseY-10 } );
}</pre>
<p>So, all credits for the solution goes to Rachel, I&#8217;ve just tweaked it a little to suit my needs and as I said, it&#8217;s not perfect but it&#8217;s a start.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mattiasnorell.com/2009/10/21/pure-actionscript-3-inverted-mask/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

