Improved Spiderweb ActionScript3.0 Class


/ Published in: ActionScript 3
Save to your folder(s)

Check the URL to see image examples


Copy this code and paste it in your HTML
  1. package com.blogspot.flanture.drawing
  2. {
  3.  
  4. // Spiderweb drawing class
  5. // author: http://flanture.blogspot.com
  6. // version: 1.0
  7. // February 2010
  8.  
  9. import flash.display.Sprite;
  10. import flash.geom.Point;
  11.  
  12. public class spiderweb extends Sprite
  13. {
  14. private var _circles:uint; // number of concentric circles
  15. private var _nods:uint; // number of nods on each circle
  16. private var _df:Number; // circles distance factor
  17. private var _fr:Number; // smallest circle radius
  18. private var _sf:Number; // stretch factor
  19.  
  20. public function spiderweb(circles:uint, nods:uint, fr:Number, df:Number, sf:Number)
  21. {
  22. _circles = circles;
  23. _nods = nods;
  24. _fr = fr;
  25. _df = df;
  26. _sf = sf;
  27.  
  28. init();
  29. }
  30.  
  31. private function init():void
  32. {
  33.  
  34. var cx = 0;
  35. var cy = 0;
  36. var coords:Array = new Array();
  37. var controls:Array = new Array();
  38. var angle:Number;
  39. var controlAngle:Number;
  40. var currR:Number;
  41. var angleStep:Number = 360 / _nods;
  42. currR = _fr;
  43.  
  44. for(var k:uint = 0; k < _circles; k++)
  45. {
  46. currR += currR * _df;
  47.  
  48. for (var i:uint = 0; i < _nods; i++)
  49. {
  50. angle = angleStep * (i + 1);
  51. controlAngle = angle - angleStep / 2;
  52.  
  53. controlAngle = controlAngle * Math.PI / 180;
  54. angle = angle * Math.PI / 180;
  55.  
  56. var x1:Number = trim(currR * Math.cos(angle));
  57. var y1:Number = trim(currR * Math.sin(angle));
  58.  
  59. var tx:Number = trim(currR * Math.cos(controlAngle));
  60. var ty:Number = trim(currR * Math.sin(controlAngle));
  61.  
  62. tx *= _sf;
  63. ty *= _sf;
  64.  
  65. var cpoint:Point = new Point(tx, ty);
  66. controls.push(cpoint);
  67.  
  68. var point:Point = new Point(x1, y1);
  69. coords.push(point);
  70.  
  71. }
  72.  
  73. drawStraights(coords);
  74. drawCurves(coords, controls);
  75. coords.splice(0, _nods);
  76. controls.splice(0, _nods);
  77. }
  78. }
  79.  
  80. private function trim(num:Number):Number
  81. {
  82. var nstr:int = int(num*100);
  83. return Number(nstr / 100);
  84. }
  85.  
  86. private function drawStraights(array):void
  87. {
  88. this.graphics.lineStyle(1, 0xffffff);
  89. this.graphics.moveTo(0,0);
  90. for (var i:uint = 0; i < array.length; i++)
  91. {
  92. this.graphics.lineTo(array[i].x, array[i].y);
  93. this.graphics.moveTo(0,0);
  94. }
  95. }
  96.  
  97. private function drawCurves(arrayA, arrayB):void
  98. {
  99.  
  100. this.graphics.lineStyle(2, 0xffffff);
  101. for (var j:uint = 0; j < arrayA.length-1; j++)
  102. {
  103. this.graphics.moveTo(0,0);
  104. this.graphics.moveTo(arrayA[j].x, arrayA[j].y);
  105. this.graphics.curveTo(arrayB[j+1].x, arrayB[j+1].y, arrayA[j+1].x, arrayA[j+1].y);
  106. this.graphics.moveTo(0,0);
  107. }
  108. this.graphics.moveTo(arrayA[arrayA.length-1].x, arrayA[arrayA.length-1].y);
  109. this.graphics.curveTo(arrayB[0].x, arrayB[0].y, arrayA[0].x, arrayA[0].y);
  110. }
  111. }
  112. }

URL: http://flanture.blogspot.com/2010/02/improved-spiderweb-as30-class.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.