mirror of
https://github.com/Hopiu/fabric.js.git
synced 2026-04-08 08:21:00 +00:00
Add rectangles optimization to Spray brush
This commit is contained in:
parent
4b78a2d819
commit
f5775eb2f4
1 changed files with 32 additions and 1 deletions
|
|
@ -37,7 +37,14 @@ fabric.SprayBrush = fabric.util.createClass( fabric.BaseBrush, /** @lends fabric
|
|||
* @type Boolean
|
||||
* @default
|
||||
*/
|
||||
randomOpacity: false,
|
||||
randomOpacity: false,
|
||||
|
||||
/**
|
||||
* Whether overlapping dots (rectangles) should be removed (for performance reasons)
|
||||
* @type Boolean
|
||||
* @default
|
||||
*/
|
||||
optimizeOverlapping: true,
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
|
|
@ -97,6 +104,11 @@ fabric.SprayBrush = fabric.util.createClass( fabric.BaseBrush, /** @lends fabric
|
|||
rects.push(rect);
|
||||
}
|
||||
}
|
||||
|
||||
if (this.optimizeOverlapping) {
|
||||
rects = this._getOptimizedRects(rects);
|
||||
}
|
||||
|
||||
var group = new fabric.Group(rects);
|
||||
this.canvas.add(group);
|
||||
this.canvas.fire('path:created', { path: group });
|
||||
|
|
@ -107,6 +119,25 @@ fabric.SprayBrush = fabric.util.createClass( fabric.BaseBrush, /** @lends fabric
|
|||
this.canvas.renderAll();
|
||||
},
|
||||
|
||||
_getOptimizedRects: function(rects) {
|
||||
|
||||
// avoid creating duplicate rects at the same coordinates
|
||||
var uniqueRects = { }, key;
|
||||
|
||||
for (var i = 0, len = rects.length; i < len; i++) {
|
||||
key = rects[i].left + '' + rects[i].top;
|
||||
if (!uniqueRects[key]) {
|
||||
uniqueRects[key] = rects[i];
|
||||
}
|
||||
}
|
||||
var uniqueRectsArray = [ ];
|
||||
for (key in uniqueRects) {
|
||||
uniqueRectsArray.push(uniqueRects[key]);
|
||||
}
|
||||
|
||||
return uniqueRectsArray;
|
||||
},
|
||||
|
||||
/**
|
||||
* Renders brush
|
||||
*/
|
||||
|
|
|
|||
Loading…
Reference in a new issue