Readable Radial Gradient

Actionscript:
  1. var size:Number = 400;
  2. var halfSize:Number = size / 2;
  3. var canvas:BitmapData = new BitmapData(size, size,false, 0x000000);
  4. addChild(new Bitmap(canvas, PixelSnapping.AUTO, true));
  5.  
  6. var rings:Number = 40;
  7. var color:uint, theta:Number, dx:Number, dy:Number;
  8.  
  9. for (var i:int = 0; i<canvas.width; i++){
  10.     for (var j:int = 0; j<canvas.height; j++){
  11.         // distance from center of canvas to current x, y coord
  12.         dx = i - halfSize;
  13.         dy = j - halfSize;
  14.         theta = Math.sqrt((dx * dx)+(dy * dy)) * rings;
  15.         color = (100 + 100  * Math.sin(theta * Math.PI/180)) <<16;
  16.         canvas.setPixel(i,  j, color);
  17.     }
  18. }
  19.  
  20. /*
  21. WARNING: This code was written for fun. Use at your own risk.
  22. */

This is the previous post written in a readable way. Draws a radial gradient using pythagorean theorem and sine.

This entry was posted in BitmapData, pixel manipulation, setPixel. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*