fabric.js/src/pattern.class.js

137 lines
3.5 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 or no-repeat)
2013-02-06 15:20:45 +00:00
* @type String
* @default
2013-02-06 15:20:45 +00:00
*/
2013-02-03 01:12:38 +00:00
repeat: 'repeat',
/**
* Pattern horizontal offset from object's left/top corner
* @type Number
* @default
*/
offsetX: 0,
/**
* Pattern vertical offset from object's left/top corner
2013-04-22 13:26:57 +00:00
* @type Number
* @default
*/
offsetY: 0,
2013-02-03 01:12:38 +00:00
/**
* Constructor
2013-08-14 10:54:53 +00:00
* @param {Object} [options] Options object
2013-02-03 01:12:38 +00:00
* @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
* @return {Object} Object representation of a pattern instance
2013-02-03 01:12:38 +00:00
*/
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
* @param {fabric.Object} object
* @return {String} SVG representation of a pattern
*/
2013-07-22 20:00:03 +00:00
toSVG: function(object) {
var patternSource = typeof this.source === 'function' ? this.source() : this.source;
2013-07-22 20:00:03 +00:00
var patternWidth = patternSource.width / object.getWidth();
var patternHeight = patternSource.height / object.getHeight();
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"' +
2013-07-25 14:45:15 +00:00
' width="' + patternSource.width +
'" height="' + patternSource.height +
2013-07-22 20:00:03 +00:00
'" xlink:href="' + 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
* @param {CanvasRenderingContext2D} ctx Context to create pattern
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);
}
});