fabric.js/src/pattern.class.js

133 lines
3.2 KiB
JavaScript
Raw Normal View History

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
*/
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',
/**
* 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
*/
offsetY: 0,
2013-02-03 01:12:38 +00:00
/**
* Constructor
* @param {Object} [options]
* @return {fabric.Pattern} thisArg
*/
initialize: function(options) {
options || (options = { });
this.id = fabric.Object.__uid++;
2013-02-03 01:12:38 +00:00
if (options.source) {
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
}
if (options.repeat) {
this.repeat = options.repeat;
}
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') {
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,
repeat: this.repeat,
offsetX: this.offsetX,
offsetY: this.offsetY
2013-02-03 01:12:38 +00:00
};
},
/* _TO_SVG_START_ */
/**
* Returns SVG representation of a pattern
* @return {String} SVG representation of a pattern
*/
toSVG: function() {
var patternSource = typeof this.source === 'function' ? this.source() : this.source;
var patternWidth = patternSource.width;
var patternHeight = patternSource.height;
var patternImgSrc = '';
if (patternSource.src) {
patternImgSrc = patternSource.src;
}
else if (patternSource.toDataURL) {
patternImgSrc = patternSource.toDataURL();
}
return '<pattern id="SVGID_' + this.id +
'" x="' + this.offsetX +
'" y="' + this.offsetY +
'" width="' + patternWidth +
'" height="' + patternHeight + '">' +
'<image x="0" y="0"' +
' width="' + patternWidth +
'" height="' + patternHeight +
'" src="' + patternImgSrc +
'"></image>' +
'</pattern>';
},
/* _TO_SVG_END_ */
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);
}
});