path.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. define(function(require, exports, module) {
  2. var Utils = require('../core/utils');
  3. var createClass = require('../core/class').createClass;
  4. var Shape = require('./shape');
  5. var svg = require('./svg');
  6. var g = require('./geometry');
  7. var slice = Array.prototype.slice,
  8. flatten = Utils.flatten;
  9. var PathDrawer = createClass('PathDrawer', {
  10. constructor: function(path) {
  11. this.segment = [];
  12. this.path = path;
  13. this.__clear = false;
  14. },
  15. getPath: function() {
  16. return this.path;
  17. },
  18. redraw: function() {
  19. this._transation = this._transation || [];
  20. return this.clear();
  21. },
  22. done: function() {
  23. var transation = this._transation;
  24. this._transation = null;
  25. this.push(transation);
  26. return this;
  27. },
  28. clear: function() {
  29. if (this._transation) {
  30. this._transation = [];
  31. } else {
  32. this.path.setPathData('M 0 0');
  33. }
  34. this._clear = true;
  35. return this;
  36. },
  37. push: function() {
  38. var segment = slice.call(arguments);
  39. var originData;
  40. if (this._transation) {
  41. this._transation.push(segment);
  42. return this;
  43. }
  44. if (this._clear) {
  45. originData = '';
  46. this._clear = false;
  47. } else {
  48. originData = this.path.getPathData();
  49. }
  50. originData = originData || '';
  51. this.path.setPathData(originData + g.pathToString(segment));
  52. return this;
  53. },
  54. moveTo: function(x, y) {
  55. return this.push('M', slice.call(arguments));
  56. },
  57. moveBy: function(dx, dy) {
  58. return this.push('m', slice.call(arguments));
  59. },
  60. lineTo: function(x, y) {
  61. return this.push('L', slice.call(arguments));
  62. },
  63. lineBy: function(dx, dy) {
  64. return this.push('l', slice.call(arguments));
  65. },
  66. arcTo: function(rx, ry, xr, laf, sf, x, y) {
  67. return this.push('A', slice.call(arguments));
  68. },
  69. arcBy: function(rx, ry, xr, laf, sf, dx, dy) {
  70. return this.push('a', arguments);
  71. },
  72. carcTo: function(r, laf, sf, x, y) {
  73. return this.push('A', [r, r, 0].concat(slice.call(arguments, 1)));
  74. },
  75. carcBy: function(r, laf, sf, dx, dy) {
  76. return this.push('a', [r, r, 0].concat(slice.call(arguments, 1)));
  77. },
  78. bezierTo: function(x1, y1, x2, y2, x, y) {
  79. return this.push('C', slice.call(arguments));
  80. },
  81. bezierBy: function(dx1, dy1, dx2, dy2, dx, dy) {
  82. return this.push('c', slice.call(arguments));
  83. },
  84. close: function() {
  85. return this.push('z');
  86. }
  87. });
  88. return createClass('Path', {
  89. base: Shape,
  90. constructor: function(data) {
  91. this.callBase('path');
  92. if (data) {
  93. this.setPathData(data);
  94. }
  95. this.node.setAttribute('fill', svg.defaults.fill);
  96. this.node.setAttribute('stroke', svg.defaults.stroke);
  97. },
  98. setPathData: function(data) {
  99. data = data || 'M0,0';
  100. this.pathdata = g.pathToString(data);
  101. this.node.setAttribute('d', this.pathdata);
  102. this.trigger('shapeupdate', {
  103. type: 'pathdata'
  104. });
  105. return this;
  106. },
  107. getPathData: function() {
  108. return this.pathdata || '';
  109. },
  110. getDrawer: function() {
  111. return new PathDrawer(this);
  112. },
  113. isClosed: function() {
  114. var data = this.getPathData();
  115. return !!~data.indexOf('z') || !!~data.indexOf('Z');
  116. }
  117. });
  118. });