fabric.js/src/pattern.class.js

69 lines
1.5 KiB
JavaScript
Raw Normal View History

2013-02-03 01:12:38 +00:00
/**
* Pattern class
* @class Pattern
* @memberOf fabric
*/
fabric.Pattern = fabric.util.createClass(/** @scope fabric.Pattern.prototype */ {
2013-02-06 15:20:45 +00:00
/**
* Repeat property of a pattern (one of repeat, repeat-x, repeat-y)
* @property
* @type String
*/
2013-02-03 01:12:38 +00:00
repeat: 'repeat',
/**
* Constructor
* @method initialize
* @param {Object} [options]
* @return {fabric.Pattern} thisArg
*/
initialize: function(options) {
options || (options = { });
if (options.source) {
this.source = typeof options.source === 'string'
? new Function(options.source)
: options.source;
}
if (options.repeat) {
this.repeat = options.repeat;
}
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
* @method toObject
* @return {Object}
*/
toObject: function() {
var source;
// callback
if (typeof this.source === 'function') {
source = String(this.source)
.match(/function\s+\w*\s*\(.*\)\s+\{([\s\S]*)\}/)[1];
}
// <img> element
else if (typeof this.source.src === 'string') {
source = this.source.src;
}
return {
source: source,
repeat: this.repeat
};
},
/**
2013-02-06 15:20:45 +00:00
* Returns an instance of CanvasPattern
2013-02-03 01:12:38 +00:00
* @method toLive
* @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);
}
});