-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathgeom.ts
519 lines (456 loc) · 14.4 KB
/
geom.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
import { Point, Line, Rectangle, Polyline, Ellipse, Path } from '../../geometry'
import { attr } from './attr'
import { sample, toPath, getPointsFromSvgElement } from './path'
import {
ensureId,
isSVGGraphicsElement,
createSvgElement,
isHTMLElement,
} from './elem'
import { getComputedStyle } from './style'
import {
createSVGPoint,
createSVGMatrix,
decomposeMatrix,
transformRectangle,
transformStringToMatrix,
} from './matrix'
/**
* Returns the bounding box of the element after transformations are
* applied. If `withoutTransformations` is `true`, transformations of
* the element will not be considered when computing the bounding box.
* If `target` is specified, bounding box will be computed relatively
* to the `target` element.
*/
export function bbox(
elem: SVGElement,
withoutTransformations?: boolean,
target?: SVGElement,
): Rectangle {
let box
const ownerSVGElement = elem.ownerSVGElement
// If the element is not in the live DOM, it does not have a bounding
// box defined and so fall back to 'zero' dimension element.
if (!ownerSVGElement) {
return new Rectangle(0, 0, 0, 0)
}
try {
box = (elem as SVGGraphicsElement).getBBox()
} catch (e) {
// Fallback for IE.
box = {
x: elem.clientLeft,
y: elem.clientTop,
width: elem.clientWidth,
height: elem.clientHeight,
}
}
if (withoutTransformations) {
return Rectangle.create(box)
}
const matrix = getTransformToElement(elem, target || ownerSVGElement)
return transformRectangle(box, matrix)
}
/**
* Returns the bounding box of the element after transformations are
* applied. Unlike `bbox()`, this function fixes a browser implementation
* bug to return the correct bounding box if this elemenent is a group of
* svg elements (if `options.recursive` is specified).
*/
export function getBBox(
elem: SVGElement,
options: {
target?: SVGElement | null
recursive?: boolean
} = {},
): Rectangle {
let outputBBox
const ownerSVGElement = elem.ownerSVGElement
// If the element is not in the live DOM, it does not have a bounding box
// defined and so fall back to 'zero' dimension element.
// If the element is not an SVGGraphicsElement, we could not measure the
// bounding box either
if (!ownerSVGElement || !isSVGGraphicsElement(elem)) {
if (isHTMLElement(elem)) {
// If the element is a HTMLElement, return the position relative to the body
const { left, top, width, height } = getBoundingOffsetRect(elem as any)
return new Rectangle(left, top, width, height)
}
return new Rectangle(0, 0, 0, 0)
}
let target = options.target
const recursive = options.recursive
if (!recursive) {
try {
outputBBox = elem.getBBox()
} catch (e) {
outputBBox = {
x: elem.clientLeft,
y: elem.clientTop,
width: elem.clientWidth,
height: elem.clientHeight,
}
}
if (!target) {
return Rectangle.create(outputBBox)
}
// transform like target
const matrix = getTransformToElement(elem, target)
return transformRectangle(outputBBox, matrix)
}
// recursive
{
const children = elem.childNodes
const n = children.length
if (n === 0) {
return getBBox(elem, { target })
}
if (!target) {
target = elem // eslint-disable-line
}
for (let i = 0; i < n; i += 1) {
const child = children[i] as SVGElement
let childBBox
if (child.childNodes.length === 0) {
childBBox = getBBox(child, { target })
} else {
// if child is a group element, enter it with a recursive call
childBBox = getBBox(child, { target, recursive: true })
}
if (!outputBBox) {
outputBBox = childBBox
} else {
outputBBox = outputBBox.union(childBBox)
}
}
return outputBBox as Rectangle
}
}
// BBox is calculated by the attribute on the node
export function getBBoxByElementAttr(elem: SVGElement) {
let node = elem
let tagName = node ? node.tagName.toLowerCase() : ''
// find shape node
while (tagName === 'g') {
node = node.firstElementChild as SVGElement
tagName = node ? node.tagName.toLowerCase() : ''
}
const attr = (name: string) => {
const s = node.getAttribute(name)
const v = s ? parseFloat(s) : 0
return Number.isNaN(v) ? 0 : v
}
let r
let bbox
switch (tagName) {
case 'rect':
bbox = new Rectangle(attr('x'), attr('y'), attr('width'), attr('height'))
break
case 'circle':
r = attr('r')
bbox = new Rectangle(attr('cx') - r, attr('cy') - r, 2 * r, 2 * r)
break
default:
break
}
return bbox
}
// Matrix is calculated by the transform attribute on the node
export function getMatrixByElementAttr(elem: SVGElement, target: SVGElement) {
let matrix = createSVGMatrix()
if (isSVGGraphicsElement(target) && isSVGGraphicsElement(elem)) {
let node = elem
const matrixList = []
while (node && node !== target) {
const transform = node.getAttribute('transform') || null
const nodeMatrix = transformStringToMatrix(transform)
matrixList.push(nodeMatrix)
node = node.parentNode as SVGGraphicsElement
}
matrixList.reverse().forEach((m) => {
matrix = matrix.multiply(m)
})
}
return matrix
}
/**
* Returns an DOMMatrix that specifies the transformation necessary
* to convert `elem` coordinate system into `target` coordinate system.
*/
export function getTransformToElement(elem: SVGElement, target: SVGElement) {
if (isSVGGraphicsElement(target) && isSVGGraphicsElement(elem)) {
const targetCTM = target.getScreenCTM()
const nodeCTM = elem.getScreenCTM()
if (targetCTM && nodeCTM) {
return targetCTM.inverse().multiply(nodeCTM)
}
}
// Could not get actual transformation matrix
return createSVGMatrix()
}
/**
* Converts a global point with coordinates `x` and `y` into the
* coordinate space of the element.
*/
export function toLocalPoint(
elem: SVGElement | SVGSVGElement,
x: number,
y: number,
) {
const svg =
elem instanceof SVGSVGElement
? elem
: (elem.ownerSVGElement as SVGSVGElement)
const p = svg.createSVGPoint()
p.x = x
p.y = y
try {
const ctm = svg.getScreenCTM()!
const globalPoint = p.matrixTransform(ctm.inverse())
const globalToLocalMatrix = getTransformToElement(elem, svg).inverse()
return globalPoint.matrixTransform(globalToLocalMatrix)
} catch (e) {
return p
}
}
/**
* Convert the SVGElement to an equivalent geometric shape. The element's
* transformations are not taken into account.
*
* SVGRectElement => Rectangle
*
* SVGLineElement => Line
*
* SVGCircleElement => Ellipse
*
* SVGEllipseElement => Ellipse
*
* SVGPolygonElement => Polyline
*
* SVGPolylineElement => Polyline
*
* SVGPathElement => Path
*
* others => Rectangle
*/
export function toGeometryShape(elem: SVGElement) {
const attr = (name: string) => {
const s = elem.getAttribute(name)
const v = s ? parseFloat(s) : 0
return Number.isNaN(v) ? 0 : v
}
switch (elem instanceof SVGElement && elem.nodeName.toLowerCase()) {
case 'rect':
return new Rectangle(attr('x'), attr('y'), attr('width'), attr('height'))
case 'circle':
return new Ellipse(attr('cx'), attr('cy'), attr('r'), attr('r'))
case 'ellipse':
return new Ellipse(attr('cx'), attr('cy'), attr('rx'), attr('ry'))
case 'polyline': {
const points = getPointsFromSvgElement(elem as SVGPolylineElement)
return new Polyline(points)
}
case 'polygon': {
const points = getPointsFromSvgElement(elem as SVGPolygonElement)
if (points.length > 1) {
points.push(points[0])
}
return new Polyline(points)
}
case 'path': {
let d = elem.getAttribute('d') as string
if (!Path.isValid(d)) {
d = Path.normalize(d)
}
return Path.parse(d)
}
case 'line': {
return new Line(attr('x1'), attr('y1'), attr('x2'), attr('y2'))
}
default:
break
}
// Anything else is a rectangle
return getBBox(elem)
}
export function getIntersection(
elem: SVGElement | SVGSVGElement,
ref: Point | Point.PointLike | Point.PointData,
target?: SVGElement,
) {
const svg = elem instanceof SVGSVGElement ? elem : elem.ownerSVGElement!
target = target || svg // eslint-disable-line
const bbox = getBBox(target)
const center = bbox.getCenter()
if (!bbox.intersectsWithLineFromCenterToPoint(ref)) {
return null
}
let spot: Point | null = null
const tagName = elem.tagName.toLowerCase()
// Little speed up optimization for `<rect>` element. We do not do convert
// to path element and sampling but directly calculate the intersection
// through a transformed geometrical rectangle.
if (tagName === 'rect') {
const gRect = new Rectangle(
parseFloat(elem.getAttribute('x') || '0'),
parseFloat(elem.getAttribute('y') || '0'),
parseFloat(elem.getAttribute('width') || '0'),
parseFloat(elem.getAttribute('height') || '0'),
)
// Get the rect transformation matrix with regards to the SVG document.
const rectMatrix = getTransformToElement(elem, target)
const rectMatrixComponents = decomposeMatrix(rectMatrix)
// Rotate the rectangle back so that we can use
// `intersectsWithLineFromCenterToPoint()`.
const reseted = svg.createSVGTransform()
reseted.setRotate(-rectMatrixComponents.rotation, center.x, center.y)
const rect = transformRectangle(gRect, reseted.matrix.multiply(rectMatrix))
spot = Rectangle.create(rect).intersectsWithLineFromCenterToPoint(
ref,
rectMatrixComponents.rotation,
)
} else if (
tagName === 'path' ||
tagName === 'polygon' ||
tagName === 'polyline' ||
tagName === 'circle' ||
tagName === 'ellipse'
) {
const pathNode = tagName === 'path' ? elem : toPath(elem as any)
const samples = sample(pathNode as SVGPathElement)
let minDistance = Infinity
let closestSamples: any[] = []
for (let i = 0, ii = samples.length; i < ii; i += 1) {
const sample = samples[i]
// Convert the sample point in the local coordinate system
// to the global coordinate system.
let gp = createSVGPoint(sample.x, sample.y)
gp = gp.matrixTransform(getTransformToElement(elem, target))
const ggp = Point.create(gp)
const centerDistance = ggp.distance(center)
// Penalize a higher distance to the reference point by 10%.
// This gives better results. This is due to
// inaccuracies introduced by rounding errors and getPointAtLength() returns.
const refDistance = ggp.distance(ref) * 1.1
const distance = centerDistance + refDistance
if (distance < minDistance) {
minDistance = distance
closestSamples = [{ sample, refDistance }]
} else if (distance < minDistance + 1) {
closestSamples.push({ sample, refDistance })
}
}
closestSamples.sort((a, b) => a.refDistance - b.refDistance)
if (closestSamples[0]) {
spot = Point.create(closestSamples[0].sample)
}
}
return spot
}
export interface AnimateCallbacks {
start?: (e: Event) => void
repeat?: (e: Event) => void
complete?: (e: Event) => void
}
export type AnimationOptions = AnimateCallbacks & {
[name: string]: string | number | undefined
}
export function animate(elem: SVGElement, options: AnimationOptions) {
return createAnimation(elem, options, 'animate')
}
export function animateTransform(elem: SVGElement, options: AnimationOptions) {
return createAnimation(elem, options, 'animateTransform')
}
function createAnimation(
elem: SVGElement,
options: AnimationOptions,
type: 'animate' | 'animateTransform',
) {
// @see
// https://www.w3.org/TR/SVG11/animate.html#AnimateElement
// https://developer.mozilla.org/en-US/docs/Web/API/SVGAnimateElement
// https://developer.mozilla.org/en-US/docs/Web/API/SVGAnimateTransformElement
const animate = createSvgElement<SVGAnimationElement>(type)
elem.appendChild(animate)
try {
return setupAnimation(animate, options)
} catch (error) {
// pass
}
return () => {}
}
function setupAnimation(
animate: SVGAnimationElement,
options: AnimationOptions,
) {
const { start, complete, repeat, ...attrs } = options
attr(animate, attrs)
start && animate.addEventListener('beginEvent', start)
complete && animate.addEventListener('endEvent', complete)
repeat && animate.addEventListener('repeatEvent', repeat)
const ani = animate as any
ani.beginElement()
return () => ani.endElement()
}
/**
* Animate the element along the path SVG element (or Vector object).
* `attrs` contain Animation Timing attributes describing the animation.
*/
export function animateAlongPath(
elem: SVGElement,
options: AnimationOptions,
path: SVGPathElement,
): () => void {
const id = ensureId(path)
// https://developer.mozilla.org/en-US/docs/Web/API/SVGAnimationElement
const animate = createSvgElement<SVGAnimateMotionElement>('animateMotion')
const mpath = createSvgElement('mpath')
attr(mpath, { 'xlink:href': `#${id}` })
animate.appendChild(mpath)
elem.appendChild(animate)
try {
return setupAnimation(animate, options)
} catch (e) {
// Fallback for IE 9.
if (document.documentElement.getAttribute('smiling') === 'fake') {
// Register the animation. (See `https://answers.launchpad.net/smil/+question/203333`)
const ani = animate as any
ani.animators = []
const win = window as any
const animationID = ani.getAttribute('id')
if (animationID) {
win.id2anim[animationID] = ani
}
const targets = win.getTargets(ani)
for (let i = 0, ii = targets.length; i < ii; i += 1) {
const target = targets[i]
const animator = new win.Animator(ani, target, i)
win.animators.push(animator)
ani.animators[i] = animator
animator.register()
}
}
}
return () => {}
}
export function getBoundingOffsetRect(elem: HTMLElement) {
let left = 0
let top = 0
let width = 0
let height = 0
if (elem) {
let current = elem as any
while (current) {
left += current.offsetLeft
top += current.offsetTop
current = current.offsetParent
if (current) {
left += parseInt(getComputedStyle(current, 'borderLeft'), 10)
top += parseInt(getComputedStyle(current, 'borderTop'), 10)
}
}
width = elem.offsetWidth
height = elem.offsetHeight
}
return { left, top, width, height }
}