IncrementalDisplayable.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. var _util = require("../core/util");
  2. var inherits = _util.inherits;
  3. var Displayble = require("./Displayable");
  4. var BoundingRect = require("../core/BoundingRect");
  5. /**
  6. * Displayable for incremental rendering. It will be rendered in a separate layer
  7. * IncrementalDisplay have two main methods. `clearDisplayables` and `addDisplayables`
  8. * addDisplayables will render the added displayables incremetally.
  9. *
  10. * It use a not clearFlag to tell the painter don't clear the layer if it's the first element.
  11. */
  12. // TODO Style override ?
  13. function IncrementalDisplayble(opts) {
  14. Displayble.call(this, opts);
  15. this._displayables = [];
  16. this._temporaryDisplayables = [];
  17. this._cursor = 0;
  18. this.notClear = true;
  19. }
  20. IncrementalDisplayble.prototype.incremental = true;
  21. IncrementalDisplayble.prototype.clearDisplaybles = function () {
  22. this._displayables = [];
  23. this._temporaryDisplayables = [];
  24. this._cursor = 0;
  25. this.dirty();
  26. this.notClear = false;
  27. };
  28. IncrementalDisplayble.prototype.addDisplayable = function (displayable, notPersistent) {
  29. if (notPersistent) {
  30. this._temporaryDisplayables.push(displayable);
  31. } else {
  32. this._displayables.push(displayable);
  33. }
  34. this.dirty();
  35. };
  36. IncrementalDisplayble.prototype.addDisplayables = function (displayables, notPersistent) {
  37. notPersistent = notPersistent || false;
  38. for (var i = 0; i < displayables.length; i++) {
  39. this.addDisplayable(displayables[i], notPersistent);
  40. }
  41. };
  42. IncrementalDisplayble.prototype.eachPendingDisplayable = function (cb) {
  43. for (var i = this._cursor; i < this._displayables.length; i++) {
  44. cb && cb(this._displayables[i]);
  45. }
  46. for (var i = 0; i < this._temporaryDisplayables.length; i++) {
  47. cb && cb(this._temporaryDisplayables[i]);
  48. }
  49. };
  50. IncrementalDisplayble.prototype.update = function () {
  51. this.updateTransform();
  52. for (var i = this._cursor; i < this._displayables.length; i++) {
  53. var displayable = this._displayables[i]; // PENDING
  54. displayable.parent = this;
  55. displayable.update();
  56. displayable.parent = null;
  57. }
  58. for (var i = 0; i < this._temporaryDisplayables.length; i++) {
  59. var displayable = this._temporaryDisplayables[i]; // PENDING
  60. displayable.parent = this;
  61. displayable.update();
  62. displayable.parent = null;
  63. }
  64. };
  65. IncrementalDisplayble.prototype.brush = function (ctx, prevEl) {
  66. // Render persistant displayables.
  67. for (var i = this._cursor; i < this._displayables.length; i++) {
  68. var displayable = this._displayables[i];
  69. displayable.beforeBrush && displayable.beforeBrush(ctx);
  70. displayable.brush(ctx, i === this._cursor ? null : this._displayables[i - 1]);
  71. displayable.afterBrush && displayable.afterBrush(ctx);
  72. }
  73. this._cursor = i; // Render temporary displayables.
  74. for (var i = 0; i < this._temporaryDisplayables.length; i++) {
  75. var displayable = this._temporaryDisplayables[i];
  76. displayable.beforeBrush && displayable.beforeBrush(ctx);
  77. displayable.brush(ctx, i === 0 ? null : this._temporaryDisplayables[i - 1]);
  78. displayable.afterBrush && displayable.afterBrush(ctx);
  79. }
  80. this._temporaryDisplayables = [];
  81. this.notClear = true;
  82. };
  83. var m = [];
  84. IncrementalDisplayble.prototype.getBoundingRect = function () {
  85. if (!this._rect) {
  86. var rect = new BoundingRect(Infinity, Infinity, -Infinity, -Infinity);
  87. for (var i = 0; i < this._displayables.length; i++) {
  88. var displayable = this._displayables[i];
  89. var childRect = displayable.getBoundingRect().clone();
  90. if (displayable.needLocalTransform()) {
  91. childRect.applyTransform(displayable.getLocalTransform(m));
  92. }
  93. rect.union(childRect);
  94. }
  95. this._rect = rect;
  96. }
  97. return this._rect;
  98. };
  99. IncrementalDisplayble.prototype.contain = function (x, y) {
  100. var localPos = this.transformCoordToLocal(x, y);
  101. var rect = this.getBoundingRect();
  102. if (rect.contain(localPos[0], localPos[1])) {
  103. for (var i = 0; i < this._displayables.length; i++) {
  104. var displayable = this._displayables[i];
  105. if (displayable.contain(x, y)) {
  106. return true;
  107. }
  108. }
  109. }
  110. return false;
  111. };
  112. inherits(IncrementalDisplayble, Displayble);
  113. var _default = IncrementalDisplayble;
  114. module.exports = _default;