Skip to content

Commit 7cbd41e

Browse files
committed
Use multVecMatrix in getBoundingBoxByChildren
Try to apply transformation matrix to all 4 corners of the element and then define rectangular bonding box
1 parent 8f117ea commit 7cbd41e

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/utils/bbox.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Context } from '../context/context'
22
import { getAttribute } from './node'
33
import { SvgNode } from '../nodes/svgnode'
4-
import { Rect } from './geometry'
4+
import { Rect, multVecMatrix } from './geometry'
55

66
export function getBoundingBoxByChildren(context: Context, svgnode: SvgNode): number[] {
77
if (getAttribute(svgnode.element, context.styleSheets, 'display') === 'none') {
@@ -12,22 +12,26 @@ export function getBoundingBoxByChildren(context: Context, svgnode: SvgNode): nu
1212
const nodeBox = child.getBoundingBox(context)
1313
if ((nodeBox[0] === 0) && (nodeBox[1] === 0) && (nodeBox[2] === 0) && (nodeBox[3] === 0))
1414
return
15-
const transform = child.computeNodeTransform(context)
16-
// TODO: take into account rotation matrix
17-
nodeBox[0] = nodeBox[0] * transform.sx + transform.tx
18-
nodeBox[1] = nodeBox[1] * transform.sy + transform.ty
19-
nodeBox[2] = nodeBox[2] * transform.sx
20-
nodeBox[3] = nodeBox[3] * transform.sy
15+
const transform = child.computeNodeTransform(context),
16+
p1 = multVecMatrix([nodeBox[0], nodeBox[1]], transform),
17+
p2 = multVecMatrix([nodeBox[0] + nodeBox[2], nodeBox[1]], transform),
18+
p3 = multVecMatrix([nodeBox[0], nodeBox[1] + nodeBox[3]], transform),
19+
p4 = multVecMatrix([nodeBox[0] + nodeBox[2], nodeBox[1] + nodeBox[3]], transform),
20+
minx = Math.min(p1[0], p2[0], p3[0], p4[0]),
21+
miny = Math.min(p1[1], p2[1], p3[1], p4[1]),
22+
maxx = Math.max(p1[0], p2[0], p3[0], p4[0]),
23+
maxy = Math.max(p1[1], p2[1], p3[1], p4[1]);
24+
2125
if (boundingBox.length === 0)
22-
boundingBox = nodeBox
26+
boundingBox = [minx, miny, maxx - minx, maxy - miny]
2327
else
2428
boundingBox = [
25-
Math.min(boundingBox[0], nodeBox[0]),
26-
Math.min(boundingBox[1], nodeBox[1]),
27-
Math.max(boundingBox[0] + boundingBox[2], nodeBox[0] + nodeBox[2]) -
28-
Math.min(boundingBox[0], nodeBox[0]),
29-
Math.max(boundingBox[1] + boundingBox[3], nodeBox[1] + nodeBox[3]) -
30-
Math.min(boundingBox[1], nodeBox[1])
29+
Math.min(boundingBox[0], minx),
30+
Math.min(boundingBox[1], miny),
31+
Math.max(boundingBox[0] + boundingBox[2], maxx) -
32+
Math.min(boundingBox[0], minx),
33+
Math.max(boundingBox[1] + boundingBox[3], maxy) -
34+
Math.min(boundingBox[1], miny)
3135
]
3236
})
3337
return boundingBox.length === 0 ? [0, 0, 0, 0] : boundingBox

0 commit comments

Comments
 (0)