@wardic/dom 1.1.3 → 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/unstable/PagePoint.d.ts +43 -0
- package/dist/unstable/PagePoint.d.ts.map +1 -0
- package/dist/unstable/PagePoint.js +119 -0
- package/dist/unstable/PagePoint.js.map +1 -0
- package/package.json +3 -3
- package/src/index.ts +1 -1
- package/src/unstable/PagePoint.ts +129 -0
package/dist/index.d.ts
CHANGED
|
@@ -28,7 +28,7 @@ import type * as RefreshFrameSource from "@wardic/core/RefreshFrameSource";
|
|
|
28
28
|
import type * as Renderer from "@wardic/core/Renderer";
|
|
29
29
|
import * as Layer from "effect/Layer";
|
|
30
30
|
import * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
31
|
-
export declare const VERSION = "1.1.
|
|
31
|
+
export declare const VERSION = "1.1.4";
|
|
32
32
|
export * as DomBatch from "./DomBatch.ts";
|
|
33
33
|
export * as DomClock from "./DomClock.ts";
|
|
34
34
|
export * as DomInput from "./DomInput.ts";
|
package/dist/index.js
CHANGED
|
@@ -8,7 +8,7 @@ import * as DomInterpolators from "./DomInterpolators.js";
|
|
|
8
8
|
import * as DomPositionalKeys from "./DomPositionalKeys.js";
|
|
9
9
|
import * as DomProbe from "./DomProbe.js";
|
|
10
10
|
import * as DomRenderer from "./DomRenderer.js";
|
|
11
|
-
export const VERSION = "1.1.
|
|
11
|
+
export const VERSION = "1.1.4";
|
|
12
12
|
export * as DomBatch from "./DomBatch.js";
|
|
13
13
|
export * as DomClock from "./DomClock.js";
|
|
14
14
|
export * as DomInput from "./DomInput.js";
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type * as WardConfig from "@wardic/dom/unstable/WardConfig";
|
|
2
|
+
/** An element given as it is, an accessor of one, or a selector (#795), looked up in the document at each point. */
|
|
3
|
+
export type ElementGiven<E extends Element> = E | (() => E | undefined) | string;
|
|
4
|
+
/**
|
|
5
|
+
* A `transformPagePoint` for a drag or a pan inside a parent with a CSS transform, rotated, scaled or
|
|
6
|
+
* skewed: each pointer point is turned through the inverse of the parent's transform, read at each
|
|
7
|
+
* point, so the element stays under the pointer at any zoom, and a parent that animates is followed.
|
|
8
|
+
* The parent is an element, an accessor of a ref, or a selector looked up in the document at each
|
|
9
|
+
* point. Motion's `correctParentTransform`.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* // On Solid: a ref's accessor.
|
|
13
|
+
* let plan!: HTMLDivElement
|
|
14
|
+
* <div ref={plan} style={{ transform: `scale(${zoom()})` }}>
|
|
15
|
+
* <WardicConfig transformPagePoint={correctParentTransform(() => plan)}>
|
|
16
|
+
* <w.div drag />
|
|
17
|
+
* </WardicConfig>
|
|
18
|
+
* </div>
|
|
19
|
+
* // On Foldkit, which has no refs: a selector.
|
|
20
|
+
* w.div([w.Drag(), w.Config({ transformPagePoint: correctParentTransform("#plan") })])
|
|
21
|
+
* @see https://wardic.dev/reference/solid/utilities/page-points
|
|
22
|
+
*/
|
|
23
|
+
export declare const correctParentTransform: (parent: ElementGiven<Element>) => WardConfig.WardicTransformPoint;
|
|
24
|
+
/**
|
|
25
|
+
* A `transformPagePoint` for a drag or a pan inside an SVG whose `viewBox` differs from the size it
|
|
26
|
+
* renders at: each pointer point is scaled into the viewBox's units, so a dragged shape stays under
|
|
27
|
+
* the pointer. The SVG is an element, an accessor of a ref, or a selector looked up in the document at
|
|
28
|
+
* each point; a selector that finds no SVG maps nothing. Motion's `transformViewBoxPoint`.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* // On Solid: a ref's accessor.
|
|
32
|
+
* let svg!: SVGSVGElement
|
|
33
|
+
* <WardicConfig transformPagePoint={transformViewBoxPoint(() => svg)}>
|
|
34
|
+
* <svg ref={svg} viewBox="0 0 100 100" width="500" height="500">
|
|
35
|
+
* <w.rect drag width="10" height="10" />
|
|
36
|
+
* </svg>
|
|
37
|
+
* </WardicConfig>
|
|
38
|
+
* // On Foldkit: a selector.
|
|
39
|
+
* w.rect([w.Drag(), w.Config({ transformPagePoint: transformViewBoxPoint("#plan") })])
|
|
40
|
+
* @see https://wardic.dev/reference/solid/utilities/page-points
|
|
41
|
+
*/
|
|
42
|
+
export declare const transformViewBoxPoint: (svg: ElementGiven<SVGSVGElement>) => WardConfig.WardicTransformPoint;
|
|
43
|
+
//# sourceMappingURL=PagePoint.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PagePoint.d.ts","sourceRoot":"","sources":["../../src/unstable/PagePoint.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,KAAK,UAAU,MAAM,iCAAiC,CAAA;AAElE,oHAAoH;AACpH,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,OAAO,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,SAAS,CAAC,GAAG,MAAM,CAAA;AA6ChF;;;;;;;;;;;;;;;;;;GAkBG;AAEH,eAAO,MAAM,sBAAsB,WACxB,YAAY,CAAC,OAAO,CAAC,KAAG,UAAU,CAAC,oBAY3C,CAAA;AAEH;;;;;;;;;;;;;;;;;GAiBG;AAEH,eAAO,MAAM,qBAAqB,QAC1B,YAAY,CAAC,aAAa,CAAC,KAAG,UAAU,CAAC,oBAY9C,CAAA"}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Point mappings for a provider's `transformPagePoint` (#443, Motion's `correctParentTransform` and
|
|
3
|
+
* `transformViewBoxPoint`), for every DOM binding. Each takes its element as it is, an accessor of one
|
|
4
|
+
* (a ref's, set at mount), or a selector, for a binding with no refs (Foldkit's), read at each point.
|
|
5
|
+
* Wardic's points are in the viewport's coordinates, where Motion's are the page's, so neither adds the
|
|
6
|
+
* page's scroll.
|
|
7
|
+
*
|
|
8
|
+
* Unstable, for Wardic's own packages: public, with no semver promise.
|
|
9
|
+
*
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
12
|
+
// Unstable: ADR-0071, decision 5. #795: solid's `page-point.ts`, moved to dom for the Foldkit binding (its storefront's room example).
|
|
13
|
+
import * as Predicate from "effect/Predicate";
|
|
14
|
+
/** The element now: an accessor's, the first its selector finds, or the one given. */
|
|
15
|
+
const elementOf = (given) => {
|
|
16
|
+
if (Predicate.isFunction(given))
|
|
17
|
+
return given();
|
|
18
|
+
if (Predicate.isString(given))
|
|
19
|
+
return foundBy(given);
|
|
20
|
+
return given;
|
|
21
|
+
};
|
|
22
|
+
/** The first element `selector` finds in the document, if any. */
|
|
23
|
+
const foundBy = (selector) => {
|
|
24
|
+
const found = document.querySelector(selector);
|
|
25
|
+
if (Predicate.isNull(found))
|
|
26
|
+
return undefined;
|
|
27
|
+
return found;
|
|
28
|
+
};
|
|
29
|
+
/** Whether an element is an SVG root: a selector may find any element. */
|
|
30
|
+
const isSvg = (element) => Predicate.hasProperty(element, "viewBox");
|
|
31
|
+
/** The SVG `given` names now, if it names one. */
|
|
32
|
+
const svgOf = (given) => {
|
|
33
|
+
const element = elementOf(given);
|
|
34
|
+
if (Predicate.isUndefined(element))
|
|
35
|
+
return undefined;
|
|
36
|
+
if (isSvg(element))
|
|
37
|
+
return element;
|
|
38
|
+
return undefined;
|
|
39
|
+
};
|
|
40
|
+
/** The inverse of `element`'s computed transform's linear part, or `undefined` for none, or one that can't be inverted. */
|
|
41
|
+
const inverseOf = (element) => {
|
|
42
|
+
const transform = getComputedStyle(element).transform;
|
|
43
|
+
if (Predicate.isUndefined(transform) || transform === "" || transform === "none")
|
|
44
|
+
return undefined;
|
|
45
|
+
const { a, b, c, d } = new DOMMatrixReadOnly(transform);
|
|
46
|
+
const det = a * d - b * c;
|
|
47
|
+
if (Math.abs(det) < 1e-10)
|
|
48
|
+
return undefined;
|
|
49
|
+
return { a: d / det, b: -b / det, c: -c / det, d: a / det };
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* A `transformPagePoint` for a drag or a pan inside a parent with a CSS transform, rotated, scaled or
|
|
53
|
+
* skewed: each pointer point is turned through the inverse of the parent's transform, read at each
|
|
54
|
+
* point, so the element stays under the pointer at any zoom, and a parent that animates is followed.
|
|
55
|
+
* The parent is an element, an accessor of a ref, or a selector looked up in the document at each
|
|
56
|
+
* point. Motion's `correctParentTransform`.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* // On Solid: a ref's accessor.
|
|
60
|
+
* let plan!: HTMLDivElement
|
|
61
|
+
* <div ref={plan} style={{ transform: `scale(${zoom()})` }}>
|
|
62
|
+
* <WardicConfig transformPagePoint={correctParentTransform(() => plan)}>
|
|
63
|
+
* <w.div drag />
|
|
64
|
+
* </WardicConfig>
|
|
65
|
+
* </div>
|
|
66
|
+
* // On Foldkit, which has no refs: a selector.
|
|
67
|
+
* w.div([w.Drag(), w.Config({ transformPagePoint: correctParentTransform("#plan") })])
|
|
68
|
+
* @see https://wardic.dev/reference/solid/utilities/page-points
|
|
69
|
+
*/
|
|
70
|
+
// #443. Points pass as they are while the parent is gone or has no transform.
|
|
71
|
+
export const correctParentTransform = (parent) => (point) => {
|
|
72
|
+
const element = elementOf(parent);
|
|
73
|
+
if (Predicate.isUndefined(element))
|
|
74
|
+
return point;
|
|
75
|
+
const inverse = inverseOf(element);
|
|
76
|
+
if (Predicate.isUndefined(inverse))
|
|
77
|
+
return point;
|
|
78
|
+
const box = element.getBoundingClientRect();
|
|
79
|
+
const cx = box.left + box.width / 2;
|
|
80
|
+
const cy = box.top + box.height / 2;
|
|
81
|
+
const dx = point.x - cx;
|
|
82
|
+
const dy = point.y - cy;
|
|
83
|
+
return { x: cx + inverse.a * dx + inverse.c * dy, y: cy + inverse.b * dx + inverse.d * dy };
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* A `transformPagePoint` for a drag or a pan inside an SVG whose `viewBox` differs from the size it
|
|
87
|
+
* renders at: each pointer point is scaled into the viewBox's units, so a dragged shape stays under
|
|
88
|
+
* the pointer. The SVG is an element, an accessor of a ref, or a selector looked up in the document at
|
|
89
|
+
* each point; a selector that finds no SVG maps nothing. Motion's `transformViewBoxPoint`.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* // On Solid: a ref's accessor.
|
|
93
|
+
* let svg!: SVGSVGElement
|
|
94
|
+
* <WardicConfig transformPagePoint={transformViewBoxPoint(() => svg)}>
|
|
95
|
+
* <svg ref={svg} viewBox="0 0 100 100" width="500" height="500">
|
|
96
|
+
* <w.rect drag width="10" height="10" />
|
|
97
|
+
* </svg>
|
|
98
|
+
* </WardicConfig>
|
|
99
|
+
* // On Foldkit: a selector.
|
|
100
|
+
* w.rect([w.Drag(), w.Config({ transformPagePoint: transformViewBoxPoint("#plan") })])
|
|
101
|
+
* @see https://wardic.dev/reference/solid/utilities/page-points
|
|
102
|
+
*/
|
|
103
|
+
// #443. Points pass as they are while the SVG is gone, has no viewBox, or renders at no size.
|
|
104
|
+
export const transformViewBoxPoint = (svg) => (point) => {
|
|
105
|
+
const element = svgOf(svg);
|
|
106
|
+
if (Predicate.isUndefined(element))
|
|
107
|
+
return point;
|
|
108
|
+
const viewBox = element.viewBox.baseVal;
|
|
109
|
+
if (Predicate.isNullish(viewBox) || (viewBox.width === 0 && viewBox.height === 0))
|
|
110
|
+
return point;
|
|
111
|
+
const box = element.getBoundingClientRect();
|
|
112
|
+
if (box.width === 0 || box.height === 0)
|
|
113
|
+
return point;
|
|
114
|
+
return {
|
|
115
|
+
x: (point.x - box.left) * (viewBox.width / box.width) + box.left,
|
|
116
|
+
y: (point.y - box.top) * (viewBox.height / box.height) + box.top,
|
|
117
|
+
};
|
|
118
|
+
};
|
|
119
|
+
//# sourceMappingURL=PagePoint.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PagePoint.js","sourceRoot":"","sources":["../../src/unstable/PagePoint.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,uIAAuI;AACvI,OAAO,KAAK,SAAS,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6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wardic/dom",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.4",
|
|
4
4
|
"description": "The browser environment for Wardic: its clock, renderer, input, probe and projection system, as Effect Layers.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"animation",
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
"typecheck:tests": "../../node_modules/typescript-7/bin/tsc -p tsconfig.test.json"
|
|
56
56
|
},
|
|
57
57
|
"dependencies": {
|
|
58
|
-
"@wardic/core": "1.1.
|
|
58
|
+
"@wardic/core": "1.1.4",
|
|
59
59
|
"csstype": "^3.1.0",
|
|
60
60
|
"culori": "^4.0.2"
|
|
61
61
|
},
|
|
@@ -63,6 +63,6 @@
|
|
|
63
63
|
"effect": "^4.1.4"
|
|
64
64
|
},
|
|
65
65
|
"devDependencies": {
|
|
66
|
-
"@wardic/test": "1.1.
|
|
66
|
+
"@wardic/test": "1.1.4"
|
|
67
67
|
}
|
|
68
68
|
}
|
package/src/index.ts
CHANGED
|
@@ -38,7 +38,7 @@ import * as DomPositionalKeys from "./DomPositionalKeys.ts"
|
|
|
38
38
|
import * as DomProbe from "./DomProbe.ts"
|
|
39
39
|
import * as DomRenderer from "./DomRenderer.ts"
|
|
40
40
|
|
|
41
|
-
export const VERSION = "1.1.
|
|
41
|
+
export const VERSION = "1.1.4"
|
|
42
42
|
|
|
43
43
|
export * as DomBatch from "./DomBatch.ts"
|
|
44
44
|
export * as DomClock from "./DomClock.ts"
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Point mappings for a provider's `transformPagePoint` (#443, Motion's `correctParentTransform` and
|
|
3
|
+
* `transformViewBoxPoint`), for every DOM binding. Each takes its element as it is, an accessor of one
|
|
4
|
+
* (a ref's, set at mount), or a selector, for a binding with no refs (Foldkit's), read at each point.
|
|
5
|
+
* Wardic's points are in the viewport's coordinates, where Motion's are the page's, so neither adds the
|
|
6
|
+
* page's scroll.
|
|
7
|
+
*
|
|
8
|
+
* Unstable, for Wardic's own packages: public, with no semver promise.
|
|
9
|
+
*
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
12
|
+
// Unstable: ADR-0071, decision 5. #795: solid's `page-point.ts`, moved to dom for the Foldkit binding (its storefront's room example).
|
|
13
|
+
import * as Predicate from "effect/Predicate"
|
|
14
|
+
import type * as WardConfig from "@wardic/dom/unstable/WardConfig"
|
|
15
|
+
|
|
16
|
+
/** An element given as it is, an accessor of one, or a selector (#795), looked up in the document at each point. */
|
|
17
|
+
export type ElementGiven<E extends Element> = E | (() => E | undefined) | string
|
|
18
|
+
|
|
19
|
+
/** The element now: an accessor's, the first its selector finds, or the one given. */
|
|
20
|
+
const elementOf = <E extends Element>(given: ElementGiven<E>): E | undefined => {
|
|
21
|
+
if (Predicate.isFunction(given)) return given()
|
|
22
|
+
if (Predicate.isString(given)) return foundBy<E>(given)
|
|
23
|
+
return given
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The first element `selector` finds in the document, if any. */
|
|
27
|
+
const foundBy = <E extends Element>(selector: string): E | undefined => {
|
|
28
|
+
const found = document.querySelector<E>(selector)
|
|
29
|
+
if (Predicate.isNull(found)) return undefined
|
|
30
|
+
return found
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Whether an element is an SVG root: a selector may find any element. */
|
|
34
|
+
const isSvg = (element: Element): element is SVGSVGElement => Predicate.hasProperty(element, "viewBox")
|
|
35
|
+
|
|
36
|
+
/** The SVG `given` names now, if it names one. */
|
|
37
|
+
const svgOf = (given: ElementGiven<SVGSVGElement>): SVGSVGElement | undefined => {
|
|
38
|
+
const element = elementOf(given)
|
|
39
|
+
if (Predicate.isUndefined(element)) return undefined
|
|
40
|
+
if (isSvg(element)) return element
|
|
41
|
+
return undefined
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** A 2D linear map's coefficients: the inverse of an element's transform, as Motion's `getInverseMatrix`. */
|
|
45
|
+
interface Linear {
|
|
46
|
+
readonly a: number
|
|
47
|
+
readonly b: number
|
|
48
|
+
readonly c: number
|
|
49
|
+
readonly d: number
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The inverse of `element`'s computed transform's linear part, or `undefined` for none, or one that can't be inverted. */
|
|
53
|
+
const inverseOf = (element: Element): Linear | undefined => {
|
|
54
|
+
const transform = getComputedStyle(element).transform
|
|
55
|
+
if (Predicate.isUndefined(transform) || transform === "" || transform === "none") return undefined
|
|
56
|
+
const { a, b, c, d } = new DOMMatrixReadOnly(transform)
|
|
57
|
+
const det = a * d - b * c
|
|
58
|
+
if (Math.abs(det) < 1e-10) return undefined
|
|
59
|
+
return { a: d / det, b: -b / det, c: -c / det, d: a / det }
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* A `transformPagePoint` for a drag or a pan inside a parent with a CSS transform, rotated, scaled or
|
|
64
|
+
* skewed: each pointer point is turned through the inverse of the parent's transform, read at each
|
|
65
|
+
* point, so the element stays under the pointer at any zoom, and a parent that animates is followed.
|
|
66
|
+
* The parent is an element, an accessor of a ref, or a selector looked up in the document at each
|
|
67
|
+
* point. Motion's `correctParentTransform`.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* // On Solid: a ref's accessor.
|
|
71
|
+
* let plan!: HTMLDivElement
|
|
72
|
+
* <div ref={plan} style={{ transform: `scale(${zoom()})` }}>
|
|
73
|
+
* <WardicConfig transformPagePoint={correctParentTransform(() => plan)}>
|
|
74
|
+
* <w.div drag />
|
|
75
|
+
* </WardicConfig>
|
|
76
|
+
* </div>
|
|
77
|
+
* // On Foldkit, which has no refs: a selector.
|
|
78
|
+
* w.div([w.Drag(), w.Config({ transformPagePoint: correctParentTransform("#plan") })])
|
|
79
|
+
* @see https://wardic.dev/reference/solid/utilities/page-points
|
|
80
|
+
*/
|
|
81
|
+
// #443. Points pass as they are while the parent is gone or has no transform.
|
|
82
|
+
export const correctParentTransform =
|
|
83
|
+
(parent: ElementGiven<Element>): WardConfig.WardicTransformPoint =>
|
|
84
|
+
(point) => {
|
|
85
|
+
const element = elementOf(parent)
|
|
86
|
+
if (Predicate.isUndefined(element)) return point
|
|
87
|
+
const inverse = inverseOf(element)
|
|
88
|
+
if (Predicate.isUndefined(inverse)) return point
|
|
89
|
+
const box = element.getBoundingClientRect()
|
|
90
|
+
const cx = box.left + box.width / 2
|
|
91
|
+
const cy = box.top + box.height / 2
|
|
92
|
+
const dx = point.x - cx
|
|
93
|
+
const dy = point.y - cy
|
|
94
|
+
return { x: cx + inverse.a * dx + inverse.c * dy, y: cy + inverse.b * dx + inverse.d * dy }
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* A `transformPagePoint` for a drag or a pan inside an SVG whose `viewBox` differs from the size it
|
|
99
|
+
* renders at: each pointer point is scaled into the viewBox's units, so a dragged shape stays under
|
|
100
|
+
* the pointer. The SVG is an element, an accessor of a ref, or a selector looked up in the document at
|
|
101
|
+
* each point; a selector that finds no SVG maps nothing. Motion's `transformViewBoxPoint`.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* // On Solid: a ref's accessor.
|
|
105
|
+
* let svg!: SVGSVGElement
|
|
106
|
+
* <WardicConfig transformPagePoint={transformViewBoxPoint(() => svg)}>
|
|
107
|
+
* <svg ref={svg} viewBox="0 0 100 100" width="500" height="500">
|
|
108
|
+
* <w.rect drag width="10" height="10" />
|
|
109
|
+
* </svg>
|
|
110
|
+
* </WardicConfig>
|
|
111
|
+
* // On Foldkit: a selector.
|
|
112
|
+
* w.rect([w.Drag(), w.Config({ transformPagePoint: transformViewBoxPoint("#plan") })])
|
|
113
|
+
* @see https://wardic.dev/reference/solid/utilities/page-points
|
|
114
|
+
*/
|
|
115
|
+
// #443. Points pass as they are while the SVG is gone, has no viewBox, or renders at no size.
|
|
116
|
+
export const transformViewBoxPoint =
|
|
117
|
+
(svg: ElementGiven<SVGSVGElement>): WardConfig.WardicTransformPoint =>
|
|
118
|
+
(point) => {
|
|
119
|
+
const element = svgOf(svg)
|
|
120
|
+
if (Predicate.isUndefined(element)) return point
|
|
121
|
+
const viewBox = element.viewBox.baseVal
|
|
122
|
+
if (Predicate.isNullish(viewBox) || (viewBox.width === 0 && viewBox.height === 0)) return point
|
|
123
|
+
const box = element.getBoundingClientRect()
|
|
124
|
+
if (box.width === 0 || box.height === 0) return point
|
|
125
|
+
return {
|
|
126
|
+
x: (point.x - box.left) * (viewBox.width / box.width) + box.left,
|
|
127
|
+
y: (point.y - box.top) * (viewBox.height / box.height) + box.top,
|
|
128
|
+
}
|
|
129
|
+
}
|