Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Student and Teacher Editions
More products
Solutions
Creative tools for business
Digital marketing
Digital media
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Buy
Home use for personal and home office
Education for students, educators, and staff
Business for small and medium businesses
Licensing programs for businesses, schools, and government
Special offers
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
My products and services
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out My orders My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
Qty:
Purchase requires verification of academic eligibility
Subtotal
Review and Checkout
Adobe Developer Connection / Pixel Bender Technology Center /

Effects with the Pixel Bender Toolkit – Part 8: Controlling the displacement filter with mouse positioning

by Kevin Goldsmith

Kevin Goldsmith

Content

  • Setting up the files
  • Deleting the Slider component
  • Controlling the filter effect with mouse interactions
  • Incorporating the minimum and maximum metadata
  • Where to go from here

Created

11 January 2010

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
Flash Professional graphic effects Pixel Bender

Requirements

Prerequisite knowledge

Some familiarity with ActionScript 3.

User level

Beginning

Required products

  • Flash Player 10 and later
  • Flash Professional (Download trial)
  • Pixel Bender Toolkit (Macintosh)
  • Pixel Bender Toolkit (Windows)

Sample files

  • pixel_bender_08.zip (8535 KB)

In this article, you'll add a type of interactivity that animates a Pixel Bender filter to correspond to changing mouse positions. This is the eighth installment in this series of articles about using the Pixel Bender Toolkit to create visual effects with bitmap images.

In the previous section, you learned how to update the displacement filter to enable it to move in all directions. You also updated the parameters to make the displacement filter effect more dramatic when you moved the slider controls.

In this section, you'll update the ActionScript code in the Flash file to control the displacement filter parameter with changing positions of the mouse.

Setting up the files

Before you begin, if you'd like to continue experimenting with the previous version of the project, you can add a second slider to control the float2 parameter that you just added to the Displacement filter. That process is fairly straightforward and it is always a good idea to practice when learning a new concept.

In this part of the series, we'll discuss adding another type of interactivity to control the filter effect; this time you'll track the placement of the cursor and use it to animate the displacement filter.

Before updating the ActionScript, set the file up in Flash in preparation for the code edits. Follow these steps:

  1. Launch Flash CS4 (or make it active if it is already open).
  2. Open the file named Exercise5.fla from the pixel_bender folder on your desktop.

    (If you are just beginning to follow along with this section and didn't complete the files in the previous parts of this series, download the sample files provided. Uncompress the ZIP file and save the contents into a folder named pixel_bender that you create on your desktop. Open the file named exercise8A.fla to begin following along at this point.)

  3. When you open the FLA file, you'll see a flower image and a slider control on the Stage.
  4. Choose Window > Actions to open the Actions panel (if it is not already open). Alternatively, expand the panel if it was open but collapsed.
  5. Select Frame 1 in the Timeline and review the code in the Script window. It should look similar to the ActionScript shown below:
import fl.events.SliderEvent; var urlRequest:URLRequest = new URLRequest("Exercise4Filter.pbj"); var urlLoader:URLLoader = new URLLoader(); urlLoader.dataFormat = URLLoaderDataFormat.BINARY; urlLoader.addEventListener( Event.COMPLETE, applyFilter ); urlLoader.load(urlRequest); var shader:Shader; var shaderFilter:ShaderFilter; function applyFilter( event:Event ):void { urlLoader.removeEventListener( Event.COMPLETE, applyFilter ); shader = new Shader( event.target.data ); shaderFilter = new ShaderFilter( shader ); // set up slider amount_slider.minimum = shader.data.amount.minValue; amount_slider.maximum = shader.data.amount.maxValue; amount_slider.value = shader.data.amount.defaultValue; amount_slider.snapInterval = Math.abs(amount_slider.minimum - amount_slider.maximum) / 100; amount_slider.liveDragging = true; amount_slider.addEventListener( SliderEvent.CHANGE, updateFilter ); flower.filters = [ shaderFilter ]; } function updateFilter( event:SliderEvent ):void { shader.data.amount.value = [amount_slider.value]; flower.filters = [ shaderFilter ]; }
  1. If desired, rearrange the panels in the Flash authoring environment so that you can see both the Stage and the Actions panel simultaneously (see Figure 1).
Panels arranged in the Flash workspace so that you can edit both the code and the elements on the Stage
Figure 1. Panels arranged in the Flash workspace so that you can edit both the code and the elements on the Stage

Deleting the Slider component

Follow these steps to remove the slider from the Flash file:

  1. Select the instance of the slider control on the Stage.
  2. Press the Delete key to delete it.
  3. Open the Actions panel and review the code in the Script window. Remove the line of code at the top that imports the SliderEvent:
import fl.events.SliderEvent;
  1. Remove the slider setup code located in the applyFilter function. After making these changes, the applyFilter function should look like this:
function applyFilter( event:Event ):void { urlLoader.removeEventListener( Event.COMPLETE, applyFilter ); shader = new Shader( event.target.data ); shaderFilter = new ShaderFilter( shader ); flower.filters = [ shaderFilter ]; }
  1. Locate the beginning of the updateFilter function:
function updateFilter( event:SliderEvent ):void
  1. Change the event type from a SliderEvent to the generic type Event:
function updateFilter( event:Event ):void
  1. Locate the line that sets the amount parameter in the Pixel Bender filter. You need to disable the code, but don't delete this line; you'll re-enable it later:
shader.data.amount.value = [amount_slider.value];

Instead, just type two forward slashes before the line to comment it out:

// shader.data.amount.value = [amount_slider.value];

After making these changes, your ActionScript code should look like this:

var urlRequest:URLRequest = new URLRequest("Exercise4Filter.pbj"); var urlLoader:URLLoader = new URLLoader(); urlLoader.dataFormat = URLLoaderDataFormat.BINARY; urlLoader.addEventListener( Event.COMPLETE, applyFilter ); urlLoader.load(urlRequest); var shader:Shader; var shaderFilter:ShaderFilter; function applyFilter( event:Event ):void { urlLoader.removeEventListener( Event.COMPLETE, applyFilter ); shader = new Shader( event.target.data ); shaderFilter = new ShaderFilter( shader ); flower.filters = [ shaderFilter ]; } function updateFilter( event:Event ):void { // shader.data.amount.value = [amount_slider.value]; flower.filters = [ shaderFilter ]; }
  1. Choose Debug > Debug Movie.
  2. If you do not encounter any syntax errors, Flash Player launches to display the yellow flowers image. This time the SWF does not show a slider. The image appears normally because the filter is not applied.
  3. Choose File > Save. Save the Flash project as Exercise8.fla in the pixel_bender folder on your desktop.

Controlling the filter effect with mouse interactions

Now that you've removed the slider control, the next task involves adding code to control the filter effects by tracking the cursor. This step allows the filter to be reapplied and creates a more interactive experience for the user:

  1. Choose Window > Actions to access the Actions panel.
  2. In the Script window, edit the linked PBJ file, changing the reference from Exercise4Filter.pbj to Exercise7Filter.pbj.
  3. Add the following line to the end of the applyFilter function, before the closing curly brace:
flower.addEventListener( Event.ENTER_FRAME, updateFilter );
  1. Locate this commented line of code in the body of the updateFilter function:
// shader.data.amount.value = [amount_slider.value]; flower.filters = [ shaderFilter ];
  1. Change the body of the updateFilter function to look like this:
var xOffset:Number = flower.width/2 - flower.mouseX; var yOffset:Number = flower.height/2 - flower.mouseY; shader.data.amount.value = [ xOffset, yOffset ]; flower.filters = [ shaderFilter ];

The code above retrieves the current mouse position, determines its offset (based on the center of the image), and uses the value to set the offset as the parameter value to the Pixel Bender filter.

  1. Choose Debug > Debug Movie. If you do not encounter any syntax errors, Flash Player launches to display the yellow flowers image.
  2. Interact with the SWF by moving your cursor over it. Although the filter is running, when you test the SWF you'll notice that the parameter values are ignoring the minimum and maximum values.

    Note: Because Flash player does not enforce the minValue or maxValues on the filter, it is a best practice to take these values into account in your ActionScript code.

  3. Choose File > Save. Name the Flash project as Exercise8.fla and save it in the folder called pixel_bender on your desktop.

Incorporating the minimum and maximum metadata

In this section, you'll update the code to enable the filter to be constrained by the minimum and maximum values that are set in the Pixel Bender metadata. Follow these steps:

  1. Change the code in the updateFilter function, to match the example below:
function updateFilter( event:Event ):void { var xOffset:Number = flower.width/2 - flower.mouseX; var yOffset:Number = flower.height/2 - flower.mouseY; xOffset = Math.min( xOffset, shader.data.amount.maxValue[0] ); xOffset = Math.max( xOffset, shader.data.amount.minValue[0] ); yOffset = Math.min( yOffset, shader.data.amount.maxValue[1] ); yOffset = Math.max( yOffset, shader.data.amount.minValue[1] ); shader.data.amount.value = [ xOffset, yOffset ]; flower.filters = [ shaderFilter ]; }

This code forces the parameter values set by the ActionScript code to stay within the ranges specified by the Pixel Bender code.

  1. Choose Debug > Debug Movie.
  2. If you do not encounter any syntax errors, Flash Player launches to display the yellow flower image.
  3. Interact with the SWF by moving your mouse to see the filter running. Notice how the effect is constrained to the minimum and maximum values set in the Pixel Bender metadata.
  4. Save the Flash project in the pixel_bender folder on your desktop.

Where to go from here

After testing and reviewing the code from the SWF, continue with Part 9 in this series, where you'll learn how to create a Pixel Bender kernel that integrates values from multiple image sources.

Check out the following resources to learn more about working with the Pixel Bender Toolkit:

  • Pixel Bender forum
  • Pixel Bender basics for Flash
  • Pixel Bender basics for Flex and AIR

Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License+Adobe Commercial Rights

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

More Like This

  • Effects with the Pixel Bender Toolkit – Part 2: Creating a vintage tone filter
  • Effects with the Pixel Bender Toolkit – Part 3: Adding parameters to filters
  • Effects with the Pixel Bender Toolkit – Part 1: Creating a new filter
  • Effects with the Pixel Bender Toolkit – Part 6: Modifying Pixel Bender parameters in Flash
  • Effects with the Pixel Bender Toolkit – Part 5: Applying a filter to an image in Flash
  • Effects with the Pixel Bender Toolkit – Part 4: Sampling multiple pixels
  • Pixel Bender release notes
  • Effects with the Pixel Bender Toolkit – Part 9: Integrating multiple image sources with a Pixel Bender kernel
  • Animating a particle system using Pixel Bender
  • Effects with the Pixel Bender Toolkit – Part 10: Using a multiple-input filter as a blend shader

Tutorials & Samples

Tutorials

  • Animating a particle system
  • Controlling the displacement filter in Pixel Bender with mouse positioning
  • Integrating multiple image sources with a Pixel Bender kernel

Samples

Pixel Bender Forum

More
04/10/2012 Fast Fourier Transform (FFT) in Pixel Bender?
04/19/2012 Shader works in toolkit, not in Flash
04/12/2012 Pixel Bender removed from AE CS6 !?
03/22/2012 Load an HDR image into Pixel Bender Toolkit

Pixel Bender Exchange

More
Escher's Droste Effect
4D Quaternion Julia-set Ray Tracer with Ambient Occlusion
Raytracer
Zoom Blur Focus

Products

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Mobile Apps
  • Photoshop
  • Touch Apps
  • Student and Teacher Editions

Solutions

  • Digital marketing
  • Digital media
  • Web Experience Management

Industries

  • Education
  • Financial services
  • Government

Help

  • Product help centers
  • Orders and returns
  • Downloading and installing
  • My Adobe

Learning

  • Adobe Developer Connection
  • Adobe TV
  • Training and certification
  • Forums
  • Design Center

Ways to buy

  • For personal and home office
  • For students, educators, and staff
  • For small and medium businesses
  • For businesses, schools, and government
  • Special offers

Downloads

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Security
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy Policy and Cookies (Updated)

Ad Choices

Reviewed by TRUSTe: site privacy statement