star.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. define(function(require, exports, module) {
  2. /**
  3. * @see http://www.jdawiseman.com/papers/easymath/surds_star_inner_radius.html
  4. */
  5. var defaultRatioForStar = {
  6. '3': 0.2, // yy
  7. '5': 0.38196601125,
  8. '6': 0.57735026919,
  9. '8': 0.541196100146,
  10. '10': 0.726542528005,
  11. '12': 0.707106781187
  12. };
  13. var Point = require('./point');
  14. return require('../core/class').createClass('Star', {
  15. base: require('./path'),
  16. constructor: function(vertex, radius, shrink, offset, angleOffset) {
  17. this.callBase();
  18. this.vertex = vertex || 3;
  19. this.radius = radius || 0;
  20. this.shrink = shrink;
  21. this.offset = offset || new Point(0, 0);
  22. this.angleOffset = angleOffset || 0;
  23. this.draw();
  24. },
  25. getVertex: function() {
  26. return this.vertex;
  27. },
  28. setVertex: function(value) {
  29. this.vertex = value;
  30. return this.draw();
  31. },
  32. getRadius: function() {
  33. return this.radius;
  34. },
  35. setRadius: function(value) {
  36. this.radius = value;
  37. return this.draw();
  38. },
  39. getShrink: function() {
  40. return this.shrink;
  41. },
  42. setShrink: function(value) {
  43. this.shrink = value;
  44. return this.draw();
  45. },
  46. getOffset: function() {
  47. return this.offset;
  48. },
  49. setOffset: function(value) {
  50. this.offset = value;
  51. return this.draw();
  52. },
  53. getAngleOffset: function() {
  54. return this.angleOffset;
  55. },
  56. setAngleOffset: function(value) {
  57. this.angleOffset = value;
  58. return this.draw();
  59. },
  60. draw: function() {
  61. var innerRadius = this.radius,
  62. outerRadius = this.radius * (this.shrink || defaultRatioForStar[this.vertex] || 0.5),
  63. vertex = this.vertex,
  64. offset = this.offset,
  65. angleStart = 90,
  66. angleStep = 180 / vertex,
  67. angleOffset = this.angleOffset,
  68. drawer = this.getDrawer(),
  69. i, angle;
  70. drawer.clear();
  71. drawer.moveTo(Point.fromPolar(outerRadius, angleStart));
  72. for (i = 1; i <= vertex * 2; i++) {
  73. angle = angleStart + angleStep * i;
  74. // 绘制内点
  75. if (i % 2) {
  76. drawer.lineTo(Point.fromPolar(innerRadius, angle + angleOffset).offset(offset));
  77. }
  78. // 绘制外点
  79. else {
  80. drawer.lineTo(Point.fromPolar(outerRadius, angle));
  81. }
  82. }
  83. drawer.close();
  84. }
  85. });
  86. });