Skip to content

Commit

Permalink
fix group becoming cachrable (fabricjs#5021)
Browse files Browse the repository at this point in the history
* fix group becoming cacheable
  • Loading branch information
asturur authored Jun 4, 2018
1 parent f144a59 commit fa9fb8a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/shapes/object.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,8 @@
this._cacheCanvas = fabric.document.createElement('canvas');
this._cacheContext = this._cacheCanvas.getContext('2d');
this._updateCacheCanvas();
// if canvas gets created, is empty, so dirty.
this.dirty = true;
},

/**
Expand Down Expand Up @@ -1002,6 +1004,7 @@
if (this.shouldCache()) {
if (!this._cacheCanvas) {
this._createCacheCanvas();

}
if (this.isCacheDirty()) {
this.statefullCache && this.saveState({ propertySet: 'cacheProperties' });
Expand Down
13 changes: 9 additions & 4 deletions test/unit/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -1019,13 +1019,21 @@
assert.equal(object.dirty, true, 'after setting a property from cache, dirty flag is true');
});

QUnit.test('_createCacheCanvas sets object as dirty', function(assert) {
var object = new fabric.Object({ scaleX: 3, scaleY: 2, width: 1, height: 2});
assert.equal(object.dirty, true, 'object is dirty after creation');
object.dirty = false;
assert.equal(object.dirty, false, 'object is not dirty after specifying it');
object._createCacheCanvas();
assert.equal(object.dirty, true, 'object is dirty again if cache gets created');
});

QUnit.test('isCacheDirty statefullCache disabled', function(assert) {
var object = new fabric.Object({ scaleX: 3, scaleY: 2, width: 1, height: 2});
assert.equal(object.dirty, true, 'object is dirty after creation');
object.cacheProperties = ['propA', 'propB'];
object.dirty = false;
object.statefullCache = false;
object._createCacheCanvas();
assert.equal(object.isCacheDirty(), false, 'object is not dirty if dirty flag is false');
object.dirty = true;
assert.equal(object.isCacheDirty(), true, 'object is dirty if dirty flag is true');
Expand All @@ -1038,9 +1046,6 @@
object.statefullCache = true;
object.propA = 'A';
object.setupState({ propertySet: 'cacheProperties' });
object._createCacheCanvas();
assert.equal(object.isCacheDirty(), true, 'object is dirty if canvas has been just created');
object.setupState({ propertySet: 'cacheProperties' });
assert.equal(object.isCacheDirty(), false, 'object is not dirty');
object.propA = 'B';
assert.equal(object.isCacheDirty(), true, 'object is dirty because change in propA is detected by statefullCache');
Expand Down

0 comments on commit fa9fb8a

Please sign in to comment.