﻿<?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; inverted mask</title>
	<atom:link href="http://blog.mattiasnorell.com/tag/inverted-mask/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>Inverted Mask &#8211; The latest and greatest. A sneak peek.</title>
		<link>http://blog.mattiasnorell.com/2010/11/02/inverted-mask-the-latest-and-greatest-a-sneak-peek/</link>
		<comments>http://blog.mattiasnorell.com/2010/11/02/inverted-mask-the-latest-and-greatest-a-sneak-peek/#comments</comments>
		<pubDate>Mon, 01 Nov 2010 23:36:40 +0000</pubDate>
		<dc:creator>Mattias</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[inverted mask]]></category>

		<guid isPermaLink="false">http://blog.mattiasnorell.com/?p=1536</guid>
		<description><![CDATA[Ok, the latest and greatest might be pushing it a bit. But I needed to update my inverted mask AS3-lib so that it supported animated masks, moving backgrounds et cetera. The code isn´t really ready for prime time use quite &#8230; <a href="http://blog.mattiasnorell.com/2010/11/02/inverted-mask-the-latest-and-greatest-a-sneak-peek/">Läs mer <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Ok, the latest and greatest might be pushing it a bit. But I needed to update my inverted mask AS3-lib so that it supported animated masks, moving backgrounds et cetera. The code isn´t really ready for prime time use quite yet but this is a little demo of the new and improved inverted mask-class. The demo below use your webcam as one source and a jpeg as the second one. To swap between the inverted and normal mask, click anywhere inside the swf.</p>
<p>Maby not so fun fact. The picture, depicting the Brighton pier, was shot during Flash on the beach´10.</p>
<p>
<object width="640" height="480">
<param name="movie" value="http://blog.mattiasnorell.com/wp-content/uploads/2010/11/InvertedWebcam.swf"></param>
<param name="quality" value="high"></param>
<param name="wmode" value="window"></param>
<param name="menu" value="false"></param>
<param name="bgcolor" value="#FFFFFF"></param>
<param name="allowScriptAccess" value="always"></param>
<embed type="application/x-shockwave-flash" width="640" height="480" src="http://blog.mattiasnorell.com/wp-content/uploads/2010/11/InvertedWebcam.swf" quality="high" bgcolor="#FFFFFF" wmode="window" menu="false" ></embed>
</object>
</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mattiasnorell.com/2010/11/02/inverted-mask-the-latest-and-greatest-a-sneak-peek/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pure ActionScript 3 inverted mask 2.0</title>
		<link>http://blog.mattiasnorell.com/2010/08/21/pure-actionscript-3-inverted-mask-2/</link>
		<comments>http://blog.mattiasnorell.com/2010/08/21/pure-actionscript-3-inverted-mask-2/#comments</comments>
		<pubDate>Sat, 21 Aug 2010 00:00:05 +0000</pubDate>
		<dc:creator>Mattias</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Adobe]]></category>
		<category><![CDATA[In english]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[inverted mask]]></category>

		<guid isPermaLink="false">http://blog.mattiasnorell.com/?p=972</guid>
		<description><![CDATA[Update: I´ve rewritten the inverted mask-class from scratch and moved it here. It now support animated movieclips, video, webcam-feed and pretty much everything eles. I think. About ten months ago I wrote a little as3-class that took care of something &#8230; <a href="http://blog.mattiasnorell.com/2010/08/21/pure-actionscript-3-inverted-mask-2/">Läs mer <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<blockquote><p>Update: I´ve rewritten the inverted mask-class from scratch and moved it <a href="http://blog.mattiasnorell.com/as3-libs/inverted-mask/">here</a>. It now support animated movieclips, video, webcam-feed and pretty much everything eles. I think.</p></blockquote>
<p>About ten months ago I wrote a little as3-class that took care of something I think Adobe should have implemented in the FlashPlayer a long time ago, the inverted mask. As time went by I&#8217;ve added some features I found useful while working on projects at work. In this version you can swap between inverted and normal masks, hide/show the mask and remove it from memory.</p>
<p>Finally I just want to say: Adobe, I love Flash but I still think this is way to complicated. A simple <em>DisplayObject.invertedMask = theMask;</em> would be just fine. I know it&#8217;s easy to say &#8221;just do it&#8221; but this is a feature a lot of developers would find useful.</p>
<h3><a href="http://blog.mattiasnorell.com/wp-content/uploads/2010/08/Main.swf" rel="shadowbox[sbpost-972];width=640;height=385;">Demo</a></h3>
<h3>The InvertedMask.as-class</h3>
<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;

/**
* Inverted Mask
* Version 2.0
*
* ...
* 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/2010/08/21/pure-actionscript-3-inverted-mask-2/
*
*/

public class InvertedMask extends Sprite{

private var inverted:Sprite;
private var dup:BitmapData;
private var newAlpha:Bitmap;
private var isInverted:Boolean;

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

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

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

$mask.parent.removeChild($mask);

$parent.x=$parent.y = 0;

inverted.addChild($parent);
inverted.addChild(newAlpha);

inverted.blendMode = BlendMode.LAYER;

addChild(inverted);

isInverted = true;
}

public function moveMask($target:Object,$maskPos:Object):void {
$target.getChildAt(0).getChildAt(1).x  = $maskPos.x;
$target.getChildAt(0).getChildAt(1).y  = $maskPos.y;
}

public function Show():void{
inverted.blendMode = BlendMode.LAYER;
}

public function Hide():void{
inverted.blendMode = BlendMode.NORMAL;
}

public function autoShowHide():void{
(inverted.blendMode == BlendMode.LAYER) ? Hide() : Show();
}

public function swapMaskType($type:String = "auto"):void{
if($type == 'auto'){
$type = (isInverted == true) ? 'normal' : 'inverted';
}

if($type == 'inverted'){
Show();
inverted.mask = null;
isInverted = true;
}else if($type == 'normal'){
Hide();
inverted.mask = newAlpha;
isInverted = false;
}else{
trace("You must use either 'inverted','normal' or 'auto'.");
}
}

public function remove():void{
inverted.mask = null;
inverted.blendMode = BlendMode.NORMAL;

newAlpha.bitmapData.dispose();
newAlpha = null;
dup.dispose();
dup = null;
}
}
}</pre>
<h3>Source for the demo swf</h3>
<p>When I code I use Flash Develop, hence the [Embed]-code below. If you use the Flash IDE just remove those two rows and import the image from the library.</p>
<pre class="brush: actionscript3; font-size: 50%">package{
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	public class Main extends Sprite{

		[Embed(source="image.jpg")]
		public var imgCls:Class;

		public function Main():void{
			var img:Sprite = new Sprite();
			img.addChild(new imgCls());
			addChild(img);

			var mask:Sprite = new Sprite();
			mask.graphics.beginFill(0xFF0000,1);
			mask.graphics.drawRect(0,0,30,30);
			mask.graphics.endFill();
			addChild(mask);

			var newMask:InvertedMask = new InvertedMask(img, mask, { x:0, y:0 } );
			addChild(newMask);

			newMask.addEventListener(MouseEvent.MOUSE_MOVE, moveMask);
			newMask.addEventListener(MouseEvent.CLICK, swap);
		}

		private function swap(e:MouseEvent):void {
			e.currentTarget.swapMaskType();
		}

		private function moveMask(e:MouseEvent):void {
			e.currentTarget.moveMask(e.currentTarget, { x:mouseX-10, y:mouseY-10 } );
		}
	}
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.mattiasnorell.com/2010/08/21/pure-actionscript-3-inverted-mask-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>

