2013-02-03 01:12:38 +00:00
|
|
|
/**
|
|
|
|
|
* Pattern class
|
2013-04-25 18:21:32 +00:00
|
|
|
* @class fabric.Pattern
|
2013-02-03 01:12:38 +00:00
|
|
|
*/
|
2013-04-24 16:58:04 +00:00
|
|
|
fabric.Pattern = fabric.util.createClass(/** @lends fabric.Pattern.prototype */ {
|
2013-02-03 01:12:38 +00:00
|
|
|
|
2013-02-06 15:20:45 +00:00
|
|
|
/**
|
|
|
|
|
* Repeat property of a pattern (one of repeat, repeat-x, repeat-y)
|
|
|
|
|
* @type String
|
|
|
|
|
*/
|
2013-02-03 01:12:38 +00:00
|
|
|
repeat: 'repeat',
|
|
|
|
|
|
2013-04-22 13:16:58 +00:00
|
|
|
/**
|
|
|
|
|
* Pattern horizontal offset from object's left/top corner
|
|
|
|
|
* @type Number
|
|
|
|
|
*/
|
|
|
|
|
offsetX: 0,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Pattern vertical offset from object's left/top corner
|
2013-04-22 13:26:57 +00:00
|
|
|
* @type Number
|
2013-04-22 13:16:58 +00:00
|
|
|
*/
|
|
|
|
|
offsetY: 0,
|
|
|
|
|
|
2013-02-03 01:12:38 +00:00
|
|
|
/**
|
|
|
|
|
* Constructor
|
|
|
|
|
* @param {Object} [options]
|
|
|
|
|
* @return {fabric.Pattern} thisArg
|
|
|
|
|
*/
|
|
|
|
|
initialize: function(options) {
|
|
|
|
|
options || (options = { });
|
|
|
|
|
|
|
|
|
|
if (options.source) {
|
2013-06-02 20:07:16 +00:00
|
|
|
if (typeof options.source === 'string') {
|
|
|
|
|
// function string
|
|
|
|
|
if (typeof fabric.util.getFunctionBody(options.source) !== 'undefined') {
|
|
|
|
|
this.source = new Function(fabric.util.getFunctionBody(options.source));
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// img src string
|
|
|
|
|
var _this = this;
|
|
|
|
|
this.source = fabric.util.createImage();
|
|
|
|
|
fabric.util.loadImage(options.source, function(img) {
|
|
|
|
|
_this.source = img;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// img element
|
|
|
|
|
this.source = options.source;
|
|
|
|
|
}
|
2013-02-03 01:12:38 +00:00
|
|
|
}
|
2013-02-04 16:30:36 +00:00
|
|
|
if (options.repeat) {
|
|
|
|
|
this.repeat = options.repeat;
|
|
|
|
|
}
|
2013-04-22 13:16:58 +00:00
|
|
|
if (options.offsetX) {
|
|
|
|
|
this.offsetX = options.offsetX;
|
|
|
|
|
}
|
|
|
|
|
if (options.offsetY) {
|
|
|
|
|
this.offsetY = options.offsetY;
|
|
|
|
|
}
|
2013-02-03 01:12:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
2013-02-06 15:20:45 +00:00
|
|
|
* Returns object representation of a pattern
|
2013-02-03 01:12:38 +00:00
|
|
|
* @return {Object}
|
|
|
|
|
*/
|
|
|
|
|
toObject: function() {
|
|
|
|
|
|
|
|
|
|
var source;
|
|
|
|
|
|
|
|
|
|
// callback
|
|
|
|
|
if (typeof this.source === 'function') {
|
2013-06-02 20:07:16 +00:00
|
|
|
source = String(this.source);
|
2013-02-03 01:12:38 +00:00
|
|
|
}
|
|
|
|
|
// <img> element
|
|
|
|
|
else if (typeof this.source.src === 'string') {
|
|
|
|
|
source = this.source.src;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
source: source,
|
2013-04-22 13:16:58 +00:00
|
|
|
repeat: this.repeat,
|
|
|
|
|
offsetX: this.offsetX,
|
|
|
|
|
offsetY: this.offsetY
|
2013-02-03 01:12:38 +00:00
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
2013-02-06 15:20:45 +00:00
|
|
|
* Returns an instance of CanvasPattern
|
2013-02-03 01:12:38 +00:00
|
|
|
* @param ctx
|
2013-02-06 15:20:45 +00:00
|
|
|
* @return {CanvasPattern}
|
2013-02-03 01:12:38 +00:00
|
|
|
*/
|
|
|
|
|
toLive: function(ctx) {
|
|
|
|
|
var source = typeof this.source === 'function' ? this.source() : this.source;
|
|
|
|
|
return ctx.createPattern(source, this.repeat);
|
|
|
|
|
}
|
2013-04-22 13:16:58 +00:00
|
|
|
});
|