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
|
|
|
/**
|
2013-09-17 20:42:58 +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
|
2013-08-08 16:31:26 +00:00
|
|
|
* @default
|
2013-02-06 15:20:45 +00:00
|
|
|
*/
|
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
|
2013-08-08 16:31:26 +00:00
|
|
|
* @default
|
2013-04-22 13:16:58 +00:00
|
|
|
*/
|
|
|
|
|
offsetX: 0,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Pattern vertical offset from object's left/top corner
|
2013-04-22 13:26:57 +00:00
|
|
|
* @type Number
|
2013-08-08 16:31:26 +00:00
|
|
|
* @default
|
2013-04-22 13:16:58 +00:00
|
|
|
*/
|
|
|
|
|
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 = { });
|
|
|
|
|
|
2013-07-21 10:33:13 +00:00
|
|
|
this.id = fabric.Object.__uid++;
|
|
|
|
|
|
2013-02-03 01:12:38 +00:00
|
|
|
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-08-08 16:31:26 +00:00
|
|
|
* @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') {
|
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-07-21 10:33:13 +00:00
|
|
|
/* _TO_SVG_START_ */
|
|
|
|
|
/**
|
|
|
|
|
* Returns SVG representation of a pattern
|
2013-09-17 20:42:58 +00:00
|
|
|
* @param {fabric.Object} object
|
2013-07-21 10:33:13 +00:00
|
|
|
* @return {String} SVG representation of a pattern
|
|
|
|
|
*/
|
2013-07-22 20:00:03 +00:00
|
|
|
toSVG: function(object) {
|
2013-07-21 10:33:13 +00:00
|
|
|
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();
|
2013-07-21 10:33:13 +00:00
|
|
|
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 +
|
2013-07-21 10:33:13 +00:00
|
|
|
'"></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-08-08 16:31:26 +00:00
|
|
|
* @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);
|
|
|
|
|
}
|
2013-04-22 13:16:58 +00:00
|
|
|
});
|