| Home | Tutorials | Forums | Articles | Blogs | Movies | Library | Employment | Press | Buy templates |
|
|
#1 |
|
Registered User
Join Date: May 2006
Posts: 18
|
Hi:
I'm trying to: 1) draw a gradient with code 2) use it as a mask on a bitmap the masked bitmap is embedded and instantiated: new EmbeddedClass() the mask is created via a new Shape() that Shape uses the graphics object methods to draw it: beginGradientFill, draw Rect, etc. the gradient has an alphas array like this: [0, 255], so it goes from visible to invisible (I've checked this) the actual masking: maskedBitmap.mask=maskShape; the result is a solid mask, not a gradient mask...how can I apply a dynamic gradient mask? the code is not pretty but here it goes: Code:
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.BitmapDataChannel;
import flash.display.DisplayObject;
import flash.display.InterpolationMethod;
import flash.display.Shape;
import flash.display.SpreadMethod;
import flash.display.Sprite;
import flash.events.Event;
import flash.filters.DisplacementMapFilter;
import flash.geom.Matrix;
import flash.geom.Point;
public class TestIt extends Sprite
{
[Embed(source="images/desert.jpg")] internal var Desert:Class;
internal var picture:Bitmap;
internal var rippleMap:BitmapData
internal var rippleFilter:DisplacementMapFilter;
internal var offset:Point;
internal var bandMask:Shape;
internal var bgPicture:Bitmap;
public function TestIt()
{
//put embedded picture in a bitmap
picture= new Desert();
//clone a background for when the foreground picture is partly masked
bgPicture=new Bitmap(picture.bitmapData.clone());
addChild(bgPicture);
addChild(picture);
//create a displacement map based on blue, with a lot of noise along y
rippleMap=new BitmapData(picture.width, picture.height/2);
rippleFilter=new DisplacementMapFilter(rippleMap, new Point(0,50), 100, BitmapDataChannel.BLUE, 0, 55, "clamp", 0, 0);
//listen to enterframe and animate the filter by continously offsetting the filter's displacement map's perlin noise
addEventListener(Event.ENTER_FRAME, makeRipple)
offset=new Point(0,0);
//create a gradient to use as a mask
bandMask=new Shape();
var colors:Array=[0xffffff, 0x0000FF];
var alphas:Array=[100,0];
var ratios:Array=[0, 255];
var spread:String=SpreadMethod.PAD;
var inter:String=InterpolationMethod.LINEAR_RGB;
var focal:int=0
var mat:Matrix=new Matrix();
mat.createGradientBox(640, 200, -Math.PI/2);
bandMask.graphics.beginGradientFill("linear",colors,alphas,ratios,mat, spread, inter, focal);
bandMask.graphics.drawRect(0, 50, 640,200)
bandMask.graphics.endFill();
bandMask.blendMode="layer"
//put the mask on the display list and mask the picture
addChild (bandMask);
picture.mask=bandMask;
}
internal function makeRipple(evt:Event):void {
//move the first octave of the perlin noise
offset.x+=.2;
offset.y+=.6;
rippleMap.perlinNoise(150,20, 5, 89, true, true, BitmapDataChannel.BLUE, false, [offset]);
picture.filters=[rippleFilter];
}
}
}
I'm considering alternatives, like merging the gradient right onto the displacement map (to fade the perlin noise gradually wherever I want) that would probably be easier on the processer, less alphaing, but still, I'd like to know how to get a dynamic gradient mask working |
|
|
|
|
|
#2 |
|
Registered User
Join Date: Jun 2005
Posts: 145
|
Unless they've changed the mask behavior, masks have always been just on or off. Nothing in between, just based on the shape of the mask, not its color or alpha values.
|
|
|
|
|
|
|
|
|
#3 |
|
Registered User
Join Date: May 2006
Posts: 18
|
...so I doubt that AS3 will turn back the clock
but I never actually drew a gradient dynamically in Flash 8, so I don't know if that is possible in 8 or AS3 perhaps in both it is only possible with a gradient created in the IDE, so we have to wait for Blaze? I hope not. |
|
|
|
|
|
#4 | |
|
the ever-living
|
Quote:
http://www.gotoandlearn.com/ (click in Creating alpha masks) |
|
|
|
|
|
|
#5 |
|
Addict
Join Date: Nov 2001
Location: London
Posts: 2,128
|
you can create gradient masks by using a BitmapData as the mask (might be Bitmap in AS 3.0).
|
|
|
|
|
|
#7 |
|
Registered User
Join Date: May 2006
Posts: 18
|
I turned the shape mask into a bitmap, as per Tink, but it still does not work.
Then after a little experimentation, I found that even a normal fill with alpha set to zero acts as a mask. Hm. So maybe this has nothing to do with gradients: Code:
shapeMask=new Shape(); shapeMask.graphics.beginFill(0xFFFFFF, 0); shapeMask.graphics.drawRect(0, 0, 640,200) shapeMask.graphics.endFill(); bitmapDataMask=new BitmapData(640, 200,true, 0xFFFFFFF) bitmapDataMask.draw(shapeMask); bitmapMask=new Bitmap(bitmapDataMask); //put the mask on the display list and mask the picture addChild (bitmapMask); picture.mask=bitmapMask; Last edited by pet-theory; 08-07-2006 at 07:28 PM.. |
|
|
|
|
|
#8 | |
|
Registered User
Join Date: Jun 2005
Posts: 145
|
Quote:
|
|
|
|
|
|
|
#9 |
|
Registered User
Join Date: Aug 2006
Posts: 11
|
This was the tutorial that taught me how to do it (its been available since Flash 8's cacheAsBitmap):
http://www.devx.com/webdev/Article/29296/1954?pf=true Basically, it says that you need to use ActionScript rather than the designer interface to tell your gradient that it is the mask. Instead of right clicking your mask layer and making it a mask there, use code like this: Code:
maskee.cacheAsBitmap = true;
maskMC.cacheAsBitmap = true;
maskee.setMask("maskMC");
|
|
|
|
|
|
#10 | ||
|
the ever-living
|
Quote:
Quote:
Pet theory was asking how to do in AS3 some old f8 stuff Last edited by Incrue; 08-08-2006 at 05:47 PM.. |
||
|
|
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Is mask work for dynamic text field ? | tiru | ActionScript 1.0 (and below) | 16 | 02-05-2007 04:08 PM |
| dynamic changes in the mask (movie clip does not duplicate itself in the mask layer) | enfant2soleil | ActionScript 2.0 | 5 | 08-12-2005 05:50 PM |
| Dynamic text under dynamic mask w/ MS+ scroller....... | G-Rard B | Other Flash General Questions | 0 | 12-23-2004 12:13 AM |
| Gradient Mask Away Dynamic Text with Scroll | c2kman | ActionScript 1.0 (and below) | 4 | 03-29-2004 09:39 AM |
| gradient mask?? | Trinidad | Animation and Effects | 1 | 07-28-2002 11:46 PM |