13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Filtering display objects<br />

In ActionScript, you create the matrix as a combination of an Array instance containing the values and two properties<br />

specifying the number of rows and columns in the matrix. The following example loads an image and, wh<strong>en</strong> the image<br />

finishes loading, applies a convolution filter to the image using the matrix in the previous listing:<br />

// Load an image onto the Stage.<br />

var loader:Loader = new Loader();<br />

var url:URLRequest = new URLRequest("http://www.helpexamples.com/flash/images/image1.jpg");<br />

loader.load(url);<br />

this.addChild(loader);<br />

function applyFilter(ev<strong>en</strong>t:MouseEv<strong>en</strong>t):void<br />

{<br />

// Create the convolution matrix.<br />

var matrix:Array = [0, 0, 0,<br />

0, 0, 1,<br />

0, 0, 0];<br />

}<br />

var convolution:ConvolutionFilter = new ConvolutionFilter();<br />

convolution.matrixX = 3;<br />

convolution.matrixY = 3;<br />

convolution.matrix = matrix;<br />

convolution.divisor = 1;<br />

loader.filters = [convolution];<br />

loader.addEv<strong>en</strong>tList<strong>en</strong>er(MouseEv<strong>en</strong>t.CLICK, applyFilter);<br />

Something that isn’t obvious in this code is the effect of using values other than 1 or 0 in the matrix. For example, the<br />

same matrix, with the number 8 instead of 1 in the right-hand position, performs the same action (shifting the pixels<br />

to the left). In addition, it affects the colors of the image, making them 8 times brighter. This is because the final pixel<br />

color values are calculated by multiplying the matrix values by the original pixel colors, adding the values together, and<br />

dividing by the value of the filter’s divisor property. Notice that in the example code, the divisor property is set to<br />

1. As a g<strong>en</strong>eral rule, if you want the brightness of the colors to stay about the same as in the original image, you should<br />

make the divisor equal to the sum of the matrix values. So with a matrix where the values add up to 8, and a divisor of<br />

1, the resulting image is going to be roughly 8 times brighter than the original image.<br />

Although the effect of this matrix isn’t very noticeable, other matrix values can be used to create various effects. Here<br />

are several standard sets of matrix values for differ<strong>en</strong>t effects using a three by three matrix:<br />

Basic blur (divisor 5):<br />

0 1 0<br />

1 1 1<br />

0 1 0<br />

Sharp<strong>en</strong>ing (divisor 1):<br />

0, -1, 0<br />

-1, 5, -1<br />

0, -1, 0<br />

Edge detection (divisor 1):<br />

0, -1, 0<br />

-1, 4, -1<br />

0, -1, 0<br />

Embossing effect (divisor 1):<br />

Last updated 6/6/2012<br />

285

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!