1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
/**
* Neave Webcam // Squeeze Effect
*
* Copyright (C) 2008 Paul Neave
* http://www.neave.com/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation at http://www.gnu.org/licenses/gpl.html
*/
package com.neave.webcam.effects.displace
{
import flash.display.*;
import flash.filters.*;
import flash.geom.*;
public class SqueezeEffect extends AbstractDisplaceEffect
{
/**
* Creates a squeezing distortion effect where the middle is squeezed and the edges are stretched
*
* @param source The source object to use for the effect
* @param targetBitmap The target bitmap data to draw the resulting effect into
*/
public function SqueezeEffect(source:IBitmapDrawable, targetBitmap:BitmapData)
{
super(source, targetBitmap, "Squeeze");
createSqueeze();
}
/**
* Sets up the squeeze effect
*/
private function createSqueeze():void
{
// The size of the squeeze effect
var w:int = rect.width;
var h:int = rect.height;
// The displacement gradient matrix
var m:Matrix = new Matrix();
// Grey radial gradient to smooth the edges of the distortion
var cover:Shape = new Shape();
m.createGradientBox(w, w);
cover.graphics.beginGradientFill(GradientType.LINEAR, [0x808080, 0x808080], [1, 0], [0x00, 0xFF], m);
cover.graphics.drawRect(0, 0, w, w);
// Red gradient to distort pixels horizontally
var red:Shape = new Shape();
m.createGradientBox(h, h, 0, (w - h) / 2, 0);
red.graphics.beginGradientFill(GradientType.LINEAR, [0x000000, 0xFF0000], [1, 1], [0x00, 0xFF], m);
red.graphics.drawRect(0, 0, w, w);
// Green gradient to distort pixels vertically
var green:Shape = new Shape();
m.createGradientBox(h, h, Math.PI / 2, (w - h) / 2, 0);
green.graphics.beginGradientFill(GradientType.LINEAR, [0x000000, 0x00FF00], [1, 1], [0x00, 0xFF], m);
green.graphics.drawRect(0, 0, w, w);
// Draw red and green gradients into one displacement map bitmap
displace.mapBitmap.draw(red);
displace.mapBitmap.draw(green, null, null, BlendMode.ADD);
// Fade out the edges of the distorion linearly in each direction, up, down, left and right
m.identity();
m.scale(0.5, 1);
displace.mapBitmap.draw(cover, m);
m.rotate(Math.PI);
m.translate(w, h);
displace.mapBitmap.draw(cover, m);
m.identity();
m.scale(h / w / 2, 1);
m.rotate(Math.PI / 2);
m.translate(w, 0);
displace.mapBitmap.draw(cover, m);
m.rotate(Math.PI);
m.translate(w, h);
displace.mapBitmap.draw(cover, m);
// Set the size of the displacement
displace.scaleX = displace.scaleY = w;
}
}
}
|