Tutorials
FILE CATEGORIES
Tutorials >> Magnifying Glass
DOWNLOAD SOURCE FILE FOR THIS TUTORIAL
Posted by webzo
Magnifying glass is not just in the real world. In Flash, we can also create a magnifying glass. You can zoom a picture or even text. Maybe you can even use this for disability people that are using your computer.

Create a new Movie Clip and draw your magnifying glass; fill the glass part with any color you want.

Select the red and press F8, this will create a new Movie Clip that we will use as a mask.
After converting, delete it from the magnifying glass Movie Clip.
Drag both Movie Clip to the stage, one with the instance "imgMask" and the other "magGlass".
Now create a new Movie Clip just to hold our image, drag it to the stage and give the instance name "imgHolder".
Paste this on the first frame:
[st]Code Explanation[/st]
The parameters for the function used are:
Posted by webzo
Magnifying glass is not just in the real world. In Flash, we can also create a magnifying glass. You can zoom a picture or even text. Maybe you can even use this for disability people that are using your computer.

Create a new Movie Clip and draw your magnifying glass; fill the glass part with any color you want.

Select the red and press F8, this will create a new Movie Clip that we will use as a mask.
After converting, delete it from the magnifying glass Movie Clip.
Drag both Movie Clip to the stage, one with the instance "imgMask" and the other "magGlass".
Now create a new Movie Clip just to hold our image, drag it to the stage and give the instance name "imgHolder".
Paste this on the first frame:
- Code: Select all
//load an image named "image.jpg" to the imgHolder Movie Clip
loadMovie('image.jpg', imgHolder);
//new Movie Clip just to hold the zoomed image
this.createEmptyMovieClip("zoomImg", 33);
//Set a mask inside the Movie Clip so the zoomed image is rounded
zoomImg.setMask(imgMask);
// bitmap object to zoom the image
var zoomBitmap = new flash.display.BitmapData(155, 155, false, 0xEAEAEA);
//attach the bitmap to the" zoomImg" Movie Clip
zoomImg.attachBitmap(zoomBitmap, 5);
//zoom power, increase this to zoom more
var magPower = 2;
//matrix object to position the zoomed bitmap inside the magnifier
var zoomMtx = new flash.geom.Matrix();
//scale the image using the "magPower"
zoomMtx.scale(magPower, magPower);
//swap depths for the magnifier Movie Clip so it is on top
magGlass.swapDepths(_root.getNextHighestDepth());
_root.onEnterFrame = function()
{
//hide the mouse
Mouse.hide();
//set the magnifier Movie Clip to the position of the mouse
magGlass._x = _xmouse - 70;
//so the mouse is in the middle of the lens
magGlass._y = _ymouse - 70;
//zoomed image Movie Clip
zoomImg._x = magGlass._x;
zoomImg._y = magGlass._y;
//Mask Movie Clip
imgMask._x = magGlass._x;
imgMask._y = magGlass._y;
//where to draw the zoomed image
zoomMtx.tx = -_xmouse * magPower;
zoomMtx.ty = -_ymouse * magPower;
//draw zoomed image using the image in the
//holder and the position of the matrix.
zoomBitmap.draw(imgHolder, zoomMtx);
}
[st]Code Explanation[/st]
- Code: Select all
loadMovie('image.jpg', imgHolder);
- Code: Select all
this.createEmptyMovieClip("zoomImg", 33);
- Code: Select all
//Set a mask inside the Movie Clip so the zoomed image is rounded
zoomImg.setMask(imgMask);
The parameters for the function used are:
- Code: Select all
setMask(Mask_MC);
- Code: Select all
var zoomBitmap = new flash.display.BitmapData(155, 155, false, 0xEAEAEA);
- Code: Select all
new BitmapData(width, height, transparency, color);
- Code: Select all
zoomImg.attachBitmap(zoomBitmap, 5);
- Code: Select all
attachBitmap(bitmapData, depth);
var magPower = 2;
- Code: Select all
var zoomMtx = new flash.geom.Matrix();
- Code: Select all
zoomMtx.scale(magPower, magPower);
- Code: Select all
scale(_x scale, _y scale);
magGlass.swapDepths(_root.getNextHighestDepth());
- Code: Select all
_root.onEnterFrame = function()
{
Mouse.hide();
magGlass._x = _xmouse - 70;
magGlass._y = _ymouse - 70;
- Code: Select all
zoomImg._x = magGlass._x;
zoomImg._y = magGlass._y;
imgMask._x = magGlass._x;
imgMask._y = magGlass._y;
- Code: Select all
zoomMtx.tx = -_xmouse * magPower;
zoomMtx.ty = -_ymouse * magPower;
- Code: Select all
zoomBitmap.draw(imgHolder, zoomMtx);
}
- Code: Select all
draw(source, matrix)

Twitter