mirror of
https://github.com/Hopiu/fabric.js.git
synced 2026-04-08 00:10:59 +00:00
55 lines
1.4 KiB
JavaScript
55 lines
1.4 KiB
JavaScript
/**
|
|
* PatternBrush class
|
|
* @class fabric.PatternBrush
|
|
* @extends fabric.BaseBrush
|
|
*/
|
|
fabric.PatternBrush = fabric.util.createClass(fabric.PencilBrush, /** @lends fabric.PatternBrush.prototype */ {
|
|
|
|
getPatternSrc: function() {
|
|
|
|
var dotWidth = 20,
|
|
dotDistance = 5,
|
|
patternCanvas = fabric.document.createElement('canvas'),
|
|
patternCtx = patternCanvas.getContext('2d');
|
|
|
|
patternCanvas.width = patternCanvas.height = dotWidth + dotDistance;
|
|
|
|
patternCtx.fillStyle = this.color;
|
|
patternCtx.beginPath();
|
|
patternCtx.arc(dotWidth / 2, dotWidth / 2, dotWidth / 2, 0, Math.PI * 2, false);
|
|
patternCtx.closePath();
|
|
patternCtx.fill();
|
|
|
|
return patternCanvas;
|
|
},
|
|
|
|
getPatternSrcFunction: function() {
|
|
return String(this.getPatternSrc).replace('this.color', '"' + this.color + '"');
|
|
},
|
|
|
|
/**
|
|
* Creates "pattern" instance property
|
|
*/
|
|
getPattern: function() {
|
|
return this.canvas.contextTop.createPattern(this.source || this.getPatternSrc(), 'repeat');
|
|
},
|
|
|
|
/**
|
|
* Sets brush styles
|
|
*/
|
|
_setBrushStyles: function() {
|
|
this.callSuper('_setBrushStyles');
|
|
this.canvas.contextTop.strokeStyle = this.getPattern();
|
|
},
|
|
|
|
/**
|
|
* Creates path
|
|
*/
|
|
createPath: function(pathData) {
|
|
var path = this.callSuper('createPath', pathData);
|
|
path.stroke = new fabric.Pattern({
|
|
source: this.source || this.getPatternSrcFunction()
|
|
});
|
|
return path;
|
|
}
|
|
});
|