PageRenderTime 6092ms CodeModel.GetById 38ms RepoModel.GetById 2ms app.codeStats 0ms

/Onyx-VJ 4.3.0/Onyx-Patches/src/library/patches/Imagination.as

http://onyx-vj.googlecode.com/
ActionScript | 355 lines | 199 code | 44 blank | 112 comment | 8 complexity | 6d46d57f67b80f00e5d72defe9f3f668 MD5 | raw file
  1. /**
  2. * Copyright (c) 2003-2010, www.onyx-vj.com
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * - Neither the name of the www.onyx-vj.com nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software without
  17. * specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. * Based on Imagination code by Paul Neave (http://www.neave.com)
  31. * Adapted for Onyx-VJ by Bruce LANE (http://www.batchass.fr)
  32. */
  33. package library.patches
  34. {
  35. import flash.display.Bitmap;
  36. import flash.display.BitmapData;
  37. import flash.display.BlendMode;
  38. import flash.display.Graphics;
  39. import flash.display.Shape;
  40. import flash.display.Sprite;
  41. import flash.events.Event;
  42. import flash.events.MouseEvent;
  43. import flash.filters.BlurFilter;
  44. import flash.geom.Matrix;
  45. import flash.geom.Point;
  46. import onyx.core.Console;
  47. import onyx.core.RenderInfo;
  48. import onyx.plugin.DISPLAY_HEIGHT;
  49. import onyx.plugin.DISPLAY_WIDTH;
  50. import onyx.plugin.Patch;
  51. import onyx.plugin.createDefaultBitmap;
  52. //[SWF(width='320', height='240', frameRate='24')]
  53. public class Imagination extends Patch
  54. {
  55. // Main constants
  56. private const RED_STEP:Number = 0.02;
  57. private const GREEN_STEP:Number = 0.015;
  58. private const BLUE_STEP:Number = 0.025;
  59. private const MAX_LENGTH:int = 80;
  60. private const SPREAD_MIN:int = 1;
  61. private const SPREAD_MAX:int = 40;
  62. // Main variables
  63. private var canvasWidth:int;
  64. private var canvasHeight:int;
  65. private var list:Array;
  66. private var px:Number;
  67. private var py:Number;
  68. private var size:Number;
  69. private var spread:int;
  70. private var paused:Boolean;
  71. private var red:Number;
  72. private var green:Number;
  73. private var blue:Number;
  74. private var lines:Shape;
  75. private var bmp:Bitmap;
  76. private var blackBitmap:BitmapData;
  77. private var m:Matrix;
  78. private var p:Point;
  79. private var blur:BlurFilter;
  80. private var _sourceBD:BitmapData = createDefaultBitmap();
  81. private var sprite:Sprite;
  82. public function Imagination()
  83. {
  84. Console.output('Imagination 4.2.1');
  85. Console.output('Credits to Paul NEAVE (http://www.neave.com)');
  86. Console.output('Adapted by Bruce LANE (http://www.batchass.fr)');
  87. sprite = new Sprite();
  88. addChild(sprite);
  89. canvasWidth = DISPLAY_WIDTH;
  90. canvasHeight = DISPLAY_HEIGHT;
  91. initStage();
  92. initLines();
  93. initBitmap();
  94. }
  95. /**
  96. *
  97. */
  98. override public function render(info:RenderInfo):void {
  99. if (paused) return;
  100. var source:BitmapData = info.source;
  101. // Line movement is set by how much the mouse has moved since its previous position and a random value
  102. var dx:Number = (px - px + Math.random() * 4 - 2) / 2;
  103. var dy:Number = (py - py + Math.random() * 4 - 2) / 2;
  104. // Limit the amount of movement
  105. if (dx < -spread) dx = -spread;
  106. if (dx > spread) dx = spread;
  107. if (dy < -spread) dy = -spread;
  108. if (dy > spread) dy = spread;
  109. // Store the mouse position
  110. //px = sprite.mouseX;
  111. //py = sprite.mouseY;
  112. // Line thickness varies up and down with sine
  113. var s:Number = Math.sin(size += 0.2) * 8 + 4;
  114. // Put the red, green and blue values together into a single hexadecimal value
  115. var c:uint = (Math.sin(red += RED_STEP) * 128 + 127) << 16
  116. | (Math.sin(green += GREEN_STEP) * 128 + 127) << 8
  117. | (Math.sin(blue += BLUE_STEP) * 128 + 127);
  118. // Create a new point on the line
  119. list.push(new ImaginationPoint(px, py, dx, dy, s, c));
  120. // Draw!
  121. drawLines();
  122. drawBitmap();
  123. info.render( sprite );
  124. }
  125. /**
  126. * Sets up stage listeners
  127. */
  128. private function initStage():void
  129. {
  130. addEventListener(Event.RESIZE, stageResizeListener);
  131. addEventListener(Event.MOUSE_LEAVE, mouseLeaveListener);
  132. addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveListener);
  133. addEventListener(MouseEvent.MOUSE_DOWN, mouseDownListener);
  134. addEventListener(MouseEvent.MOUSE_UP, mouseUpListener);
  135. }
  136. /**
  137. * Sets up line variables
  138. */
  139. private function initLines():void
  140. {
  141. list = new Array();
  142. px = py = size = 0;
  143. spread = SPREAD_MAX;
  144. paused = false;
  145. // Start using red
  146. red = 0;
  147. green = 255;
  148. blue = 255;
  149. // The main lines shape
  150. lines = new Shape();
  151. }
  152. /**
  153. * Sets up the main bitmap
  154. */
  155. private function initBitmap():void
  156. {
  157. // Stage sizes
  158. var sw:int = DISPLAY_WIDTH;
  159. var sh:int = DISPLAY_HEIGHT;
  160. var sw2:int = Math.ceil(sw / 2);
  161. var sh2:int = Math.ceil(sh / 2);
  162. // Create the main bitmap to draw into (and half the size to run faster)
  163. bmp = new Bitmap(new BitmapData(sw2, sh2, true, 0xFF000000));
  164. bmp.smoothing = true;
  165. bmp.scaleX = bmp.scaleY = 2;
  166. bmp.x = (canvasWidth - sw) / 2;
  167. bmp.y = (canvasHeight - sh) / 2;
  168. sprite.addChild(bmp);
  169. // Create bitmap data for fading into black
  170. blackBitmap = new BitmapData(sw2, sh2, true, 0xFF000000);
  171. // Bitmap is moved over into position then halved in size to run faster
  172. m = new Matrix();
  173. m.translate(-bmp.x, -bmp.y);
  174. m.scale(0.5, 0.5);
  175. // Origin and blur filter
  176. p = new Point(0, 0);
  177. blur = new BlurFilter(4, 4, 1);
  178. }
  179. /**
  180. * Draws the line animation
  181. */
  182. private function drawLines():void
  183. {
  184. // Clear the graphics before we draw the lines
  185. var g:Graphics = lines.graphics;
  186. g.clear();
  187. g.moveTo(px, py);
  188. // Draw a curve through all points in the list
  189. for (var i:int = list.length - 1; i > 0; i--)
  190. {
  191. // Animate the lines outwards
  192. list[i].x += list[i].dx;
  193. list[i].y += list[i].dy;
  194. // Draw the curve, fading out the last 8 points with alpha
  195. g.lineStyle(list[i].size, list[i].color, (list.length > (MAX_LENGTH - 8) && i < 8) ? i / 8 : 1);
  196. g.curveTo(list[i].x, list[i].y, (list[i].x + list[i - 1].x) / 2, (list[i].y + list[i - 1].y) / 2);
  197. // Remove the last point from the list if we've reached the maximum length
  198. if (list.length > MAX_LENGTH) list.splice(0, 1);
  199. }
  200. }
  201. /**
  202. * Draws the lines into the bitmap with a fade effect
  203. */
  204. private function drawBitmap():void
  205. {
  206. // Repeatedly fade out and blur the lines then draw in the new ones
  207. var b:BitmapData = bmp.bitmapData;
  208. b.lock();
  209. b.merge(blackBitmap, b.rect, p, 4, 4, 4, 0);
  210. b.applyFilter(b, b.rect, p, blur);
  211. b.draw(lines, m, null, BlendMode.ADD);
  212. b.unlock();
  213. }
  214. /**
  215. * Listens for when the mouse leaves the stage
  216. */
  217. private function mouseLeaveListener(e:Event):void
  218. {
  219. paused = true;
  220. }
  221. /**
  222. * Listens for when the mouse re-enters the stage
  223. */
  224. private function mouseMoveListener(e:MouseEvent):void
  225. {
  226. paused = false;
  227. px = e.localX;
  228. py = e.localY;
  229. }
  230. /**
  231. * Listens for mouse press
  232. */
  233. private function mouseDownListener(e:MouseEvent):void
  234. {
  235. // Allow a kind of drawing-mode when mouse is pressed
  236. spread = SPREAD_MIN;
  237. }
  238. /**
  239. * Listens for mouse press
  240. */
  241. private function mouseUpListener(e:MouseEvent):void
  242. {
  243. // Spread lines out when mouse is released
  244. spread = SPREAD_MAX;
  245. }
  246. /**
  247. * Frees up memory by disposing all bitmap data
  248. */
  249. private function disposeBitmaps():void
  250. {
  251. bmp.bitmapData.dispose();
  252. bmp.bitmapData = null;
  253. blackBitmap.dispose();
  254. blackBitmap = null;
  255. }
  256. /**
  257. * Removes Neave Imagination and all other objects
  258. */
  259. public function destroy():void
  260. {
  261. removeEventListener(Event.RESIZE, stageResizeListener);
  262. removeEventListener(Event.MOUSE_LEAVE, mouseLeaveListener);
  263. removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveListener);
  264. removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownListener);
  265. removeEventListener(MouseEvent.MOUSE_UP, mouseUpListener);
  266. sprite.removeChild(bmp);
  267. disposeBitmaps();
  268. }
  269. /**
  270. * Listens for stage resize
  271. */
  272. private function stageResizeListener(e:Event):void
  273. {
  274. // Start again with the bitmap if the stage is resized
  275. sprite.removeChild(bmp);
  276. disposeBitmaps();
  277. initBitmap();
  278. }
  279. }
  280. }
  281. final class ImaginationPoint
  282. {
  283. // Position variables
  284. internal var x:Number;
  285. internal var y:Number;
  286. // Movement variables
  287. internal var dx:Number;
  288. internal var dy:Number;
  289. // Other variables
  290. internal var size:Number;
  291. internal var color:uint;
  292. /**
  293. * Declares a 'Neave Imagination' point, a point with extra properties
  294. *
  295. * @param x The x position
  296. * @param y The y position
  297. * @param dx The movement in the x-axis
  298. * @param dy The movement in the y-axis
  299. * @param size The size of the line
  300. * @param color The colour of the line
  301. */
  302. public function ImaginationPoint(x:Number, y:Number, dx:Number, dy:Number, size:Number, color:uint)
  303. {
  304. this.x = x;
  305. this.y = y;
  306. this.dx = dx;
  307. this.dy = dy;
  308. this.size = size;
  309. this.color = color;
  310. }
  311. }