Customizing Senocular's Motion Blur for Actionscript 2.0

  • pajhonka
  • Born
  • Born
  • pajhonka
  • Posts: 1

Post 3+ Months Ago

I'm building banners in AS2, Flash CS4, and I've tried to customize Senocular's very handy directional motion blur. The problem I'm having is I can't make this code reusable- I want to use it in a more linear fashion for banners rather then dynamically with mouse clicks. Has anyone had any luck using this in a timeline? Thanks!

This is my attempt to customize at the moment (old_loc is coming up as undefined, which in turn makes new_loc NaN):
Code: [ Select ]
import flash.geom.*;
import flash.display.BitmapData;
import flash.filters.BlurFilter;

Stage.scaleMode = 'noScale';

var maxBlur:Number = 40;// maximum blur amount for motion blurring
var easeAmount:Number = .75;// amount of easing to be used for movement (0-1)
var maxSize:Number;
var offset:Number;
var blur_bmp:BitmapData;
var rotate_matrix:Matrix = new Matrix();
var blur_rect:Rectangle = new Rectangle();
var blur_point:Point;
var blur_filter:BlurFilter;
var target_loc:Point;
var blurred_mc:MovieClip;

function motionBlur(mc:MovieClip, xPos, yPos):Void
{
    //mc._visible = false;
    maxSize = maxBlur + Math.sqrt(mc._width * mc._width + mc._height * mc._height);
    trace("maxSize = " + maxSize);
    offset = maxSize / 2;
    trace("offset = " + offset);
    blur_bmp = new BitmapData(maxSize, maxSize, true, 0);
    this.createEmptyMovieClip("blurred_mc",1);
    blurred_mc.createEmptyMovieClip("image",1);
    blurred_mc.image.attachBitmap(blur_bmp,1,false,true);
    blurred_mc.image._x = -offset;
    blurred_mc.image._y = -offset;
    blur_rect = blur_bmp.rectangle;// rectangle area filter affects
    blur_point = new Point(0, 0);// offset point for filter
    blur_filter = new BlurFilter(0, 0);// filter (blur, starting with no power)
    this.onEnterFrame = moveIt(mc);
    target_loc = new Point(xPos, yPos);
}

function moveIt(mc:MovieClip) {
    // get old (current) location of blurred_mc
    var old_loc = new Point(blurred_mc._x, blurred_mc._y);
    trace("old_loc : " + old_loc);
    // develop a new location for blurred_mc with interpolate
    var new_loc = Point.interpolate(old_loc, target_loc, easeAmount);
    trace("new_loc : " + new_loc);
    // assign the new_loc to the position of blurred_mc
    blurred_mc._x = new_loc.x;
    blurred_mc._y = new_loc.y;
    // get the distance from the old loc to the new
    // this will be used to determine how much blur to apply
    var distance = Point.distance(old_loc, new_loc);
    // get the angle from the old loc to the new
    // this will be used to determine the angle of the blur
    var angle = PointAngle(old_loc, new_loc);
    // reset the rotate_matrix to remove any transformations
    // that were applied last frame
    rotate_matrix.identity();
    // rotate the matrix opposite of the angle found between
    // the new and old locations of blurred_mc
    rotate_matrix.rotate(-angle);
    // move the matrix by the offset to account for the
    // centered position of the original click_mc
    rotate_matrix.translate(offset,offset);
    // clear blur_bmp by filling it with empty pixels
    blur_bmp.fillRect(blur_bmp.rectangle,0);
    // draw the rotated, translated click_mc into blur_bmp
    blur_bmp.draw(mc,rotate_matrix);
    // apply the blur power to the blur filter
    blur_filter.blurX = Math.min(maxBlur, distance * 1.5);
    // apply the blur filter to the blur_bmp
    blur_bmp.applyFilter(blur_bmp,blur_rect,blur_point,blur_filter);
    // rotate blurred_mc to counteract the rotation of
    // rotate_matrix used in draw
    blurred_mc._rotation = angle * 180 / Math.PI;
}

button_mc.onMouseDown = function():Void
{
    motionBlur(click_mc, click_mc._x+=20, click_mc._y+=20);
    trace("CLICK MC X: " + click_mc._x);    
    trace("CLICK MC Y: " + click_mc._y);    
}
// PointAngle: returns the angle between two points
function PointAngle(pt1:Point, pt2:Point):Number
{
    var dx = pt2.x - pt1.x;
    var dy = pt2.y - pt1.y;
    return Math.atan2(dy, dx);
}
  1. import flash.geom.*;
  2. import flash.display.BitmapData;
  3. import flash.filters.BlurFilter;
  4. Stage.scaleMode = 'noScale';
  5. var maxBlur:Number = 40;// maximum blur amount for motion blurring
  6. var easeAmount:Number = .75;// amount of easing to be used for movement (0-1)
  7. var maxSize:Number;
  8. var offset:Number;
  9. var blur_bmp:BitmapData;
  10. var rotate_matrix:Matrix = new Matrix();
  11. var blur_rect:Rectangle = new Rectangle();
  12. var blur_point:Point;
  13. var blur_filter:BlurFilter;
  14. var target_loc:Point;
  15. var blurred_mc:MovieClip;
  16. function motionBlur(mc:MovieClip, xPos, yPos):Void
  17. {
  18.     //mc._visible = false;
  19.     maxSize = maxBlur + Math.sqrt(mc._width * mc._width + mc._height * mc._height);
  20.     trace("maxSize = " + maxSize);
  21.     offset = maxSize / 2;
  22.     trace("offset = " + offset);
  23.     blur_bmp = new BitmapData(maxSize, maxSize, true, 0);
  24.     this.createEmptyMovieClip("blurred_mc",1);
  25.     blurred_mc.createEmptyMovieClip("image",1);
  26.     blurred_mc.image.attachBitmap(blur_bmp,1,false,true);
  27.     blurred_mc.image._x = -offset;
  28.     blurred_mc.image._y = -offset;
  29.     blur_rect = blur_bmp.rectangle;// rectangle area filter affects
  30.     blur_point = new Point(0, 0);// offset point for filter
  31.     blur_filter = new BlurFilter(0, 0);// filter (blur, starting with no power)
  32.     this.onEnterFrame = moveIt(mc);
  33.     target_loc = new Point(xPos, yPos);
  34. }
  35. function moveIt(mc:MovieClip) {
  36.     // get old (current) location of blurred_mc
  37.     var old_loc = new Point(blurred_mc._x, blurred_mc._y);
  38.     trace("old_loc : " + old_loc);
  39.     // develop a new location for blurred_mc with interpolate
  40.     var new_loc = Point.interpolate(old_loc, target_loc, easeAmount);
  41.     trace("new_loc : " + new_loc);
  42.     // assign the new_loc to the position of blurred_mc
  43.     blurred_mc._x = new_loc.x;
  44.     blurred_mc._y = new_loc.y;
  45.     // get the distance from the old loc to the new
  46.     // this will be used to determine how much blur to apply
  47.     var distance = Point.distance(old_loc, new_loc);
  48.     // get the angle from the old loc to the new
  49.     // this will be used to determine the angle of the blur
  50.     var angle = PointAngle(old_loc, new_loc);
  51.     // reset the rotate_matrix to remove any transformations
  52.     // that were applied last frame
  53.     rotate_matrix.identity();
  54.     // rotate the matrix opposite of the angle found between
  55.     // the new and old locations of blurred_mc
  56.     rotate_matrix.rotate(-angle);
  57.     // move the matrix by the offset to account for the
  58.     // centered position of the original click_mc
  59.     rotate_matrix.translate(offset,offset);
  60.     // clear blur_bmp by filling it with empty pixels
  61.     blur_bmp.fillRect(blur_bmp.rectangle,0);
  62.     // draw the rotated, translated click_mc into blur_bmp
  63.     blur_bmp.draw(mc,rotate_matrix);
  64.     // apply the blur power to the blur filter
  65.     blur_filter.blurX = Math.min(maxBlur, distance * 1.5);
  66.     // apply the blur filter to the blur_bmp
  67.     blur_bmp.applyFilter(blur_bmp,blur_rect,blur_point,blur_filter);
  68.     // rotate blurred_mc to counteract the rotation of
  69.     // rotate_matrix used in draw
  70.     blurred_mc._rotation = angle * 180 / Math.PI;
  71. }
  72. button_mc.onMouseDown = function():Void
  73. {
  74.     motionBlur(click_mc, click_mc._x+=20, click_mc._y+=20);
  75.     trace("CLICK MC X: " + click_mc._x);    
  76.     trace("CLICK MC Y: " + click_mc._y);    
  77. }
  78. // PointAngle: returns the angle between two points
  79. function PointAngle(pt1:Point, pt2:Point):Number
  80. {
  81.     var dx = pt2.x - pt1.x;
  82.     var dy = pt2.y - pt1.y;
  83.     return Math.atan2(dy, dx);
  84. }
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

Post Information

  • Total Posts in this topic: 1 post
  • Users browsing this forum: No registered users and 19 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.