-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ClippingGroup.ts
56 lines (48 loc) · 1.19 KB
/
ClippingGroup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import {
Group,
LayoutManager,
FixedLayout,
Path,
FabricObject,
GroupProps,
} from 'fabric';
export class ClippingGroup extends Group {
static type = 'clipping';
static getDefaults(): Record<string, any> {
return {
...super.getDefaults(),
originX: 'center',
originY: 'center',
left: 0,
top: 0,
};
}
private blockErasing = false;
constructor(objects: FabricObject[], options: Partial<GroupProps>) {
super(objects, {
layoutManager: new LayoutManager(new FixedLayout()),
...options,
});
}
drawObject(ctx: CanvasRenderingContext2D) {
const paths: Path[] = [];
const objects: FabricObject[] = [];
this._objects.forEach((object) =>
(object instanceof Path ? paths : objects).push(object)
);
ctx.save();
ctx.fillStyle = 'black';
ctx.fillRect(-this.width / 2, -this.height / 2, this.width, this.height);
ctx.restore();
!this.blockErasing &&
paths.forEach((path) => {
path.render(ctx);
});
objects.forEach((object) => {
object.globalCompositeOperation = object.inverted
? 'destination-out'
: 'source-in';
object.render(ctx);
});
}
}