poly.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * 通过点来决定图形的公共父类
  3. */
  4. define(function(require, exports, module) {
  5. var Utils = require('../core/utils');
  6. return require('../core/class').createClass('Poly', {
  7. base: require('./path'),
  8. mixins: [require('./pointcontainer')],
  9. constructor: function(points, closeable) {
  10. this.callBase();
  11. //是否可闭合
  12. this.closeable = !!closeable;
  13. this.setPoints(points || []);
  14. this.changeable = true;
  15. this.update();
  16. },
  17. //当点集合发生变化时采取的动作
  18. onContainerChanged: function() {
  19. if (this.changeable) {
  20. this.update();
  21. }
  22. },
  23. update: function() {
  24. var drawer = this.getDrawer(),
  25. points = this.getPoints();
  26. drawer.clear();
  27. if (!points.length) {
  28. return this;
  29. }
  30. drawer.moveTo(points[0]);
  31. for (var i = 1, point, len = points.length; i < len; i++) {
  32. point = points[i];
  33. drawer.lineTo(point);
  34. }
  35. if (this.closeable && points.length > 2) {
  36. drawer.close();
  37. }
  38. return this;
  39. }
  40. });
  41. });