Skip to content

Commit

Permalink
fixed cache for first draw (#3870)
Browse files Browse the repository at this point in the history
* fixed cache for first draw

* fixed tests
  • Loading branch information
asturur committed Apr 23, 2017
1 parent 28d90b4 commit aff86b1
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 36 deletions.
3 changes: 3 additions & 0 deletions src/mixins/stateful.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
hasStateChanged: function(propertySet) {
propertySet = propertySet || originalSet;
propertySet = '_' + propertySet;
if (!Object.keys(this[propertySet]).length) {
return true;
}
return !_isEqual(this[propertySet], this, true);
},

Expand Down
17 changes: 3 additions & 14 deletions src/shapes/object.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -792,9 +792,9 @@
* When set to `true`, object's cache will be rerendered next render call.
* since 1.7.0
* @type Boolean
* @default false
* @default true
*/
dirty: false,
dirty: true,

/**
* When set to `true`, force the object to have its own cache, even if it is inside a group
Expand Down Expand Up @@ -839,7 +839,6 @@
}
if (this.objectCaching) {
this._createCacheCanvas();
this.setupState({ propertySet: 'cacheProperties' });
}
},

Expand All @@ -848,6 +847,7 @@
* @private
*/
_createCacheCanvas: function() {
this._cacheProperties = {};
this._cacheCanvas = fabric.document.createElement('canvas');
this._cacheContext = this._cacheCanvas.getContext('2d');
this._updateCacheCanvas();
Expand Down Expand Up @@ -1104,17 +1104,6 @@
// implemented by sub-classes, as needed.
},

/**
* Sets sourcePath of an object
* @param {String} value Value to set sourcePath to
* @return {fabric.Object} thisArg
* @chainable
*/
setSourcePath: function(value) {
this.sourcePath = value;
return this;
},

/**
* Retrieves viewportTransform from Object's canvas if possible
* @method getViewportTransform
Expand Down
4 changes: 0 additions & 4 deletions src/shapes/path.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,8 @@

this._setPositionDimensions(options);

if (options.sourcePath) {
this.setSourcePath(options.sourcePath);
}
if (this.objectCaching) {
this._createCacheCanvas();
this.setupState({ propertySet: 'cacheProperties' });
}
},

Expand Down
5 changes: 0 additions & 5 deletions src/shapes/path_group.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,8 @@
}
this.setOptions(options);
this.setCoords();
if (options.sourcePath) {
this.setSourcePath(options.sourcePath);
}
if (this.objectCaching) {
this._createCacheCanvas();
this.setupState({ propertySet: 'cacheProperties' });
}
},

Expand Down Expand Up @@ -258,7 +254,6 @@
fabric.PathGroup.fromObject = function(object, callback) {
var originalPaths = object.paths;
delete object.paths;
// remove this pattern from 2.0 accepts only object
if (typeof originalPaths === 'string') {
fabric.loadSVGFromURL(originalPaths, function (elements) {
var pathUrl = originalPaths;
Expand Down
2 changes: 1 addition & 1 deletion src/util/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@
object = new fabric.PathGroup(elements, options);

if (typeof path !== 'undefined') {
object.setSourcePath(path);
object.sourcePath = path;
}
return object;
},
Expand Down
13 changes: 3 additions & 10 deletions test/unit/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,6 @@
equal(0.123, cObj.getOpacity());
});

test('setSourcePath', function() {
var cObj = new fabric.Object();
var SRC_PATH = 'http://example.com/';

ok(typeof cObj.setSourcePath == 'function');

cObj.setSourcePath(SRC_PATH);
equal(cObj.get('sourcePath'), SRC_PATH);
});

test('stateProperties', function() {
var cObj = new fabric.Object();
ok(cObj.stateProperties);
Expand Down Expand Up @@ -1260,6 +1250,7 @@

test('isCacheDirty statefullCache disabled', function() {
var object = new fabric.Object({ scaleX: 3, scaleY: 2});
equal(object.dirty, true, 'object is dirty after creation');
object.cacheProperties = ['propA', 'propB'];
object.dirty = false;
object.statefullCache = false;
Expand All @@ -1277,6 +1268,8 @@
object.propA = 'A';
object.setupState({ propertySet: 'cacheProperties' });
object._createCacheCanvas();
equal(object.isCacheDirty(), true, 'object is dirty if canvas has been just created');
object.setupState({ propertySet: 'cacheProperties' });
equal(object.isCacheDirty(), false, 'object is not dirty');
object.propA = 'B';
equal(object.isCacheDirty(), true, 'object is dirty because change in propA is detected by statefullCache');
Expand Down
2 changes: 1 addition & 1 deletion test/unit/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
deepEqual(path.toDatalessObject(), REFERENCE_PATH_OBJECT);

var src = 'http://example.com/';
path.setSourcePath(src);
path.sourcePath = src;
deepEqual(path.toDatalessObject(), fabric.util.object.extend(fabric.util.object.clone(REFERENCE_PATH_OBJECT), {
path: src
}));
Expand Down
2 changes: 1 addition & 1 deletion test/unit/path_group.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@
getPathGroupObject(function(pathGroup) {
ok(typeof pathGroup.toDatalessObject == 'function');

pathGroup.setSourcePath('http://example.com/');
pathGroup.sourcePath = 'http://example.com/';
var expectedObject = fabric.util.object.extend(fabric.util.object.clone(REFERENCE_PATH_GROUP_OBJECT), {
'paths': 'http://example.com/',
'sourcePath': 'http://example.com/'
Expand Down

0 comments on commit aff86b1

Please sign in to comment.