Skip to content

Commit

Permalink
add custom props to pattern and gradient (#3477)
Browse files Browse the repository at this point in the history
  • Loading branch information
asturur authored Dec 1, 2016
1 parent 6db3f46 commit 8a6b17c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/gradient.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,21 @@

/**
* Returns object representation of a gradient
* @param {Array} [propertiesToInclude] Any properties that you might want to additionally include in the output
* @return {Object}
*/
toObject: function() {
return {
toObject: function(propertiesToInclude) {
var object = {
type: this.type,
coords: this.coords,
colorStops: this.colorStops,
offsetX: this.offsetX,
offsetY: this.offsetY,
gradientTransform: this.gradientTransform ? this.gradientTransform.concat() : this.gradientTransform
};
fabric.util.populateWithProperties(this, object, propertiesToInclude);

return object;
},

/* _TO_SVG_START_ */
Expand Down
10 changes: 7 additions & 3 deletions src/pattern.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ fabric.Pattern = fabric.util.createClass(/** @lends fabric.Pattern.prototype */

/**
* Returns object representation of a pattern
* @param {Array} [propertiesToInclude] Any properties that you might want to additionally include in the output
* @return {Object} Object representation of a pattern instance
*/
toObject: function() {
toObject: function(propertiesToInclude) {

var source;
var source, object;

// callback
if (typeof this.source === 'function') {
Expand All @@ -90,12 +91,15 @@ fabric.Pattern = fabric.util.createClass(/** @lends fabric.Pattern.prototype */
source = this.source.toDataURL();
}

return {
object = {
source: source,
repeat: this.repeat,
offsetX: this.offsetX,
offsetY: this.offsetY
};
fabric.util.populateWithProperties(this, object, propertiesToInclude);

return object;
},

/* _TO_SVG_START_ */
Expand Down
7 changes: 7 additions & 0 deletions test/unit/gradient.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@
equal(object.colorStops, gradient.colorStops);
});

test('toObject with custom props', function() {
var gradient = createLinearGradient();
gradient.id = 'myId';
var object = gradient.toObject(['id']);
equal(object.id, 'myId');
});

test('toObject radialGradient', function() {
var gradient = createRadialGradient();

Expand Down
7 changes: 7 additions & 0 deletions test/unit/pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@
equal(object2.repeat, 'repeat');
});

test('toObject with custom props', function() {
var pattern = createPattern();
pattern.id = 'myId';
var object = pattern.toObject(['id']);
equal(object.id, 'myId');
});

test('toLive', function() {
var pattern = createPattern();

Expand Down

0 comments on commit 8a6b17c

Please sign in to comment.