使用ActionScript实现滤镜效果

使用ActionScript实现滤镜效果
2009年02月24日 11:17

用as做滤镜效果,每种滤镜效果对应一个类别,这些类别将来会在Macromedia\Flash 8\语系\First Run\Classes\FP8\flash\filters 这个资料夹中.

一、渐变斜角——GradientBevelFilter类别

二、投影——DropShadowFilter类别

三、模糊——BlurFilter类别(这个最容易了,参数少)

四、光晕——GlowFilter类别

五、渐层光晕——GradientGlowFilter类别

六、斜角——BevelFilter类别

做法是用new关键字新建一个相应类的对象,暂时就叫myfilters吧,然後把这个对象增加进一个数组filters_array,最後把这个数组赋值给mc的fliters属性,大功告成。

我想其中关键应该就是建立对像时如何设定里面的参数吧,我想滤镜的面板大家都很熟悉的了,那麽,我就把as增加滤镜的一些参数和它们的面板相对照,这样可能更容易理解。

下面逐个举例(都是辅助里的范例啦,里面的参数名字都是可以改的,不过我觉得就用那些名字就蛮好),范例里都有函数setUpFlatRectangle(也有叫createRectangle的),是用来做一个方形mc的。

function setUpFlatRectangle(w:Number, h:Number, bgColor:Number, name:String):MovieClip {
var mc:MovieClip = this.createEmptyMovieClip(name, this.getNextHighestDepth());
mc.beginFill(bgColor);
mc.lineTo(w, 0);
mc.lineTo(w, h);
mc.lineTo(0, h);
mc.lineTo(0, 0);
return mc;
}

直接把Script复制到主场景的第一影格就ok了,不要忘记上面的函数哦,要注意改函数名的

一、渐变斜角——GradientBevelFilter类别

Script:

import flash.filters.GradientBevelFilter;
import flash.filters.BitmapFilter;
var art:MovieClip = setUpFlatRectangle(150, 150, 0xCCCCCC, "gradientBevelFilterExample");
//建立新的mc,实例名:art
var distance:Number = 5;
var angleInDegrees:Number = 225; // opposite 45 degrees
var colors:Array = [0xFFFFFF, 0xCCCCCC, 0x000000];
var alphas:Array = [1, 0, 1];
var ratios:Array = [0, 128, 255];
var blurX:Number = 8;
var blurY:Number = 8;
var strength:Number = 2;
var quality:Number = 3;
var type:String = "inner";
var knockout:Boolean = true;//设定各个参数
var filter:GradientBevelFilter = new GradientBevelFilter(distance, angleInDegrees, colors, alphas, ratios, blurX, blurY, strength, quality, type, knockout);//建立斜角滤镜对像filter
var filterArray:Array = new Array();
filterArray.push(filter); //把filter增加进数组filterArray,如果art上有多个滤镜效果,就直接增加进这个数组好了
art.filters = filterArray;//将art的filters属性设定为filterArray

下面是Script中的参数和面板中的参数对比

colors、alphas、ratios用来控制渐变颜色的,colors是色彩,alphas是百分比(这里面是0~1的小数),ratios是偏移量0~255(可以参考Color类)blurX、blurY是就对应面板里的模糊x、模糊y。strength是面板里的强度,是0~255的数;quality是品质,只有1、2、3分别对应低、中、高distance是距离,预设4.0;type是类型,用字串表示,范例中的」inner」是内侧,还有outer、fuller;knockout是个布尔值,表示是否挖空

二、投影——DropShadowFilter类别

import flash.filters.DropShadowFilter;
var art:MovieClip = createRectangle(100, 100, 0x003366, "gradientGlowFilterExample");
var distance:Number = 20;
var angleInDegrees:Number = 45;
var color:Number = 0x000000;
var alpha:Number = 0.8;
var blurX:Number = 16;
var blurY:Number = 16;
var strength:Number = 1;
var quality:Number = 3;
var inner:Boolean = false;
var knockout:Boolean = false;
var hideObject:Boolean = false;
var filter:DropShadowFilter = new DropShadowFilter(distance, angleInDegrees, color, alpha, blurX, blurY, strength, quality, inner, knockout, hideObject);
var filterArray:Array = new Array();
filterArray.push(filter);
art.filters = filterArray;

下面是Script中的参数和面板中的参数对比

Distance是距离;
angleInDegrees是角度0~360
blurX、blurY是模糊x、模糊y
strength是强度,Script里是0~5的数
quality是品质,只能是1、2、3,分别对应低、中、高
inner是内侧阴影
knockout是挖空
hideObject是隐藏对像

三、模糊——BlurFilter类别(这个最容易了,参数少嘛)

import flash.filters.BlurFilter;
var rect:MovieClip = createRectangle(100, 100, 0x003366, "BlurFilterExample");
var blurX:Number = 30;
var blurY:Number = 30;
var quality:Number = 3;
var filter:BlurFilter = new BlurFilter(blurX, blurY, quality);
var filterArray:Array = new Array();
filterArray.push(filter);
rect.filters = filterArray;

四、发光——GlowFilter类别

import flash.filters.GlowFilter;
var rect:MovieClip = createRectangle(100, 100, 0x003366, "gradientGlowFilterExample");
var color:Number = 0x33CCFF;
var alpha:Number = .8;
var blurX:Number = 35;
var blurY:Number = 35;
var strength:Number = 2;
var quality:Number = 3;
var inner:Boolean = false;
var knockout:Boolean = false;
var filter:GlowFilter = new GlowFilter(color, alpha, blurX, blurY, strength, quality, inner, knockout);
var filterArray:Array = new Array();
filterArray.push(filter);
rect.filters = filterArray;

五、渐变发光——GradientGlowFilter类别

import flash.filters.GradientGlowFilter;
var art:MovieClip = createRectangle(100, 100, 0x003366, "gradientGlowFilterExample");
var distance:Number = 0;
var angleInDegrees:Number = 45;
var colors:Array = [0xFFFFFF, 0xFF0000, 0xFFFF00, 0x00CCFF];
var alphas:Array = [0, 1, 1, 1, 1];
var ratios:Array = [0, 63, 126, 255];
var blurX:Number = 50;
var blurY:Number = 50;
var strength:Number = 2.5;
var quality:Number = 3;
var type:String = "outer";
var knockout:Boolean = false;
var filter:GradientGlowFilter = new GradientGlowFilter(distance, angleInDegrees, colors, alphas, ratios, blurX, blurY, strength, quality, type, knockout);
var filterArray:Array = new Array();
filterArray.push(filter);
art.filters = filterArray;

这里的参数跟光晕的类别很像,需要说明的就是用来设定颜色的那几个数组里的4项依次是表示从外到内的颜色的

六、斜角——BevelFilter类别

import flash.filters.BevelFilter;
var distance:Number = 5;
var angleInDegrees:Number = 45;
var highlightColor:Number = 0xFFFF00;
var highlightAlpha:Number = 1;
var shadowColor:Number = 0x0000FF;
var shadowAlpha:Number = 1;
var blurX:Number = 10;
var blurY:Number = 10;
var strength:Number = 2;
var quality:Number = 3;
var type:String = "inner";
var knockout:Boolean = false;
var filter:BevelFilter = new BevelFilter(distance, angleInDegrees, highlightColor, highlightAlpha, shadowColor, shadowAlpha, blurX, blurY, strength, quality, type, knockout);
var rect:MovieClip = createRectangle(100, 100, 0x00CC00, "bevelFilterExample");
rect.filters = new Array(filter);

参数:

highlightColor、highlightAlpha设定加亮颜色
shadowColor、shadowAlpha设定阴影颜色

posted @ 2012-08-18 22:57  Lunaa  阅读(129)  评论(0编辑  收藏  举报