Move the Mouse to Reveal the Poster

In this tutorial, learn how to reveal a poster piece by piece when the user hovers it.

View DemoDownload Source

1. This tutorial is a variation of our previous tutorial entitled “Revealing an Image with a Grid Effect Animation”, so first go check it to get the main explanations as we’ll go quickly here.

2. Create a new flash file (Actionscript 3.0) and set its dimensions to the dimensions of your poster.
Open the actions panel.

3. Paste the following code. It’s the same as the previous tutorial except for the highlighted lines. The difference here is the way we position the elements so that the tween scale animation on each piece will start from the center of the piece.
Also we add a MOUSE_OVER event listener to each piece of the poster.

import com.greensock.*;

const COLUMNS:uint=10;
const ROWS:uint=10;

var imagesGrid : Array = new Array();    

var imageLoader:Loader = new Loader();
imageLoader.load(new URLRequest("theamerican.jpg"));
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
 
function onImageLoaded(e:Event):void {
 
	var originalBitmapData:BitmapData = e.target.content.bitmapData;
 
	var imageWidth : Number  = originalBitmapData.width / COLUMNS;
    var imageHeight : Number = originalBitmapData.height / ROWS;
	
	for (var i = 0; i < COLUMNS; i++) {
 
		for (var j = 0; j < ROWS; j++) {
 
			var imageHolder:MovieClip = new MovieClip();
 			
			var image:Bitmap = new Bitmap();
			image.bitmapData=new BitmapData(imageWidth,imageHeight);
 			image.bitmapData.copyPixels(
								originalBitmapData,
			  					new Rectangle(i * imageWidth, j * imageHeight,imageWidth, imageHeight),
			  					new Point(0,0));
 
			imageHolder.addChild(image);
 			
			image.x = -imageWidth / 2;
			image.y = -imageHeight / 2;
 
			imageHolder.x= i*imageWidth + imageWidth/2;
			imageHolder.y= j*imageHeight + imageHeight/2;
			
			imageHolder.alpha=0;
			imageHolder.addEventListener(MouseEvent.MOUSE_OVER, overHandler);
 
  			imagesGrid.push(imageHolder);
			addChild(imageHolder);
		}
	}	
}

4. The overHandler() function gets the piece of the poster which has triggered the event and uses the Tweenlite engine to animate its alpha and scales properties. Then we prevent the piece that has been revealed from receiving mouse messages and remove its event listener.

function overHandler(e:MouseEvent):void {
	var imageGrid = e.target as MovieClip;
 	imageGrid.scaleX = imageGrid.scaleY = 0;
 	TweenLite.to(imageGrid, .5, {alpha: 1,scaleX:1,scaleY:1});
 	imageGrid.mouseEnabled = false;
 	imageGrid.removeEventListener(MouseEvent.MOUSE_OVER, overHandler);
}

5. Here’s the final code. Test your movie to see it in action.

import com.greensock.*;

const COLUMNS:uint=10;
const ROWS:uint=10;

var imagesGrid : Array = new Array();    

var imageLoader:Loader = new Loader();
imageLoader.load(new URLRequest("theamerican.jpg"));
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
 
function onImageLoaded(e:Event):void {
 
	var originalBitmapData:BitmapData = e.target.content.bitmapData;
 
	var imageWidth : Number  = originalBitmapData.width / COLUMNS;
    var imageHeight : Number = originalBitmapData.height / ROWS;
	
	for (var i = 0; i < COLUMNS; i++) {
 
		for (var j = 0; j < ROWS; j++) {
 
			var imageHolder:MovieClip = new MovieClip();
 			
			var image:Bitmap = new Bitmap();
			image.bitmapData=new BitmapData(imageWidth,imageHeight);
 			image.bitmapData.copyPixels(
								originalBitmapData,
			  					new Rectangle(i * imageWidth, j * imageHeight,imageWidth, imageHeight),
			  					new Point(0,0));
 
			imageHolder.addChild(image);
 			
			image.x = -imageWidth / 2;
			image.y = -imageHeight / 2;
 
			imageHolder.x= i*imageWidth + imageWidth/2;
			imageHolder.y= j*imageHeight + imageHeight/2;
			
			imageHolder.alpha=0;
			imageHolder.addEventListener(MouseEvent.MOUSE_OVER, overHandler);
 
  			imagesGrid.push(imageHolder);
			addChild(imageHolder);
		}
	}	
}

function overHandler(e:MouseEvent):void {
	var imageGrid = e.target as MovieClip;
 	imageGrid.scaleX = imageGrid.scaleY = 0;
 	TweenLite.to(imageGrid, .5, {alpha: 1,scaleX:1,scaleY:1});
 	imageGrid.mouseEnabled = false;
 	imageGrid.removeEventListener(MouseEvent.MOUSE_OVER, overHandler);
}

18 thoughts on “Move the Mouse to Reveal the Poster

  1. Samed

    Very nice. Can this be edited to randomly appear, instead of in order or by mouse hover? That could really make it useful for me!

  2. Coluche

    Great tutorial, I was looking for something like this but with more pictures.
    When the first picture is revealed, I want another one to appear after the first… What can I do to make it right?

    Thanks a lot

  3. Saurabh Datta

    I am trying to use a variation of this technique combining two effects. Effect 1 comes first and then effect 2. But somehow its not happening. And I can’t find any other tutorial that does that. Can I share the code with you somehow…maybe you could look into it??

    Thanks for the awesome tutorial!!!

  4. Pingback: 通过一个网格来显示图片的动画效果 - ActionScript - Learning-AS

  5. shaun

    Is it possible to have an image at the start rather than plain white background and when you mouse over, the other image is revealed.

    Cheers

    p.s brilliant tutorial

  6. Don

    Again, a nice tutorial! I’ve found, however, that it depends on the size of image you use. Try to resize your image so that it’s easily, nicely divisible by 2. An image with a height, say, 849 pixels will leave you with a few horizontal lines, obviously, there’s something fishy going on when it’s divided up into rectangles. I took an image and resized it so it was 600 x 840 and no problems at all. I wonder if there’s a game in this! Like Minesweeper or something!

  7. Muli

    1172: Definition com.greensock could not be found.
    1172: Definition com.greensock could not be found.
    1120: Access of undefined property TweenLite.

    🙁 what do i do?

  8. cris

    It didnt work i followed instructions and downloaded greensock tweenlite still does not work help

Leave a Reply

Your email address will not be published. Required fields are marked *