Active Window Blur

I was checking out a website yesterday when it suddenly reminded me of a Pixelfumes experiment I remembered seeing many moons ago. After finding it with a quick Google search, just for kicks, I decided to play around and update the idea to AS3. I didn’t bother with the constant updating to accomadate video as that seemed a tad processor hungry, but it wouldn’t be difficult to implement at all. What I would rather do with it is make it possible to open several windows at once all masking the same background image. Maybe later on tonight.

Anyway here’s the script for interested parties. An example follows below. Thank you to Pixelfumes for the original idea and AS2 script.

WindowBlur.as:

package com.onebyonedesign.extras {
 
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.BlurFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
 
	/**
	* Blurs a background (MovieClip or Sprite) behind a transparent window (MovieClip or Sprite).
	* Based on Pixelfumes AS2 class
	* 
	* @author Devon O.
	*/
	public class WindowBlur {
 
		private var _background:DisplayObjectContainer;
		private var _window:DisplayObjectContainer;
 
		private var _blurredImageData:BitmapData;
		private var _blurredImage:Bitmap;
		private var _mask:DisplayObjectContainer;
		private var _blurAmount:int;
 
		private var _blur:BlurFilter;
		private var _point:Point = new Point();
 
		/**
		 * 
		 * @param	background		MovieClip or Sprite which will be blurred behind window.
		 * @param	window			(Semi) transparent MovieClip or Sprite behind which will be a blurred background.
		 * @param	blurAmount		The amount of blur to apply to background image.
		 * 
		 * NOTE: background and window objects *must* be added to display list before instantiating an instance of this class.
		 * TODO: allow multiple window instances with same background.
		 * FLASH BUG: 	If window instance is created programatically, it cannot have filters applied.
		 * 				If window instance is linked to MovieClip in library it CAN have filters applied.
		 */
		public function WindowBlur(background:DisplayObjectContainer, window:DisplayObjectContainer, blurAmount:int = 8 ):void {
			_background = background;
			_window = window;
			_blurAmount = (blurAmount >= 0 && blurAmount < = 255) ? blurAmount : 16;
			_blur = new BlurFilter(_blurAmount, _blurAmount, 3);
 
			initBlur();
			initMask();
		}
 
		private function initBlur():void {
			_blurredImageData = new BitmapData(_background.width, _background.height, false);
			_blurredImageData.draw(_background);
			_blurredImageData.applyFilter(_blurredImageData, _blurredImageData.rect, _point, _blur);
			_blurredImage = new Bitmap(_blurredImageData);
 
			_background.addChild(_blurredImage);
		}
 
		private function initMask():void {
			var MaskClass:Class = Object(_window).constructor;
			_mask = new MaskClass();
			_mask.filters = _window.filters;
			_blurredImage.mask = _mask;
			_mask.visible = false;
 
			_window.addChildAt(_mask, 0);
		}
 
		private function update():void {
			_background.removeChild(_blurredImage);
			_blurredImageData.dispose();
			initBlur();
			_blurredImage.mask = _mask;
		}
 
		public function kill():void {
			_background.removeChild(_blurredImage);
			_window.removeChild(_mask);
 
			_blurredImageData.dispose();
 
			_blurredImage = null;
			_blurredImageData = null;
			_mask = null;
			_blur = null;
		}
 
		public function get blurAmount():int { return _blurAmount; }
 
		public function set blurAmount(value:int):void {
			_blurAmount = value;
			_blur.blurX = _blur.blurY = _blurAmount;
			update();
		}
	}
}

Main.as (document class):

package {
 
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import com.onebyonedesign.extras.WindowBlur;
	import com.onebyonedesign.ui.OBO_ValueSlider;
	import com.onebyonedesign.ui.events.ValueSliderEvent;
	import flash.geom.Rectangle;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
 
	/**
	* Demonstrates WindowBlur Class
	* @author Devon O.
	*/
	public class Main extends Sprite {
 
		// GraphicWindow is MovieClip in .fla library
		private var _window:GraphicWindow;
		// Background is MovieClip in .fla library
		private var _bg:Background;
		private var _windowBlur:WindowBlur;
		private var _shadow:DropShadowFilter = new DropShadowFilter(2, 90, 0x000000, 1, 2, 2, 1, 3);
		private var _dragBounds:Rectangle;
 
		public function Main():void {
			// Background is MovieClip in .fla library
			_bg = new Background();
			addChild(_bg);
			init();
		}
 
		private function init():void {
			initWindow();
			initBlur();
			initControlPanel();
		}
 
		private function initWindow():void {
			// GraphicWindow is MovieClip in library
			_window = new GraphicWindow();
			_window.x = int(stage.stageWidth * .5 - _window.width * .5);
			_window.y = int(stage.stageHeight * .5 - _window.height * .5);
			_window.filters = [_shadow];
 
			_dragBounds = new Rectangle(0, 0, stage.stageWidth - _window.width, stage.stageHeight - _window.height);
 
			addChild(_window);
			_window.addEventListener(MouseEvent.MOUSE_DOWN, pressHandler);
		}
 
		private function initBlur():void {
			_windowBlur = new WindowBlur(_bg, _window);
		}
 
		private function initControlPanel():void {
			var cp:Sprite = new Sprite();
			cp.graphics.beginFill(0xEAEAEA);
			cp.graphics.drawRoundRect(0, 0, 150, 30, 10, 10);
			cp.graphics.endFill();
			cp.filters = [_shadow];
			cp.x = 10;
			cp.y = stage.stageHeight - cp.height - 10;
 
			var tf:TextField = new TextField();
			var fmt:TextFormat = new TextFormat("_sans", 11);
			tf.defaultTextFormat = fmt;
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.selectable = false;
			tf.mouseEnabled = false;
			tf.antiAliasType = AntiAliasType.ADVANCED;
			tf.text = "Blur amount:";
			tf.x = 5;
			tf.y = 5;
			cp.addChild(tf);
 
			var blurSlider:OBO_ValueSlider = new OBO_ValueSlider(tf.textWidth, 0, 64, _windowBlur.blurAmount);
			blurSlider.x = tf.x + tf.textWidth + 8;
			blurSlider.y = 13;
			blurSlider.addEventListener(ValueSliderEvent.DRAG, blurChangeHandler);
			cp.addChild(blurSlider);
 
			addChild(cp);
		}
 
		private function blurChangeHandler(event:ValueSliderEvent):void {
			_windowBlur.blurAmount = int(event.value);
		}
 
		private function pressHandler(event:MouseEvent):void {
			stage.addEventListener(MouseEvent.MOUSE_UP, releaseHandler);
			_window.startDrag(false, _dragBounds);
		}
 
		private function releaseHandler(event:MouseEvent):void {
			stage.removeEventListener(MouseEvent.MOUSE_UP, releaseHandler);
			_window.stopDrag();
		}
	}
 
}

49 Comments »

  1. [...] taking another look at this post, I came up with a way of making multiple windows that will all blur the same image. I have to [...]

  2. Ben says:

    Nice work! I’ll have to do a cross post on this one ;)

  3. [...] saw a nice post over at the onebyoneblog that details their AS3 Class inspired by my old AS2 experiment for “Active Blurring” [...]

  4. Marc Pelland says:

    Looks great ! I was looking for a way to do this just the other day, thanks

  5. Devon O. says:

    Thank you for the comments, Ben and Marc. You might also want to check out the post after the next post (“Active Window(S) Blur”).. I came up with a way that will allow multiple windows blurring the same background. Uses an enter frame event though which I’m not really happy about..

  6. [...] recorded first by aresnick on 2008-07-31→ onebyoneblog » Active Window Blur [...]

  7. [...] taking another look at this post, I came up with a way of making multiple windows that will all blur the same image. I have to [...]

  8. Yves says:

    Hello,
    Very good effect =)
    In the last week, I have to make this effect in a
    temporarily website.
    Today, I see your blog and I wish to kill me….lol
    My blur mask is very good too, but i did doubling my image with blur and applying a mask.
    Congratulations, your blog is very nice.
    Sorry my terrible English …lol

  9. Devon O. says:

    Hello Yves.. Doubling the image and using a mask is essentially the same thing I did here. The effect on your site looks quite nice..
    Thank you for the comments. And I can guarantee you that your English is much better than my Portuguese..

  10. Skye says:

    NOTE:

    If you copy and paste from the WindowBlur.as source above it will not compile.

    This line has a space between the < and the = in the = 0 && blurAmount = 0 && blurAmount <= 255) ? blurAmount : 16;

  11. Skye says:

    Doh, it appears the comment submission didn’t escape the greaterthan and less than characters. Let’s try again:
    NOTE:
    If you copy and paste from the WindowBlur.as source above it will not compile.
    This line has a space between the < and the = in the <= comparison operator:
    _blurAmount = (blurAmount >= 0 && blurAmount < = 255) ? blurAmount : 16;

    It should read:
    _blurAmount = (blurAmount >= 0 && blurAmount <= 255) ? blurAmount : 16;

  12. Andrew says:

    Hi. Great class, it;s saved me a ton of hassle. Any ideas though why only a small section of my stage gets blurred? Is there something I need to do to alter the size of the allowed blur area?

    But yeah, sweet class. :)

  13. Jonathan says:

    Nice class – how do I get it to work over a video? I’ve tried h264 (NetStream), FLV (FLVPlayback Component and embedded on stage) – and it shows up as a solid square when dragged over the video. Any advice?

    Thanks

  14. t says:

    Is there an FLA download

  15. tim says:

    Hey folks,
    When I use an fla to call the Main.as I get two errors in the WindowBlur.as

    **Error** WindowBlur.as, Line 45: 1084: Syntax error: expecting identifier before assign.
    _blurAmount = (blurAmount >= 0 && blurAmount < = 255) ? blurAmount : 16;

    **Error** WindowBlur.as, Line 46: 1084: Syntax error: expecting rightparen before _blur.
    _blur = new BlurFilter(_blurAmount, _blurAmount, 3);

    can anyone help

  16. Devon O. says:

    there is a download here: http://www.onebyonedesign.com/downloads/window_blur.zip – it doesn’t include the ValueSlider however. You can either replace it with your own component or download the full UI library here: http://www.onebyonedesign.com/flash/ui/ . Hope it helps out..

  17. Villmer says:

    Outstanding work. It apparently does not work over video however. Has this been fixed?

  18. Devon O. says:

    I don’t know if it’s a case of considering it “fixed” or not as it was never intended to work with video. Check out the link to the Pixelfumes blog at the top of the post. His AS2 version works with video – you could just do a port to AS3 as I did..

  19. MUX says:

    this is a great port to as3 of the mighty pixelfumes code.
    i was planning to do something similar in one of my as3 projects and google brought me to you.

    i tried looking at the example zip file & it gives a ‘unexpected file format’ error.

    any chnage a repost of the zip ? or is it just me.. :o)

  20. MUX says:

    ps. dont worry i got your second example to work..
    great job..

  21. Davide says:

    Very nice code.

    Could i have, please, a .fla version where the main.as class is not linked in the document property, where i can call directly from the actionscript code?.

    Thank you very much

  22. I m trying to implement this effect on one of my websites, I do not have access to the libraries

    import com.onebyonedesign.ui.OBO_ValueSlider;
    import com.onebyonedesign.ui.events.ValueSliderEvent;

    Is there anyway to import them from somewhere..I think the effect is worth it

  23. Devon O. says:

    Hey Carlos, those are classes from a package of ui items I put together long ago. You can see examples of all the items and download them here: http://www.onebyonedesign.com/flash/ui/

  24. BHJodoKast says:

    I don’t think the OBO classes work…

    I posted about them in Devon’s Kirupa Source/Experiments page, but no response.

  25. Devon O. says:

    which classes are you having problems with, BHJodoKast? All of them should work fine.

  26. [...] Active Window Blur 这个有点像flex中Alert后模糊的背景。 [...]

  27. 92Garfield says:

    _blurAmount = (blurAmount >= 0 && blurAmount = 0 && blurAmount <= 255) ? blurAmount : 16;

    also, unfortunaly this doesnt work with multiple windows masking the same bg yet..

  28. Devon O. says:

    hey 92garfield. I’m not sure what is meant by the first question but for multiple windows on the same bg, try this post: http://blog.onebyonedesign.com/actionscript/active-windows-blur/

  29. [...] Active Window Blur 这个有点像flex中Alert后模糊的背景。 [...]

  30. as518.com says:

    [...] Active Window Blur 这个有点像flex中Alert后模糊的背景。 [...]

  31. [...] Active Window Blur 这个有点像flex中Alert后模糊的背景。 [...]

  32. [...] Active Window Blur 这个有点像flex中Alert后模糊的背景。 [...]

  33. [...] Active Window Blur – http://blog.onebyonedesign.com/?p=81 [...]

  34. [...] Active Window Blur 这个有点像flex中Alert后模糊的背景。 [...]

RSS feed for comments on this post. / TrackBack URI

Leave a Reply

Devon O. Wolfgang

Technical Reviewer of “The Essential Guide to Flash CS4 AIR Development”

Contributing Author of “Flash AS3 for Interactive Agencies”

Senior Flash Engineer PopCap Games, International Ltd.

Portfolio

Santabot: A Unity3D Flash Game


All right, so a Christmas game like “Santabot vs. The Flying Saucers from Mars” may be a day late[...]

Magnify – a jQuery Plugin


Let me begin by saying right up front, I have not given up on Flash[...]

It’s a Starling Halloween


Getting some practice for the upcoming Zombie Apocalypse[...]

Getting Started with Proscenium

So I had a chance this weekend to sit down and play around with Adobe’s new 3D framework for Stage3D, Proscenium, and thought I’d share a few of the results (a word of caution, there are no preloaders for any of the examples and may load a bit slowly). The first shows some reflections and [...]

Particle Editor for Starling Framework

An in-browser particle editor for the Starling 2D Framework for Flash Player 11.

So Long and Thanks for all the Flash on the Beach

So, another Flash on the Beach has just has just drawn to a close[...]

Game Development Tips from the Trenches of PopCap

Well, this is a post that’s a bit overdue, but, thanks to a well timed bank holiday, I finally had the opportunity to sit down and type up what I’ve been meaning to for some time now…

Old Skool Demoscene FX as 3D Textures

Many moons ago, I got the idea to create some demoscene plane deformation effects in Flash based on the formulas found here: http://www.iquilezles.org/www/articles/deform/deform.htm. I posted my less than desired results up on wonderfl. Thankfully, fellow wonderfl user, Hasufel, forked my attempt and optimized the hell out of it coming up with this. Well, today, for no [...]

Making The Gaming Scene (A Change in Careers)

And for my second blog post of the day, a much more personal note. After about three years of working with, what I would consider as objectively as possible, the best digital agency in Ireland, vStream Digital Media, I have made the immense career decision to leave the agency world and enter the arena of [...]

Feeling Lucky?

Images to dice, kick ascii style [...]

Adventures in Playbook Land

Adventures in Playbook Land


Now that the ordeal is over, I thought I’d take the time to sit down and share my account of what it was like to develop a Blackberry Playbook application using the Adobe Flex SDK[...]

Flash

Draw it for Me

So many ideas – so little time….

Kinect Application Running in Dublin

So, on Friday I just wrapped up our latest project at vStream Digital Media, a Kinect powered flipbook that lets users flip through the hand written notebooks of Philip Lynott of Thin Lizzy. The app uses OpenNI, runs in Adobe AIR and is currently on display at a pretty bitchin’ Phil Lynott exhibition running in [...]

Beach Ball Kinect Party

So we finally got a Kinect camera hooked up to a pc at work and, while it doesn’t seem to be legal to use it for commercial projects (but, hey, I’m no lawyer), the boss asked me to get it figured out and come up with some ideas just in case it would be feasible [...]

Facebook and Flash – A Book Review

Now, I should begin by saying I absolutely hate building Facebook applications. And I build a lot of them at work. Every time I get the word from above that we’re doing another FB app, I just groan – both inwardly and out. It’s become a running joke of the office. Why do I dislike [...]

Multitouch Fluid Dynamics with AIR for Android and RTMFP

The other day I was having some fun playing around with Eugene Zatepyakin’s (aka @inspirit) FluidSolverHD (Actionscript port of C++ fluid dynamics library, MSAFluid. Or is MSAFluid, the processing/java port of the C++ library? In any case it’s a very cool fluid dynamics thingamabob – the HD version using Alchemy). After a bit of tinkering, [...]