Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(V6): brush layering POC #7880

Closed
wants to merge 86 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
1a191a7
apply 7744 patch (no binaries)
ShaMan123 Apr 3, 2022
26e8392
apply 7744 patch (binaries)
ShaMan123 Apr 3, 2022
7d793b3
commit #7744 review
ShaMan123 Apr 3, 2022
2708029
patch2
ShaMan123 Apr 4, 2022
02a8ffa
Update spray_brush.class.js
ShaMan123 Apr 4, 2022
0d3690f
enable text editing under group
ShaMan123 Apr 4, 2022
ff0346a
migrate back to isDescendantOf
ShaMan123 Apr 4, 2022
812f02a
render bg
ShaMan123 Apr 4, 2022
053946c
fix(): clear canvas from entire object tree
ShaMan123 Apr 4, 2022
db76c44
fix(): center object in case object is nested
ShaMan123 Apr 4, 2022
04aad5b
Update eraser_brush.mixin.js
ShaMan123 Apr 4, 2022
8c1c535
Update canvas.class.js
ShaMan123 Apr 4, 2022
3fd4af2
Update canvas_events.mixin.js
ShaMan123 Apr 4, 2022
58e9be2
lint
ShaMan123 Apr 4, 2022
c2ef2f5
safeguard from adding group to itself
ShaMan123 Apr 4, 2022
f0145b5
Update object_ancestry.mixin.js
ShaMan123 Apr 4, 2022
cb20b81
patch tests from v6!
ShaMan123 Apr 4, 2022
347f80d
Update canvas_grouping.mixin.js
ShaMan123 Apr 4, 2022
a56d55e
Update canvas.class.js
ShaMan123 Apr 4, 2022
22045b6
create layer
ShaMan123 Apr 4, 2022
724792a
Update layer.class.js
ShaMan123 Apr 4, 2022
7163ad3
warn
ShaMan123 Apr 4, 2022
2a426e7
Update group_layout.js
ShaMan123 Apr 4, 2022
5e3fc1f
Update group_layout.js
ShaMan123 Apr 4, 2022
2b1f680
remove redundant logic
ShaMan123 Apr 4, 2022
1ad5cc8
Update canvas_grouping.mixin.js
ShaMan123 Apr 4, 2022
11c828f
fix(): calling sendPointToPlane
ShaMan123 Apr 4, 2022
b6fa3dc
Update group.class.js
ShaMan123 Apr 4, 2022
e9afc89
refactor `enterGroup`
ShaMan123 Apr 4, 2022
d8f8251
invalidate groups after active selection change
ShaMan123 Apr 4, 2022
c58a274
_shouldSetNestedCoords
ShaMan123 Apr 4, 2022
c0f5571
perf(): refrain from `contains`
ShaMan123 Apr 4, 2022
9a223d0
Update canvas_events.mixin.js
ShaMan123 Apr 4, 2022
2d5cc75
Update canvas.class.js
ShaMan123 Apr 4, 2022
01c4a87
Update canvas.class.js
ShaMan123 Apr 4, 2022
2164161
fix(): export svg backgroundColor
ShaMan123 Apr 4, 2022
a5575bd
Update group.js
ShaMan123 Apr 4, 2022
5c56124
backport collection fix to test
ShaMan123 Apr 4, 2022
5e3aaf5
Update group.class.js
ShaMan123 Apr 4, 2022
d53d6b6
lint
ShaMan123 Apr 4, 2022
9d1179d
Merge branch 'v6-group-patch2' into v6-layer
ShaMan123 Apr 4, 2022
ab776a3
Update group.class.js
ShaMan123 Apr 4, 2022
8e907bb
fix(): target prop on canvas added/removed event
ShaMan123 Apr 5, 2022
f082fc9
fix(): modify handlers strictly on canvas events
ShaMan123 Apr 5, 2022
468a815
Merge branch 'master' into v6-layer
ShaMan123 Apr 5, 2022
c2d93db
Merge branch 'master' into v6-group-patch2
ShaMan123 Apr 5, 2022
bb822ce
Merge branch 'v6-group-patch2' into v6-layer
ShaMan123 Apr 5, 2022
c16bdfb
feat(): initial support for layers under groups
ShaMan123 Apr 5, 2022
f3aa02e
lint
ShaMan123 Apr 5, 2022
c3f69b7
visual test
ShaMan123 Apr 5, 2022
bca1515
Update layer.class.js
ShaMan123 Apr 5, 2022
80b384d
Update layer.class.js
ShaMan123 Apr 5, 2022
548d65c
JSDOC
ShaMan123 Apr 5, 2022
656c36a
Create layer.js
ShaMan123 Apr 5, 2022
70f80af
Update layer.js
ShaMan123 Apr 5, 2022
4641e37
fix(): getObjectsBBox when layer/group are empty
ShaMan123 Apr 5, 2022
5728ba6
Update group.class.js
ShaMan123 Apr 6, 2022
25c8340
Update layer.js
ShaMan123 Apr 6, 2022
3d8b2e3
Update layer.class.js
ShaMan123 Apr 7, 2022
92823b0
perf(): reduce iteration
ShaMan123 Apr 8, 2022
46ef26c
Update layer.class.js
ShaMan123 Apr 9, 2022
e9cbaa0
fix(): apply transformations only on objects
ShaMan123 Apr 9, 2022
590ba58
Update group_layout.js
ShaMan123 Apr 9, 2022
329db01
replace golden
ShaMan123 Apr 9, 2022
ece7698
Create layer.png
ShaMan123 Apr 9, 2022
cdce1d1
fix setting `initialTransform`
ShaMan123 Apr 9, 2022
5a38d5e
fix(): fire `resize`/`window:resize`
ShaMan123 Apr 11, 2022
98afe42
POC: use layer to render brush
ShaMan123 Apr 17, 2022
37cc9bf
Update static_canvas.class.js
ShaMan123 Apr 17, 2022
56e17f2
Update pencil_brush.class.js
ShaMan123 Apr 17, 2022
dffe340
fix(): safegurad from objects without bbox
ShaMan123 Apr 23, 2022
dc6d32b
merge conflict?
asturur Apr 30, 2022
6f28f7f
remove whitespace
asturur Apr 30, 2022
53027cb
remove whitespace
asturur Apr 30, 2022
f4c8c5b
Update group.class.js
ShaMan123 May 1, 2022
da115b5
remove dead code
ShaMan123 May 1, 2022
db7c025
Merge branch 'v6-layer-tests' into v6-layer
ShaMan123 May 1, 2022
5489cdc
revert
ShaMan123 May 1, 2022
5ff32e5
Update object_geometry.mixin.js
ShaMan123 May 1, 2022
4c0c142
fix(): pass options to `_getTransformedDimensions`
ShaMan123 May 1, 2022
23416d1
Revert "fix(): pass options to `_getTransformedDimensions`"
ShaMan123 May 1, 2022
2f27480
fix(): getDimensions no `strokeWidth`
ShaMan123 May 1, 2022
2a26286
fix(): `_getTransformedDimensions`
ShaMan123 May 1, 2022
59e2532
Merge branch 'v6-layer' into v6-brush-layering
ShaMan123 May 1, 2022
c2fcd8c
Merge branch 'master' into v6-brush-layering
ShaMan123 May 2, 2022
0c7162c
Merge branch 'master' into v6-brush-layering
ShaMan123 Jun 13, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ var filesToInclude = [
'src/shapes/polygon.class.js',
'src/shapes/path.class.js',
'src/shapes/group.class.js',
'src/shapes/layer.class.js',
ifSpecifiedInclude('interaction', 'src/shapes/active_selection.class.js'),
'src/shapes/image.class.js',

Expand Down
33 changes: 31 additions & 2 deletions src/brushes/base_brush.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,39 @@ fabric.BaseBrush = fabric.util.createClass(/** @lends fabric.BaseBrush.prototype

limitedToCanvasSize: false,

initialize: function (layer) {
this.layer = layer;
this.canvas = this.layer.canvas;
var ctx = this.getContext();
ctx.save();
ctx.translate(-this.layer.width / 2, -this.layer.height / 2);
},

_isMainEvent: function (e) {
return this.canvas._isMainEvent(e);
},

getContext: function () {
if (!this.layer._cacheCanvas || !this.layer._cacheContext) {
this.layer._createCacheCanvas();
}
return this.layer._cacheContext;
},

resetContext: function () {
var canvas = this.layer._cacheCanvas, ctx = this.layer._cacheContext;
//console.log('reset')
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.clearRect(0, 0, canvas.width, canvas.height);
},

/**
* Sets brush styles
* @private
* @param {CanvasRenderingContext2D} ctx
*/
_setBrushStyles: function (ctx) {
_setBrushStyles: function () {
ctx = this.getContext();
ctx.strokeStyle = this.color;
ctx.lineWidth = this.width;
ctx.lineCap = this.strokeLineCap;
Expand All @@ -86,8 +112,11 @@ fabric.BaseBrush = fabric.util.createClass(/** @lends fabric.BaseBrush.prototype
*/
_saveAndTransform: function(ctx) {
var v = this.canvas.viewportTransform;

this.layer._updateCacheCanvas();
ctx.save();
ctx.transform(v[0], v[1], v[2], v[3], v[4], v[5]);
//ctx.translate(-this.layer.width / 2, -this.layer.height / 2);
// ctx.transform(v[0], v[1], v[2], v[3], v[4], v[5]);
},

/**
Expand Down
27 changes: 15 additions & 12 deletions src/brushes/pencil_brush.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
* @param {fabric.Canvas} canvas
* @return {fabric.PencilBrush} Instance of a pencil brush
*/
initialize: function(canvas) {
this.canvas = canvas;
initialize: function(layer) {
this.callSuper('initialize', layer);
this._points = [];
},

Expand All @@ -58,7 +58,7 @@
* @param {Object} pointer
*/
onMouseDown: function(pointer, options) {
if (!this.canvas._isMainEvent(options.e)) {
if (!this._isMainEvent(options.e)) {
return;
}
this.drawStraightLine = options.e[this.straightLineKey];
Expand All @@ -74,7 +74,7 @@
* @param {Object} pointer
*/
onMouseMove: function(pointer, options) {
if (!this.canvas._isMainEvent(options.e)) {
if (!this._isMainEvent(options.e)) {
return;
}
this.drawStraightLine = options.e[this.straightLineKey];
Expand All @@ -85,11 +85,11 @@
if (this.needsFullRender()) {
// redraw curve
// clear top canvas
this.canvas.clearContext(this.canvas.contextTop);
this.getContext().reset();
this._render();
}
else {
var points = this._points, length = points.length, ctx = this.canvas.contextTop;
var points = this._points, length = points.length, ctx = this.getContext();
// draw the curve update
this._saveAndTransform(ctx);
if (this.oldEnd) {
Expand All @@ -98,6 +98,8 @@
}
this.oldEnd = this._drawSegment(ctx, points[length - 2], points[length - 1], true);
ctx.stroke();
this.layer._drawClipPath(this.getContext(), this.layer.clipPath);
this.layer.drawCacheOnCanvas(this.canvas.contextTop);
ctx.restore();
}
}
Expand All @@ -107,12 +109,13 @@
* Invoked on mouse up
*/
onMouseUp: function(options) {
if (!this.canvas._isMainEvent(options.e)) {
if (!this._isMainEvent(options.e)) {
return true;
}
this.drawStraightLine = false;
this.oldEnd = undefined;
this._finalizeAndAddPath();
this.resetContext();
return false;
},

Expand All @@ -126,7 +129,7 @@

this._reset();
this._addPoint(p);
this.canvas.contextTop.moveTo(p.x, p.y);
this.getContext().moveTo(p.x, p.y);
},

/**
Expand All @@ -151,7 +154,7 @@
*/
_reset: function() {
this._points = [];
this._setBrushStyles(this.canvas.contextTop);
this._setBrushStyles(this.getContext());
this._setShadow();
this._hasStraightLine = false;
},
Expand All @@ -174,7 +177,7 @@
var i, len,
p1 = this._points[0],
p2 = this._points[1];
ctx = ctx || this.canvas.contextTop;
ctx = ctx || this.getContext();
this._saveAndTransform(ctx);
ctx.beginPath();
//if we only have 2 points in the path and they are the same
Expand Down Expand Up @@ -279,7 +282,7 @@
* and add it to the fabric canvas.
*/
_finalizeAndAddPath: function() {
var ctx = this.canvas.contextTop;
var ctx = this.getContext();
ctx.closePath();
if (this.decimate) {
this._points = this.decimatePoints(this._points, this.decimate);
Expand All @@ -295,7 +298,7 @@
}

var path = this.createPath(pathData);
this.canvas.clearContext(this.canvas.contextTop);
this.getContext().reset();
this.canvas.fire('before:path:created', { path: path });
this.canvas.add(path);
this.canvas.requestRenderAll();
Expand Down
6 changes: 5 additions & 1 deletion src/canvas.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,11 @@

this._initRetinaScaling();

this.freeDrawingBrush = fabric.PencilBrush && new fabric.PencilBrush(this);
if (fabric.PencilBrush) {
var drawingLayer = new fabric.Layer([], { canvas: this, objectCaching: true });
this.add(drawingLayer);
this.freeDrawingBrush = new fabric.PencilBrush(drawingLayer);
}

this.calcOffset();
},
Expand Down
3 changes: 2 additions & 1 deletion src/mixins/canvas_events.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,9 @@
/**
* @private
*/
_onResize: function () {
_onResize: function (e) {
this.calcOffset();
this.fire('window:resize', { e: e });
},

/**
Expand Down
2 changes: 2 additions & 0 deletions src/mixins/object_geometry.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,8 @@
* @param {Number} [options.scaleY]
* @param {Number} [options.skewX]
* @param {Number} [options.skewY]
* @param {Number} [options.width]
* @param {Number} [options.height]
* @private
* @returns {fabric.Point} dimensions
*/
Expand Down
Loading