Skip to content

Commit 50ee598

Browse files
committed
Introduce Project#_drawCount mechanism as a better and more efficient way to filter out selected items that are not currently part of the DOM.
1 parent e22b329 commit 50ee598

3 files changed

Lines changed: 23 additions & 16 deletions

File tree

src/item/Item.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,7 @@ var Item = this.Item = Base.extend(Callback, /** @lends Item# */{
12061206
*/
12071207
insertChild: function(index, item) {
12081208
if (this._children) {
1209-
item._remove(false, true);
1209+
item._remove(true);
12101210
Base.splice(this._children, [item], index, 0);
12111211
item._parent = this;
12121212
item._setProject(this._project);
@@ -1353,10 +1353,8 @@ var Item = this.Item = Base.extend(Callback, /** @lends Item# */{
13531353
/**
13541354
* Removes the item from its parent's children list.
13551355
*/
1356-
_remove: function(deselect, notify) {
1356+
_remove: function(notify) {
13571357
if (this._parent) {
1358-
if (deselect)
1359-
this.setSelected(false);
13601358
if (this._name)
13611359
this._removeFromNamed();
13621360
if (this._index != null)
@@ -1377,7 +1375,7 @@ var Item = this.Item = Base.extend(Callback, /** @lends Item# */{
13771375
* @return {Boolean} {@true the item was removed}
13781376
*/
13791377
remove: function() {
1380-
return this._remove(true, true);
1378+
return this._remove(true);
13811379
},
13821380

13831381
/**
@@ -1407,7 +1405,7 @@ var Item = this.Item = Base.extend(Callback, /** @lends Item# */{
14071405
// fine, since it only calls Base.splice() if #_index is set.
14081406
var removed = Base.splice(this._children, null, from, to - from);
14091407
for (var i = removed.length - 1; i >= 0; i--)
1410-
removed[i]._remove(true, false);
1408+
removed[i]._remove(false);
14111409
if (removed.length > 0)
14121410
this._changed(/*#=*/ Change.HIERARCHY);
14131411
return removed;
@@ -2493,6 +2491,11 @@ var Item = this.Item = Base.extend(Callback, /** @lends Item# */{
24932491
draw: function(item, ctx, param) {
24942492
if (!item._visible || item._opacity == 0)
24952493
return;
2494+
// Each time the project gets drawn, it's _drawCount is increased.
2495+
// Keep the _drawCount of drawn items in sync, so we have an easy
2496+
// way to filter out selected items that are not being drawn, e.g.
2497+
// because they are currently not part of the DOM.
2498+
item._drawCount = item._project._drawCount;
24962499
var tempCanvas, parentCtx,
24972500
itemOffset, prevOffset;
24982501
// If the item has a blendMode or is defining an opacity, draw it on

src/item/Layer.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,10 @@ var Layer = this.Layer = Group.extend(/** @lends Layer# */{
5252
* Removes the layer from its project's layers list
5353
* or its parent's children list.
5454
*/
55-
_remove: function(deselect, notify) {
55+
_remove: function(notify) {
5656
if (this._parent)
57-
return this.base(deselect, notify);
57+
return this.base(notify);
5858
if (this._index != null) {
59-
if (deselect)
60-
this.setSelected(false);
6159
Base.splice(this._project.layers, null, this._index, 1);
6260
// Tell project we need a redraw. This is similar to _changed()
6361
// mechanism.
@@ -96,7 +94,7 @@ var Layer = this.Layer = Group.extend(/** @lends Layer# */{
9694
// If the item is a layer and contained within Project#layers, use
9795
// our own version of move().
9896
if (item instanceof Layer && !item._parent
99-
&& this._remove(false, true)) {
97+
&& this._remove(true)) {
10098
Base.splice(item._project.layers, [this],
10199
item._index + (above ? 1 : 0), 0);
102100
this._setProject(item._project);

src/project/Project.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ var Project = this.Project = PaperScopeItem.extend(/** @lends Project# */{
6060
this._currentStyle = new PathStyle();
6161
this._selectedItems = {};
6262
this._selectedItemCount = 0;
63+
// See Item.draw() for an explanation of _drawCount
64+
this._drawCount = 0;
6365
// Change tracking, not in use for now. Activate once required:
6466
// this._changes = [];
6567
// this._changesById = {};
@@ -153,9 +155,11 @@ var Project = this.Project = PaperScopeItem.extend(/** @lends Project# */{
153155
// TODO: The order of these items should be that of their
154156
// drawing order.
155157
var items = [];
156-
Base.each(this._selectedItems, function(item) {
157-
items.push(item);
158-
});
158+
for (var id in this._selectedItems) {
159+
var item = this._selectedItems[id];
160+
if (item._drawCount === this._drawCount)
161+
items.push(item);
162+
}
159163
return items;
160164
},
161165

@@ -184,7 +188,7 @@ var Project = this.Project = PaperScopeItem.extend(/** @lends Project# */{
184188
*/
185189
deselectAll: function() {
186190
for (var i in this._selectedItems)
187-
this._selectedItems[i].setSelected(false);
191+
this._selectedItems[i].item.setSelected(false);
188192
},
189193

190194
/**
@@ -260,6 +264,7 @@ var Project = this.Project = PaperScopeItem.extend(/** @lends Project# */{
260264
*/
261265

262266
draw: function(ctx, matrix) {
267+
this._drawCount++;
263268
ctx.save();
264269
if (!matrix.isIdentity())
265270
matrix.applyToContext(ctx);
@@ -303,7 +308,8 @@ var Project = this.Project = PaperScopeItem.extend(/** @lends Project# */{
303308
}
304309
for (var id in this._selectedItems) {
305310
var item = this._selectedItems[id];
306-
item.drawSelected(ctx, getGlobalMatrix(item, matrix.clone()));
311+
if (item._drawCount === this._drawCount)
312+
item.drawSelected(ctx, getGlobalMatrix(item, matrix.clone()));
307313
}
308314
ctx.restore();
309315
}

0 commit comments

Comments
 (0)