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(Brush API): Observable, clipPath #8476

Closed
wants to merge 188 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
188 commits
Select commit Hold shift + click to select a range
653946c
svg
ShaMan123 Oct 31, 2022
b371499
SHARED_ATTRIBUTES
ShaMan123 Oct 31, 2022
a71cfc2
migrate
ShaMan123 Oct 31, 2022
e169232
Update circle.class.ts
ShaMan123 Oct 31, 2022
b0d0379
Update circle.class.ts
ShaMan123 Oct 31, 2022
4e7e498
defaults
ShaMan123 Oct 31, 2022
403d791
static
ShaMan123 Oct 31, 2022
575af88
Update CHANGELOG.md
ShaMan123 Oct 31, 2022
8f64f77
init
ShaMan123 Oct 31, 2022
ec4ac19
more
ShaMan123 Oct 31, 2022
1c21ea1
m
ShaMan123 Nov 1, 2022
4ba47fa
poly
ShaMan123 Nov 1, 2022
c66798c
Update polyline.class.ts
ShaMan123 Nov 1, 2022
63442ff
more
ShaMan123 Nov 1, 2022
fd2afa6
unpleasant
ShaMan123 Nov 1, 2022
4b6cc54
imports
ShaMan123 Nov 1, 2022
9acdd8d
Merge branch 'master' into ts-poly
ShaMan123 Nov 1, 2022
535fee0
Update CHANGELOG.md
ShaMan123 Nov 1, 2022
de8b311
more
ShaMan123 Nov 1, 2022
66a14cc
revert Point
ShaMan123 Nov 1, 2022
65cfcd6
IPoint
ShaMan123 Nov 1, 2022
5681fce
refactor `fromElementGenerator` => `polyFromElement`
ShaMan123 Nov 1, 2022
1d18a15
Update polyline.class.ts
ShaMan123 Nov 1, 2022
ce8660d
Update polyline.class.ts
ShaMan123 Nov 1, 2022
f6eb764
remove default points
ShaMan123 Nov 1, 2022
b5b5126
abstract methods + tidyup
ShaMan123 Nov 7, 2022
c536ecb
Update base_brush.class.ts
ShaMan123 Nov 7, 2022
faf374e
Update CHANGELOG.md
ShaMan123 Nov 7, 2022
2558035
Update base_brush.class.ts
ShaMan123 Nov 7, 2022
7576a6a
feat(): PolyBrush
ShaMan123 Nov 7, 2022
6826935
Update PolyBrush.ts
ShaMan123 Nov 7, 2022
ef485d5
Update PolyBrush.ts
ShaMan123 Nov 7, 2022
63762e8
Merge branch 'master' into ts-poly
ShaMan123 Nov 7, 2022
8f9d9d6
Merge branch 'ts-poly' into poly-brush2
ShaMan123 Nov 7, 2022
5e1f1fd
feat(): ShapeBrush
ShaMan123 Nov 7, 2022
a195dc5
shape brush
ShaMan123 Nov 7, 2022
11790a3
Merge branch 'brushes-patch' into poly-brush2
ShaMan123 Nov 7, 2022
653ddbb
Update base_brush.class.ts
ShaMan123 Nov 7, 2022
be2e508
Update ShapeBrush.ts
ShaMan123 Nov 7, 2022
3e8d0f6
Update ellipse.class.ts
ShaMan123 Nov 7, 2022
b8f308e
Update ShapeBrush.ts
ShaMan123 Nov 7, 2022
2c78623
circular shape brush
ShaMan123 Nov 7, 2022
5073ee7
symmetric
ShaMan123 Nov 7, 2022
f3dece5
refactor
ShaMan123 Nov 7, 2022
4580f41
Update ShapeBaseBrush.ts
ShaMan123 Nov 7, 2022
6ae1ea3
rename
ShaMan123 Nov 7, 2022
942cf90
Update DrawShapeBase.ts
ShaMan123 Nov 7, 2022
3bd0685
Update DrawShapeBase.ts
ShaMan123 Nov 7, 2022
62541fd
Update DrawShapeBase.ts
ShaMan123 Nov 7, 2022
a936296
Merge branch 'master' into poly-brush2
ShaMan123 Nov 21, 2022
4c28bbe
fix use `setBoundingBox`
ShaMan123 Nov 22, 2022
31cae7f
Create draw_shapes.js
ShaMan123 Nov 22, 2022
39bb29f
extensive tests!
ShaMan123 Nov 22, 2022
1e1db9a
expose super class method `transform`
ShaMan123 Nov 23, 2022
228b0a7
BEAKING: brush `_render`, `render`
ShaMan123 Nov 23, 2022
b543910
`finalize`, `finalizeShape`
ShaMan123 Nov 23, 2022
6062754
shadow
ShaMan123 Nov 23, 2022
f73790a
move shadow setter to super
ShaMan123 Nov 23, 2022
cb6dfe1
Update path.class.ts
ShaMan123 Nov 23, 2022
01b47df
adapt eraser - not tested
ShaMan123 Nov 23, 2022
1342a14
Squashed commit of the following:
ShaMan123 Nov 23, 2022
51daca3
more
ShaMan123 Nov 23, 2022
3e6e425
Update base_brush.class.ts
ShaMan123 Nov 23, 2022
70c9e0f
Merge branch 'master' into poly-brush2
ShaMan123 Nov 27, 2022
4ab566d
Update CHANGELOG.md
ShaMan123 Nov 27, 2022
03e4150
imports
ShaMan123 Nov 27, 2022
6d182e0
fix merge artifact
ShaMan123 Nov 27, 2022
c267640
Update DrawShapeBase.ts
ShaMan123 Nov 27, 2022
211bfe7
imports/types
ShaMan123 Nov 27, 2022
a90b2c7
Merge branch 'master' into clip-brush
ShaMan123 Nov 28, 2022
aaa9981
fix merge
ShaMan123 Nov 28, 2022
92e8d4a
Merge branch 'poly-brush2' into clip-brush
ShaMan123 Nov 28, 2022
8222e6a
BREAKING: stop adding brush result to canvas
ShaMan123 Nov 28, 2022
15a49ce
more
ShaMan123 Nov 28, 2022
5ace90d
fix clearing
ShaMan123 Nov 28, 2022
df0684c
Update freedraw.js
ShaMan123 Nov 28, 2022
9a5b74d
better triggering
ShaMan123 Nov 28, 2022
0f488bd
fix(): clear context top on finalize
ShaMan123 Nov 28, 2022
2cfa2d1
refactor tests
ShaMan123 Nov 28, 2022
ae7092a
Update CHANGELOG.md
ShaMan123 Nov 28, 2022
9bdd622
Update CHANGELOG.md
ShaMan123 Nov 28, 2022
f4b380c
async event fix
ShaMan123 Nov 28, 2022
d400cf3
handle `_isCurrentlyDrawing` by brush
ShaMan123 Nov 28, 2022
bdd4491
Update base_brush.class.ts
ShaMan123 Nov 28, 2022
0d9a685
Merge branch 'master' into poly-brush2
ShaMan123 Nov 29, 2022
cabe315
Merge branch 'poly-brush2' into clip-brush
ShaMan123 Nov 29, 2022
2e9d441
Merge branch 'master' into poly-brush2
ShaMan123 Dec 4, 2022
0617410
Merge branch 'poly-brush2' into clip-brush
ShaMan123 Dec 4, 2022
f23de4f
Merge branch 'master' into poly-brush2
ShaMan123 Dec 6, 2022
70fb2cd
Merge branch 'poly-brush2' into clip-brush
ShaMan123 Dec 6, 2022
7d5c2ac
tests
ShaMan123 Dec 7, 2022
2a4e583
Update freedraw.js
ShaMan123 Dec 7, 2022
ca5aeb3
Update CHANGELOG.md
ShaMan123 Dec 7, 2022
c1f7dfa
Update canvas.class.ts
ShaMan123 Dec 7, 2022
1e41df3
Merge branch 'master' into poly-brush2
ShaMan123 Dec 9, 2022
f78ee70
Merge branch 'poly-brush2' into clip-brush
ShaMan123 Dec 9, 2022
4db36ca
fix clip path
ShaMan123 Dec 10, 2022
7802c81
restore result test
ShaMan123 Dec 11, 2022
34cd396
rename goldens (mesh => result)
ShaMan123 Dec 11, 2022
9dd25ea
fix(tests): `pointDrawer` add result on mouse up
ShaMan123 Dec 11, 2022
73c085a
Update freedraw.js
ShaMan123 Dec 11, 2022
b6df029
Merge branch 'master' into clip-brush
ShaMan123 Dec 11, 2022
a2403c0
Merge branch 'master' into poly-brush2
ShaMan123 Dec 11, 2022
afaa677
Merge branch 'poly-brush2' into clip-brush
ShaMan123 Dec 11, 2022
4d493ff
types
ShaMan123 Dec 11, 2022
3e74126
refactor `finalize`
ShaMan123 Dec 11, 2022
cc925d5
BREAKING: `convertPointsToSVGPath` => `getPathFromPoints`
ShaMan123 Dec 11, 2022
bff99b2
cleanup pencil `finalize`
ShaMan123 Dec 11, 2022
3ba1ab6
Update freedraw.js
ShaMan123 Dec 11, 2022
37fb30b
Update freedraw.js
ShaMan123 Dec 11, 2022
040f727
chrome goldens
ShaMan123 Dec 11, 2022
a3bb345
Merge branch 'master' into poly-brush2
ShaMan123 Dec 12, 2022
6318b2f
fix: remove unwanted restore
ShaMan123 Dec 13, 2022
db7014f
Merge branch 'poly-brush2' into clip-brush
ShaMan123 Dec 13, 2022
db3ce69
pencil cleanup
ShaMan123 Dec 13, 2022
1141a1a
rename
ShaMan123 Dec 13, 2022
f49bd78
Merge branch 'master' into poly-brush2
ShaMan123 Dec 25, 2022
cafd0b3
fix(): imports after updating from master
ShaMan123 Dec 25, 2022
7e2fca2
Merge branch 'poly-brush2' into clip-brush
ShaMan123 Dec 25, 2022
59e81e9
reapply canvas class diff
ShaMan123 Dec 25, 2022
96504f9
fix imports
ShaMan123 Dec 25, 2022
b410f65
canvas type
ShaMan123 Dec 25, 2022
e6dd5f2
fix TS
ShaMan123 Dec 25, 2022
0c47f6a
Merge branch 'master' into poly-brush2
ShaMan123 Dec 29, 2022
f53a921
fix merge conflict
ShaMan123 Dec 29, 2022
bae043b
Update CHANGELOG.md
ShaMan123 Dec 29, 2022
c6f5896
Update base_brush.class.ts
ShaMan123 Dec 29, 2022
ea3c88b
Update DrawShapeBase.ts
ShaMan123 Dec 29, 2022
62e94e5
Update canvas.class.ts
ShaMan123 Dec 29, 2022
265d942
Merge branch 'poly-brush2' into clip-brush
ShaMan123 Dec 29, 2022
b5f4461
fix merge
ShaMan123 Dec 29, 2022
a462280
Update CHANGELOG.md
ShaMan123 Dec 29, 2022
d59f41d
better types
ShaMan123 Dec 30, 2022
6464146
Merge branch 'poly-brush2' into clip-brush
ShaMan123 Dec 30, 2022
94c4a9e
forgotten
ShaMan123 Dec 30, 2022
09a506f
Merge branch 'poly-brush2' into clip-brush
ShaMan123 Dec 30, 2022
d9e18ae
imports
ShaMan123 Dec 30, 2022
3b0f892
`_isCurrentlyDrawing` => `isCurrentlyDrawing()`
ShaMan123 Dec 30, 2022
3cdbbb2
cleanup
ShaMan123 Dec 30, 2022
81a92fe
Merge branch 'master' into poly-brush2
ShaMan123 Jan 6, 2023
9093607
export
ShaMan123 Jan 6, 2023
e5dc0d2
cleanup
ShaMan123 Jan 6, 2023
4e0f2ab
Merge branch 'poly-brush2' into clip-brush
ShaMan123 Jan 6, 2023
e7eab9d
fix merge error
ShaMan123 Jan 6, 2023
835ef5c
fix mrege error
ShaMan123 Jan 6, 2023
70b03a4
fix merge error
ShaMan123 Jan 6, 2023
b750eee
another annoying merge error
ShaMan123 Jan 6, 2023
63af835
Merge branch 'master' into poly-brush2
ShaMan123 Feb 4, 2023
d790f4b
Update CHANGELOG.md
ShaMan123 Feb 4, 2023
b07c857
Merge branch 'poly-brush2' into clip-brush
ShaMan123 Feb 4, 2023
fa0d063
fix merge error
ShaMan123 Feb 4, 2023
5f88571
Update Object.ts
ShaMan123 Feb 4, 2023
d0ee643
Update freedraw.js
ShaMan123 Feb 4, 2023
8c0a447
fix imports, merge error
ShaMan123 Feb 4, 2023
4395773
Merge branch 'poly-brush2' into clip-brush
ShaMan123 Feb 4, 2023
a430e2e
Update freedraw.js
ShaMan123 Feb 4, 2023
393de14
Update BaseBrush.ts
ShaMan123 Feb 4, 2023
e23657f
Update SelectableCanvas.ts
ShaMan123 Feb 4, 2023
a0aa51a
Squashed commit of the following:
ShaMan123 Feb 4, 2023
f1c14ca
subscribe to resize to set brush styles
ShaMan123 Feb 4, 2023
5129884
Update canvas_events.js
ShaMan123 Feb 4, 2023
fed5077
Update canvas_events.js
ShaMan123 Feb 4, 2023
41b5046
fix(): resize event
ShaMan123 Feb 4, 2023
ba5fc89
Update CHANGELOG.md
ShaMan123 Feb 4, 2023
3549c5c
comment
ShaMan123 Feb 4, 2023
a4af261
clipping methods
ShaMan123 Feb 4, 2023
aeb0f2c
Update BaseBrush.ts
ShaMan123 Feb 4, 2023
ae569e9
fix(PencilBrush): limitedToCanvasSize
ShaMan123 Feb 4, 2023
cace8d6
Revert "fix(PencilBrush): limitedToCanvasSize"
ShaMan123 Feb 4, 2023
52aeeaf
Update DrawShape.ts
ShaMan123 Feb 4, 2023
d3a6125
future proof: extractPointer
ShaMan123 Feb 4, 2023
22662a3
remove down redundancy
ShaMan123 Feb 4, 2023
ac75b89
cleanup pencil
ShaMan123 Feb 4, 2023
bc0e900
setViewportTransform + drawing
ShaMan123 Feb 4, 2023
5a1f381
Update brushes.js
ShaMan123 Feb 4, 2023
60147f2
Update PatternBrush.ts
ShaMan123 Feb 4, 2023
9d3b47b
setVpt + brush.clipPath
ShaMan123 Feb 4, 2023
65da3a2
fix(vpt): move to `absolutePointer`
ShaMan123 Feb 4, 2023
3c92b86
fix setCoords timing
ShaMan123 Feb 5, 2023
7d899bb
fix(): respect `isDrawingMode`
ShaMan123 Feb 5, 2023
01610a2
dep(): `isDrawingMode` => `brush#enabled`
ShaMan123 Feb 5, 2023
804ef49
feat(FabricEvent): `path`
ShaMan123 Feb 5, 2023
7d9abf5
feat(FabricEvent): stop propagation
ShaMan123 Feb 5, 2023
2b86cde
Merge branch 'master' into poly-brush2
ShaMan123 Feb 5, 2023
dab541c
Merge branch 'master' into clip-brush
ShaMan123 Feb 5, 2023
d1a0fdf
Update freedraw.js
ShaMan123 Feb 5, 2023
f1f0843
Update FabricEvent.ts
ShaMan123 Feb 5, 2023
9358938
Merge branch 'poly-brush2' into clip-brush
ShaMan123 Feb 5, 2023
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
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"env": {
"browser": true
},
"parserOptions": {
"ecmaVersion": 8
},
"globals": {
"Promise": true,
"define": true,
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## [next]

- feat(Brush): new API, extend `Observable`, support `clipPath` [#8476](https://github.com/fabricjs/fabric.js/pull/8476)
BREAKING:
- brush doesn't add result to canvas anymore
- `path:created`, `before:path:created` events are deprecated, use `interaction:completed` instead
- `_render` method is now protected, use `render` instead
- renamed `PencilBrush#convertPointsToSVGPath` => `getPathFromPoints`
- `Canvas#freeDrawingCursor` => `Brush#cursor`
- feat(): DrawShape, DrawOval, DrawPoly [#8430](https://github.com/fabricjs/fabric.js/pull/8430)
- patch(): Added WebGLProbe to env, removed isLikelyNode, added specific env dispose ( instead of cleanup JSDOM ) [#8652](https://github.com/fabricjs/fabric.js/pull/8652)
- ci(): Removed the browser publish script [#8656](https://github.com/fabricjs/fabric.js/pull/8656)
- feat(): Node entry point [#8632](https://github.com/fabricjs/fabric.js/pull/8632)
Expand Down
12 changes: 12 additions & 0 deletions fabric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ import { Canvas2dFilterBackend } from './src/filters/Canvas2dFilterBackend';
import { WebGLFilterBackend } from './src/filters/WebGLFilterBackend';
import { runningAnimations } from './src/util/animation/AnimationRegistry';
import { Observable } from './src/Observable';
import { FabricEvent as Event } from './src/FabricEvent';
import { Point } from './src/Point';
import { Intersection } from './src/Intersection';
import { Color } from './src/color/Color';
Expand All @@ -334,10 +335,15 @@ import { Gradient } from './src/gradient/Gradient';
import { Pattern } from './src/Pattern';
import { Shadow } from './src/Shadow';
import { BaseBrush } from './src/brushes/BaseBrush';
import { SimpleBrush } from './src/brushes/SimpleBrush';
import { PencilBrush } from './src/brushes/PencilBrush';
import { CircleBrush } from './src/brushes/CircleBrush';
import { SprayBrush } from './src/brushes/SprayBrush';
import { PatternBrush } from './src/brushes/PatternBrush';
import { DrawOval } from './src/brushes/DrawOval';
import { DrawPoly } from './src/brushes/DrawPoly';
import { DrawShape } from './src/brushes/DrawShape';
import { DrawShapeBase } from './src/brushes/DrawShapeBase';
import { FabricObject as Object } from './src/shapes/Object/FabricObject';
import { Line } from './src/shapes/Line';
import { Circle } from './src/shapes/Circle';
Expand Down Expand Up @@ -386,14 +392,20 @@ export {
Color,
Control,
Observable,
Event,
Gradient,
Pattern,
Shadow,
BaseBrush,
SimpleBrush,
PencilBrush,
CircleBrush,
SprayBrush,
PatternBrush,
DrawOval,
DrawPoly,
DrawShape,
DrawShapeBase,
Object,
Line,
Circle,
Expand Down
6 changes: 3 additions & 3 deletions src/EventTypeDefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ export type TPointerEventInfo<E extends TPointerEvent = TPointerEvent> =
TEvent<E> & {
target?: FabricObject;
subTargets?: FabricObject[];
button?: number;
isClick: boolean;
pointer: Point;
transform?: Transform | null;
Expand Down Expand Up @@ -260,6 +259,8 @@ export type StaticCanvasEvents = CollectionEvents & {
// rendering
'before:render': { ctx: CanvasRenderingContext2D };
'after:render': { ctx: CanvasRenderingContext2D };

resize: never;
};

export type CanvasEvents = StaticCanvasEvents &
Expand All @@ -269,8 +270,7 @@ export type CanvasEvents = StaticCanvasEvents &
CanvasModificationEvents &
CanvasSelectionEvents & {
// brushes
'before:path:created': { path: FabricObject };
'path:created': { path: FabricObject };
'interaction:completed': { result?: FabricObject };

// erasing
'erasing:start': never;
Expand Down
71 changes: 71 additions & 0 deletions src/FabricEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import type { Observable } from './Observable';

export type TFabricEvent<T> = FabricEvent<T> & T;

export enum PropagationState {
propagate = 1,
stop = 0,
stopImmediately = -1,
}

export class FabricEvent<T> {
declare defaultPrevented: boolean;
declare propagate: PropagationState;
declare path: Observable<any>[];

static init<T>(data?: T) {
return new FabricEvent<T>(data) as TFabricEvent<T>;
}

private constructor(data?: T) {
Object.defineProperties(this, {
defaultPrevented: {
value: false,
enumerable: false,
configurable: false,
writable: true,
},
propagate: {
value: PropagationState.propagate,
enumerable: false,
configurable: false,
writable: true,
},
path: {
value: [],
enumerable: false,
configurable: false,
writable: true,
},
});
Object.assign(this, data || {});
}

/**
* https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault
*/
preventDefault() {
this.defaultPrevented = true;
}

/**
* https://developer.mozilla.org/en-US/docs/Web/API/Event/stopPropagation
*/
stopPropagation() {
this.propagate = PropagationState.stop;
}

/**
* https://developer.mozilla.org/en-US/docs/Web/API/Event/stopImmediatePropagation
*/
stopImmediatePropagation() {
this.propagate = PropagationState.stopImmediately;
}

/**
* https://developer.mozilla.org/en-US/docs/Web/API/Event/composedPath
*/
composedPath() {
return this.path;
}
}
35 changes: 26 additions & 9 deletions src/Observable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export type TEventCallback<T = any> = (options: T) => any;
import { FabricEvent, PropagationState, TFabricEvent } from './FabricEvent';

export type TEventCallback<T = any> = (options: TFabricEvent<T>) => any;

type EventRegistryObject<
K extends string | number | symbol = string,
Expand Down Expand Up @@ -160,16 +162,31 @@ export class Observable<EventSpec> {
* @param {String} eventName Event name to fire
* @param {Object} [options] Options object
*/
fire<K extends keyof EventSpec>(eventName: K, options?: EventSpec[K]) {
if (!this.__eventListeners) {
return;
}

const listenersForEvent = this.__eventListeners[eventName]?.concat();
if (listenersForEvent) {
fire<K extends keyof EventSpec>(
eventName: K,
options?: EventSpec[K] | TFabricEvent<EventSpec[K]>
): TFabricEvent<EventSpec[K]> {
const ev =
options instanceof FabricEvent ? options : FabricEvent.init(options);
if (
this.__eventListeners &&
this.__eventListeners[eventName] &&
ev.propagate === PropagationState.propagate
) {
let called = false;
const listenersForEvent = this.__eventListeners[eventName].concat();
for (let i = 0; i < listenersForEvent.length; i++) {
listenersForEvent[i].call(this, options || {});
if (
(ev.propagate as PropagationState) ===
PropagationState.stopImmediately
) {
break;
}
called = true;
listenersForEvent[i].call(this, ev);
}
called && ev.path.push(this);
}
return ev;
}
}
Loading