-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
ClipPathLayout.ts
74 lines (69 loc) · 2.5 KB
/
ClipPathLayout.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { Point } from '../../Point';
import type { FabricObject } from '../../shapes/Object/FabricObject';
import { makeBoundingBoxFromPoints } from '../../util/misc/boundingBoxFromPoints';
import { sendPointToPlane } from '../../util/misc/planeChange';
import type { LayoutStrategyResult, StrictLayoutContext } from '../types';
import { LayoutStrategy } from './LayoutStrategy';
import { getObjectBounds } from './utils';
import { classRegistry } from '../../ClassRegistry';
/**
* Layout will adjust the bounding box to match the clip path bounding box.
*/
export class ClipPathLayout extends LayoutStrategy {
static readonly type = 'clip-path';
shouldPerformLayout(context: StrictLayoutContext): boolean {
return !!context.target.clipPath && super.shouldPerformLayout(context);
}
shouldLayoutClipPath() {
return false;
}
calcLayoutResult(
context: StrictLayoutContext,
objects: FabricObject[]
): LayoutStrategyResult | undefined {
const { target } = context;
const { clipPath } = target;
if (!clipPath || !this.shouldPerformLayout(context)) {
return;
}
// TODO: remove stroke calculation from this case
const { width, height } = makeBoundingBoxFromPoints(
getObjectBounds(target, clipPath as FabricObject)
);
const size = new Point(width, height);
if (clipPath.absolutePositioned) {
// we want the center point to exist in group's containing plane
const clipPathCenter = sendPointToPlane(
clipPath.getRelativeCenterPoint(),
undefined,
target.group?.calcTransformMatrix()
);
return {
center: clipPathCenter,
size,
};
} else {
// we want the center point to exist in group's containing plane, so we send it upwards
const clipPathCenter = clipPath
.getRelativeCenterPoint()
.transform(target.calcOwnMatrix(), true);
if (this.shouldPerformLayout(context)) {
// the clip path is positioned relative to the group's center which is affected by the bbox
// so we first calculate the bbox
const { center = new Point(), correction = new Point() } =
this.calcBoundingBox(objects, context) || {};
return {
center: center.add(clipPathCenter),
correction: correction.subtract(clipPathCenter),
size,
};
} else {
return {
center: target.getRelativeCenterPoint().add(clipPathCenter),
size,
};
}
}
}
}
classRegistry.setClass(ClipPathLayout);