center-center 0.0.40 → 0.0.43
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/docs/index.html +4 -1
- package/docs/index.mjs +10 -1
- package/package.json +14 -4
- package/src/common/index.d.mts +57 -0
- package/{index.mjs → src/common/index.mjs} +124 -246
- package/src/dom/index.d.mts +7 -0
- package/src/dom/index.mjs +46 -0
- package/src/index.d.mts +11 -0
- package/src/index.mjs +5 -0
- package/src/svg/index.d.mts +7 -0
- package/src/svg/index.mjs +58 -0
- package/src/svg/view-box/index.mjs +137 -0
- package/index.d.mts +0 -43
package/docs/index.html
CHANGED
|
@@ -7,7 +7,10 @@
|
|
|
7
7
|
{
|
|
8
8
|
"imports": {
|
|
9
9
|
"debug": "https://esm.sh/debug",
|
|
10
|
-
"center-center": "/assets/index.mjs"
|
|
10
|
+
"center-center": "/assets/index.mjs",
|
|
11
|
+
"#center-center/common": "/assets/common/index.mjs",
|
|
12
|
+
"#center-center/dom": "/assets/dom/index.mjs",
|
|
13
|
+
"#center-center/svg": "/assets/svg/index.mjs"
|
|
11
14
|
}
|
|
12
15
|
}
|
|
13
16
|
</script>
|
package/docs/index.mjs
CHANGED
|
@@ -80,7 +80,16 @@ app
|
|
|
80
80
|
res.sendFile('docs/images/favicon.ico', { root: '.' })
|
|
81
81
|
})
|
|
82
82
|
.get('/assets/index.mjs', (req, res) => {
|
|
83
|
-
res.sendFile('index.mjs', { root: '.' })
|
|
83
|
+
res.sendFile('src/index.mjs', { root: '.' })
|
|
84
|
+
})
|
|
85
|
+
.get('/assets/common/index.mjs', (req, res) => {
|
|
86
|
+
res.sendFile('src/common/index.mjs', { root: '.' })
|
|
87
|
+
})
|
|
88
|
+
.get('/assets/dom/index.mjs', (req, res) => {
|
|
89
|
+
res.sendFile('src/dom/index.mjs', { root: '.' })
|
|
90
|
+
})
|
|
91
|
+
.get('/assets/svg/index.mjs', (req, res) => {
|
|
92
|
+
res.sendFile('src/svg/index.mjs', { root: '.' })
|
|
84
93
|
})
|
|
85
94
|
.get('/', (req, res) => {
|
|
86
95
|
res.sendFile('docs/index.html', { root: '.' })
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "center-center",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.43",
|
|
4
4
|
"description": "Position an element at the center center of a container",
|
|
5
|
-
"main": "./index.mjs",
|
|
5
|
+
"main": "./src/index.mjs",
|
|
6
6
|
"type": "module",
|
|
7
|
-
"types": "./index.d.mts",
|
|
7
|
+
"types": "./src/index.d.mts",
|
|
8
8
|
"author": {
|
|
9
9
|
"name": "Jonathan Perry for Sequence Media Limited",
|
|
10
10
|
"email": "sequencemedia@sequencemedia.net",
|
|
@@ -52,6 +52,16 @@
|
|
|
52
52
|
"typescript": "^5.4.2"
|
|
53
53
|
},
|
|
54
54
|
"imports": {
|
|
55
|
-
"#center-center": "./index.mjs"
|
|
55
|
+
"#center-center/common": "./src/common/index.mjs",
|
|
56
|
+
"#center-center/dom": "./src/dom/index.mjs",
|
|
57
|
+
"#center-center/svg": "./src/svg/index.mjs",
|
|
58
|
+
"#center-center": "./src/index.mjs"
|
|
59
|
+
},
|
|
60
|
+
"exports": {
|
|
61
|
+
"./common": "./src/common/index.mjs",
|
|
62
|
+
"./dom": "./src/dom/index.mjs",
|
|
63
|
+
"./svg/view-box": "./src/svg/view-box/index.mjs",
|
|
64
|
+
"./svg": "./src/svg/index.mjs",
|
|
65
|
+
".": "./src/index.mjs"
|
|
56
66
|
}
|
|
57
67
|
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The document `scrollingElement`
|
|
3
|
+
*/
|
|
4
|
+
export interface ScrollingElement {
|
|
5
|
+
scrollLeft: number
|
|
6
|
+
scrollTop: number
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A Center Center `Rect`
|
|
11
|
+
*/
|
|
12
|
+
export interface CenterCenterRect {
|
|
13
|
+
left: number
|
|
14
|
+
top: number
|
|
15
|
+
width: number
|
|
16
|
+
height: number
|
|
17
|
+
right: number
|
|
18
|
+
bottom: number
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Center Center `Rects`
|
|
23
|
+
*/
|
|
24
|
+
export interface CenterCenterRects {
|
|
25
|
+
viewport: CenterCenterRect
|
|
26
|
+
container: CenterCenterRect
|
|
27
|
+
target: CenterCenterRect
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function getScrollingElement (): ScrollingElement
|
|
31
|
+
|
|
32
|
+
export function createViewportRect (): CenterCenterRect
|
|
33
|
+
export function createDOMElementRect (element: Element): CenterCenterRect
|
|
34
|
+
export function createSVGElementRect (element: SVGElement): CenterCenterRect
|
|
35
|
+
export function createDOMRects (container: Element, target: Element): CenterCenterRect
|
|
36
|
+
export function createSVGRects (container: Element, target: SVGElement): CenterCenterRect
|
|
37
|
+
|
|
38
|
+
export function getRectLeft (rect: CenterCenterRect): number
|
|
39
|
+
export function getRectTop (rect: CenterCenterRect): number
|
|
40
|
+
export function getRectWidth (rect: CenterCenterRect): number
|
|
41
|
+
export function getRectHeight (rect: CenterCenterRect): number
|
|
42
|
+
|
|
43
|
+
export function getViewportRect (rects: CenterCenterRects): CenterCenterRect
|
|
44
|
+
export function getContainerRect (rects: CenterCenterRects): CenterCenterRect
|
|
45
|
+
export function getTargetRect (rects: CenterCenterRects): CenterCenterRect
|
|
46
|
+
|
|
47
|
+
export function calculateRectX (rect: CenterCenterRect): number
|
|
48
|
+
export function calculateRectY (rect: CenterCenterRect): number
|
|
49
|
+
|
|
50
|
+
export function calculateBoundaryX (rects: CenterCenterRects): number
|
|
51
|
+
export function calculateBoundaryY (rects: CenterCenterRects): number
|
|
52
|
+
|
|
53
|
+
export function calculateContainerX (rects: CenterCenterRects): number
|
|
54
|
+
export function calculateContainerY (rects: CenterCenterRects): number
|
|
55
|
+
|
|
56
|
+
export function calculateTargetX (rects: CenterCenterRects): number
|
|
57
|
+
export function calculateTargetY (rects: CenterCenterRects): number
|
|
@@ -1,100 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @typedef {import('.').ScrollingElement} ScrollingElement
|
|
3
|
-
* @typedef {import('.').CenterCenterRect} CenterCenterRect
|
|
4
|
-
* @typedef {import('.').CenterCenterRects} CenterCenterRects
|
|
2
|
+
* @typedef {import('./index.mjs').ScrollingElement} ScrollingElement
|
|
3
|
+
* @typedef {import('./index.mjs').CenterCenterRect} CenterCenterRect
|
|
4
|
+
* @typedef {import('./index.mjs').CenterCenterRects} CenterCenterRects
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
const leftMap = new WeakMap()
|
|
8
|
-
const topMap = new WeakMap()
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Gets a parent element with `offsetLeft`
|
|
12
|
-
*
|
|
13
|
-
* @param {SVGElement} element
|
|
14
|
-
* @returns {Element}
|
|
15
|
-
*/
|
|
16
|
-
export function getParentElementForOffsetLeft (element) {
|
|
17
|
-
let parentElement = element.parentElement
|
|
18
|
-
|
|
19
|
-
while (!Reflect.has(parentElement, 'offsetLeft')) {
|
|
20
|
-
parentElement = parentElement.parentElement
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return parentElement
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Gets a parent element with `offsetTop`
|
|
28
|
-
*
|
|
29
|
-
* @param {SVGElement} element
|
|
30
|
-
* @returns {Element}
|
|
31
|
-
*/
|
|
32
|
-
export function getParentElementForOffsetTop (element) {
|
|
33
|
-
let parentElement = element.parentElement
|
|
34
|
-
|
|
35
|
-
while (!Reflect.has(parentElement, 'offsetTop')) {
|
|
36
|
-
parentElement = parentElement.parentElement
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return parentElement
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Gets the `offsetLeft` from the element or calculates the equivalent with
|
|
44
|
-
* DOMRect `left` and the nearest `offsetLeft` from a parent
|
|
45
|
-
*
|
|
46
|
-
* @param {Element | SVGElement} element
|
|
47
|
-
* @returns {number}
|
|
48
|
-
*/
|
|
49
|
-
export function getLeft (element) {
|
|
50
|
-
if (Reflect.has(element, 'offsetLeft')) {
|
|
51
|
-
return (
|
|
52
|
-
Reflect.get(element, 'offsetLeft')
|
|
53
|
-
)
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
if (!leftMap.has(element)) leftMap.set(element, getParentElementForOffsetLeft(element))
|
|
57
|
-
const {
|
|
58
|
-
offsetLeft = 0
|
|
59
|
-
} = leftMap.get(element)
|
|
60
|
-
|
|
61
|
-
const {
|
|
62
|
-
left
|
|
63
|
-
} = element.getBoundingClientRect()
|
|
64
|
-
|
|
65
|
-
return (
|
|
66
|
-
offsetLeft - left
|
|
67
|
-
)
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Gets the `offsetTop` from the element or calculates the equivalent with
|
|
72
|
-
* DOMRect `top` and the nearest `offsetTop` from a parent
|
|
73
|
-
*
|
|
74
|
-
* @param {Element | SVGElement} element
|
|
75
|
-
* @returns {number}
|
|
76
|
-
*/
|
|
77
|
-
export function getTop (element) {
|
|
78
|
-
if (Reflect.has(element, 'offsetTop')) {
|
|
79
|
-
return (
|
|
80
|
-
Reflect.get(element, 'offsetTop')
|
|
81
|
-
)
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const {
|
|
85
|
-
top
|
|
86
|
-
} = element.getBoundingClientRect()
|
|
87
|
-
|
|
88
|
-
if (!topMap.has(element)) topMap.set(element, getParentElementForOffsetTop(element))
|
|
89
|
-
const {
|
|
90
|
-
offsetTop = 0
|
|
91
|
-
} = topMap.get(element)
|
|
92
|
-
|
|
93
|
-
return (
|
|
94
|
-
offsetTop - top
|
|
95
|
-
)
|
|
96
|
-
}
|
|
97
|
-
|
|
98
7
|
/**
|
|
99
8
|
* Gets the document `scrollingElement` (or a valid alternative)
|
|
100
9
|
*
|
|
@@ -112,46 +21,6 @@ export function getScrollingElement () {
|
|
|
112
21
|
}
|
|
113
22
|
}
|
|
114
23
|
|
|
115
|
-
/**
|
|
116
|
-
* Destructures the `left` field from a `Rect`
|
|
117
|
-
*
|
|
118
|
-
* @param {CenterCenterRect}
|
|
119
|
-
* @returns {number}
|
|
120
|
-
*/
|
|
121
|
-
export function getRectLeft ({ left }) {
|
|
122
|
-
return left
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Destructures the `top` field from a `Rect`
|
|
127
|
-
*
|
|
128
|
-
* @param {CenterCenterRect}
|
|
129
|
-
* @returns {number}
|
|
130
|
-
*/
|
|
131
|
-
export function getRectTop ({ top }) {
|
|
132
|
-
return top
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Destructures the `width` field from a `Rect`
|
|
137
|
-
*
|
|
138
|
-
* @param {CenterCenterRect}
|
|
139
|
-
* @returns {number}
|
|
140
|
-
*/
|
|
141
|
-
export function getRectWidth ({ width }) {
|
|
142
|
-
return width
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Destructures the `height` field from a `Rect`
|
|
147
|
-
*
|
|
148
|
-
* @param {CenterCenterRect}
|
|
149
|
-
* @returns {number}
|
|
150
|
-
*/
|
|
151
|
-
export function getRectHeight ({ height }) {
|
|
152
|
-
return height
|
|
153
|
-
}
|
|
154
|
-
|
|
155
24
|
/**
|
|
156
25
|
* Creates a `Rect` for the viewport
|
|
157
26
|
*
|
|
@@ -178,30 +47,6 @@ export function createViewportRect () {
|
|
|
178
47
|
}
|
|
179
48
|
}
|
|
180
49
|
|
|
181
|
-
/**
|
|
182
|
-
* Creates a `Rect` for an SVG element
|
|
183
|
-
*
|
|
184
|
-
* @param {Element} element
|
|
185
|
-
* @returns {CenterCenterRect}
|
|
186
|
-
*/
|
|
187
|
-
export function createSVGElementRect (element) {
|
|
188
|
-
const {
|
|
189
|
-
x: left,
|
|
190
|
-
y: top,
|
|
191
|
-
width,
|
|
192
|
-
height
|
|
193
|
-
} = element.getBBox()
|
|
194
|
-
|
|
195
|
-
return {
|
|
196
|
-
left,
|
|
197
|
-
top,
|
|
198
|
-
width,
|
|
199
|
-
height,
|
|
200
|
-
right: (left + width),
|
|
201
|
-
bottom: (top + height)
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
50
|
/**
|
|
206
51
|
* Creates a `Rect` for a DOM element
|
|
207
52
|
*
|
|
@@ -235,15 +80,27 @@ export function createDOMElementRect (element) {
|
|
|
235
80
|
}
|
|
236
81
|
|
|
237
82
|
/**
|
|
238
|
-
* Creates a `Rect` for
|
|
83
|
+
* Creates a `Rect` for an SVG element
|
|
239
84
|
*
|
|
240
|
-
* @param {
|
|
85
|
+
* @param {SVGElement} element
|
|
241
86
|
* @returns {CenterCenterRect}
|
|
242
87
|
*/
|
|
243
|
-
export function
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
88
|
+
export function createSVGElementRect (element) {
|
|
89
|
+
const {
|
|
90
|
+
x: left,
|
|
91
|
+
y: top,
|
|
92
|
+
width,
|
|
93
|
+
height
|
|
94
|
+
} = element.getBBox()
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
left,
|
|
98
|
+
top,
|
|
99
|
+
width,
|
|
100
|
+
height,
|
|
101
|
+
right: (left + width),
|
|
102
|
+
bottom: (top + height)
|
|
103
|
+
}
|
|
247
104
|
}
|
|
248
105
|
|
|
249
106
|
/**
|
|
@@ -253,46 +110,125 @@ export function createElementRect (element) {
|
|
|
253
110
|
* @param {Element} target
|
|
254
111
|
* @returns {CenterCenterRects}
|
|
255
112
|
*/
|
|
256
|
-
export function
|
|
113
|
+
export function createDOMRects (container, target) {
|
|
257
114
|
return {
|
|
258
115
|
viewport: createViewportRect(),
|
|
259
116
|
container: createDOMElementRect(container),
|
|
260
|
-
target:
|
|
117
|
+
target: createDOMElementRect(target)
|
|
261
118
|
}
|
|
262
119
|
}
|
|
263
120
|
|
|
264
121
|
/**
|
|
265
|
-
* Creates `Rects` for the viewport as well as the DOM container and
|
|
122
|
+
* Creates `Rects` for the viewport as well as the DOM container and SVG target elements
|
|
266
123
|
*
|
|
267
124
|
* @param {Element} container
|
|
268
|
-
* @param {
|
|
125
|
+
* @param {SVGElement} target
|
|
269
126
|
* @returns {CenterCenterRects}
|
|
270
127
|
*/
|
|
271
|
-
export function
|
|
128
|
+
export function createSVGRects (container, target) {
|
|
272
129
|
return {
|
|
273
130
|
viewport: createViewportRect(),
|
|
274
131
|
container: createDOMElementRect(container),
|
|
275
|
-
target:
|
|
132
|
+
target: createSVGElementRect(target)
|
|
276
133
|
}
|
|
277
134
|
}
|
|
278
135
|
|
|
279
136
|
/**
|
|
280
|
-
*
|
|
137
|
+
* Destructures the `left` field from a `Rect`
|
|
281
138
|
*
|
|
282
|
-
* @param {
|
|
283
|
-
* @
|
|
284
|
-
|
|
139
|
+
* @param {CenterCenterRect} rect
|
|
140
|
+
* @returns {number}
|
|
141
|
+
*/
|
|
142
|
+
export function getRectLeft ({ left }) {
|
|
143
|
+
return left
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Destructures the `top` field from a `Rect`
|
|
148
|
+
*
|
|
149
|
+
* @param {CenterCenterRect} rect
|
|
150
|
+
* @returns {number}
|
|
151
|
+
*/
|
|
152
|
+
export function getRectTop ({ top }) {
|
|
153
|
+
return top
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Destructures the `width` field from a `Rect`
|
|
158
|
+
*
|
|
159
|
+
* @param {CenterCenterRect} rect
|
|
160
|
+
* @returns {number}
|
|
161
|
+
*/
|
|
162
|
+
export function getRectWidth ({ width }) {
|
|
163
|
+
return width
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Destructures the `height` field from a `Rect`
|
|
168
|
+
*
|
|
169
|
+
* @param {CenterCenterRect} rect
|
|
170
|
+
* @returns {number}
|
|
171
|
+
*/
|
|
172
|
+
export function getRectHeight ({ height }) {
|
|
173
|
+
return height
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Destructures the viewport `Rect`
|
|
178
|
+
*
|
|
179
|
+
* @param {CenterCenterRects} rects
|
|
180
|
+
* @returns {CenterCenterRect}
|
|
181
|
+
*/
|
|
182
|
+
export function getViewportRect ({ viewport }) {
|
|
183
|
+
return viewport
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Destructures the container `Rect`
|
|
188
|
+
*
|
|
189
|
+
* @param {CenterCenterRects} rects
|
|
190
|
+
* @returns {CenterCenterRect}
|
|
191
|
+
*/
|
|
192
|
+
export function getContainerRect ({ container }) {
|
|
193
|
+
return container
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Destructures the target `Rect`
|
|
198
|
+
*
|
|
199
|
+
* @param {CenterCenterRects} rects
|
|
200
|
+
* @returns {CenterCenterRect}
|
|
201
|
+
*/
|
|
202
|
+
export function getTargetRect ({ target }) {
|
|
203
|
+
return target
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Calculates the `x` coordinate of a `Rect`
|
|
208
|
+
*
|
|
209
|
+
* @param {CenterCenterRect}
|
|
210
|
+
* @returns {number}
|
|
211
|
+
*/
|
|
212
|
+
export function calculateRectX ({ left: l, width: w }) {
|
|
213
|
+
return (l + (w / 2))
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Calculates the `y` coordinate of a `Rect`
|
|
218
|
+
*
|
|
219
|
+
* @param {CenterCenterRect}
|
|
220
|
+
* @returns {number}
|
|
285
221
|
*/
|
|
286
|
-
export function
|
|
287
|
-
return (
|
|
288
|
-
createDOMRects(container, target)
|
|
289
|
-
)
|
|
222
|
+
export function calculateRectY ({ top: t, height: h }) {
|
|
223
|
+
return (t + (h / 2))
|
|
290
224
|
}
|
|
291
225
|
|
|
292
226
|
/**
|
|
293
|
-
* Calculates the boundary `x` coordinate of the target in the container
|
|
227
|
+
* Calculates the right-most boundary `x` coordinate of the target in the container
|
|
228
|
+
*
|
|
229
|
+
* The range for target `x` is from left-most 0 to right-most boundary, inclusive
|
|
294
230
|
*
|
|
295
|
-
* @param {CenterCenterRects}
|
|
231
|
+
* @param {CenterCenterRects} rects
|
|
296
232
|
* @returns {number}
|
|
297
233
|
*/
|
|
298
234
|
export function calculateBoundaryX (rects) {
|
|
@@ -303,9 +239,11 @@ export function calculateBoundaryX (rects) {
|
|
|
303
239
|
}
|
|
304
240
|
|
|
305
241
|
/**
|
|
306
|
-
* Calculates the boundary `y` coordinate of the target in the container
|
|
242
|
+
* Calculates the bottom-most boundary `y` coordinate of the target in the container
|
|
307
243
|
*
|
|
308
|
-
*
|
|
244
|
+
* The range for target `y` is from top-most 0 to bottom-most boundary, inclusive
|
|
245
|
+
*
|
|
246
|
+
* @param {CenterCenterRects} rects
|
|
309
247
|
* @returns {number}
|
|
310
248
|
*/
|
|
311
249
|
export function calculateBoundaryY (rects) {
|
|
@@ -318,7 +256,7 @@ export function calculateBoundaryY (rects) {
|
|
|
318
256
|
/**
|
|
319
257
|
* Calculates the `x` coordinate of the container
|
|
320
258
|
*
|
|
321
|
-
* @param {CenterCenterRects}
|
|
259
|
+
* @param {CenterCenterRects} rects
|
|
322
260
|
* @returns {number}
|
|
323
261
|
*/
|
|
324
262
|
export function calculateContainerX ({
|
|
@@ -342,7 +280,7 @@ export function calculateContainerX ({
|
|
|
342
280
|
/**
|
|
343
281
|
* Calculates the `y` coordinate of the container
|
|
344
282
|
*
|
|
345
|
-
* @param {CenterCenterRects}
|
|
283
|
+
* @param {CenterCenterRects} rects
|
|
346
284
|
* @returns {number}
|
|
347
285
|
*/
|
|
348
286
|
export function calculateContainerY ({
|
|
@@ -366,7 +304,7 @@ export function calculateContainerY ({
|
|
|
366
304
|
/**
|
|
367
305
|
* Calculates the `x` coordinate of the target
|
|
368
306
|
*
|
|
369
|
-
* @param {CenterCenterRects}
|
|
307
|
+
* @param {CenterCenterRects} rects
|
|
370
308
|
* @returns {number}
|
|
371
309
|
*/
|
|
372
310
|
export function calculateTargetX (rects) {
|
|
@@ -379,7 +317,7 @@ export function calculateTargetX (rects) {
|
|
|
379
317
|
/**
|
|
380
318
|
* Calculates the `y` coordinate of the target
|
|
381
319
|
*
|
|
382
|
-
* @param {CenterCenterRects}
|
|
320
|
+
* @param {CenterCenterRects} rects
|
|
383
321
|
* @returns {number}
|
|
384
322
|
*/
|
|
385
323
|
export function calculateTargetY (rects) {
|
|
@@ -388,63 +326,3 @@ export function calculateTargetY (rects) {
|
|
|
388
326
|
|
|
389
327
|
return (y - (h / 2))
|
|
390
328
|
}
|
|
391
|
-
|
|
392
|
-
/**
|
|
393
|
-
* Destructures the viewport `Rect`
|
|
394
|
-
*
|
|
395
|
-
* @param {CenterCenterRects}
|
|
396
|
-
* @returns {CenterCenterRect}
|
|
397
|
-
*/
|
|
398
|
-
export function getViewportRect ({ viewport }) {
|
|
399
|
-
return viewport
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
/**
|
|
403
|
-
* Destructures the container `Rect`
|
|
404
|
-
*
|
|
405
|
-
* @param {CenterCenterRects}
|
|
406
|
-
* @returns {CenterCenterRect}
|
|
407
|
-
*/
|
|
408
|
-
export function getContainerRect ({ container }) {
|
|
409
|
-
return container
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
/**
|
|
413
|
-
* Destructures the target `Rect`
|
|
414
|
-
*
|
|
415
|
-
* @param {CenterCenterRects}
|
|
416
|
-
* @returns {CenterCenterRect}
|
|
417
|
-
*/
|
|
418
|
-
export function getTargetRect ({ target }) {
|
|
419
|
-
return target
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
/**
|
|
423
|
-
* Calculates the target `left` position
|
|
424
|
-
*
|
|
425
|
-
* @param {CenterCenterRects}
|
|
426
|
-
* @returns {number}
|
|
427
|
-
*/
|
|
428
|
-
export function calculateLeft (rects) {
|
|
429
|
-
const boundary = calculateBoundaryX(rects)
|
|
430
|
-
const x = calculateTargetX(rects)
|
|
431
|
-
|
|
432
|
-
return (
|
|
433
|
-
Math.max(0, Math.min(boundary, x))
|
|
434
|
-
)
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
/**
|
|
438
|
-
* Calculates the target `top` position
|
|
439
|
-
*
|
|
440
|
-
* @param {CenterCenterRects}
|
|
441
|
-
* @returns {number}
|
|
442
|
-
*/
|
|
443
|
-
export function calculateTop (rects) {
|
|
444
|
-
const boundary = calculateBoundaryY(rects)
|
|
445
|
-
const y = calculateTargetY(rects)
|
|
446
|
-
|
|
447
|
-
return (
|
|
448
|
-
Math.max(0, Math.min(boundary, y))
|
|
449
|
-
)
|
|
450
|
-
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type CenterCenterRects
|
|
3
|
+
} from '#center-center/common'
|
|
4
|
+
|
|
5
|
+
export function createRects (container: Element, target: Element): CenterCenterRects
|
|
6
|
+
export function calculateLeft (rects: CenterCenterRects): number
|
|
7
|
+
export function calculateTop (rects: CenterCenterRects): number
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {import('./common/index.mjs').ScrollingElement} ScrollingElement
|
|
3
|
+
* @typedef {import('./common/index.mjs').CenterCenterRect} CenterCenterRect
|
|
4
|
+
* @typedef {import('./common/index.mjs').CenterCenterRects} CenterCenterRects
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
calculateBoundaryX,
|
|
9
|
+
calculateBoundaryY,
|
|
10
|
+
calculateTargetX,
|
|
11
|
+
calculateTargetY
|
|
12
|
+
} from '#center-center/common'
|
|
13
|
+
|
|
14
|
+
export {
|
|
15
|
+
createDOMRects as createRects
|
|
16
|
+
} from '#center-center/common'
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Calculates the target `left` position
|
|
20
|
+
*
|
|
21
|
+
* @param {CenterCenterRects} rects
|
|
22
|
+
* @returns {number}
|
|
23
|
+
*/
|
|
24
|
+
export function calculateLeft (rects) {
|
|
25
|
+
const boundary = calculateBoundaryX(rects)
|
|
26
|
+
const x = calculateTargetX(rects)
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
Math.max(0, Math.min(boundary, x))
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Calculates the target `top` position
|
|
35
|
+
*
|
|
36
|
+
* @param {CenterCenterRects} rects
|
|
37
|
+
* @returns {number}
|
|
38
|
+
*/
|
|
39
|
+
export function calculateTop (rects) {
|
|
40
|
+
const boundary = calculateBoundaryY(rects)
|
|
41
|
+
const y = calculateTargetY(rects)
|
|
42
|
+
|
|
43
|
+
return (
|
|
44
|
+
Math.max(0, Math.min(boundary, y))
|
|
45
|
+
)
|
|
46
|
+
}
|
package/src/index.d.mts
ADDED
package/src/index.mjs
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type CenterCenterRects
|
|
3
|
+
} from '#center-center/common'
|
|
4
|
+
|
|
5
|
+
export function createRects (container: Element, target: SVGElement): CenterCenterRects
|
|
6
|
+
export function calculateX (rects: CenterCenterRects): number
|
|
7
|
+
export function calculateX (rects: CenterCenterRects): number
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {import('../common/index.mjs').ScrollingElement} ScrollingElement
|
|
3
|
+
* @typedef {import('../common/index.mjs').CenterCenterRect} CenterCenterRect
|
|
4
|
+
* @typedef {import('../common/index.mjs').CenterCenterRects} CenterCenterRects
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
getTargetRect,
|
|
9
|
+
calculateRectX,
|
|
10
|
+
calculateRectY,
|
|
11
|
+
calculateContainerX,
|
|
12
|
+
calculateContainerY,
|
|
13
|
+
getContainerRect,
|
|
14
|
+
getRectWidth,
|
|
15
|
+
getRectHeight
|
|
16
|
+
} from '#center-center/common'
|
|
17
|
+
|
|
18
|
+
export {
|
|
19
|
+
createSVGRects as createRects
|
|
20
|
+
} from '#center-center/common'
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Calculates the target `x` position
|
|
24
|
+
*
|
|
25
|
+
* @param {CenterCenterRects} rects
|
|
26
|
+
* @param {number | undefined} viewBoxW
|
|
27
|
+
* @param {number | undefined} scale
|
|
28
|
+
* @returns {number}
|
|
29
|
+
*/
|
|
30
|
+
export function calculateX (rects, viewBoxW = 0, scale = 1) {
|
|
31
|
+
const targetX = calculateRectX(getTargetRect(rects))
|
|
32
|
+
const containerX = calculateContainerX(rects)
|
|
33
|
+
const w = getRectWidth(getContainerRect(rects))
|
|
34
|
+
const ratio = (viewBoxW / w)
|
|
35
|
+
|
|
36
|
+
return (
|
|
37
|
+
targetX - ((containerX * ratio) / scale)
|
|
38
|
+
)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Calculates the target `y` position
|
|
43
|
+
*
|
|
44
|
+
* @param {CenterCenterRects} rects
|
|
45
|
+
* @param {number | undefined} viewBoxH
|
|
46
|
+
* @param {number | undefined} scale
|
|
47
|
+
* @returns {number}
|
|
48
|
+
*/
|
|
49
|
+
export function calculateY (rects, viewBoxH = 0, scale = 1) {
|
|
50
|
+
const targetY = calculateRectY(getTargetRect(rects))
|
|
51
|
+
const containerY = calculateContainerY(rects)
|
|
52
|
+
const h = getRectHeight(getContainerRect(rects))
|
|
53
|
+
const ratio = (viewBoxH / h)
|
|
54
|
+
|
|
55
|
+
return (
|
|
56
|
+
targetY - ((containerY * ratio) / scale)
|
|
57
|
+
)
|
|
58
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An `<svg />` is of type `Element`
|
|
3
|
+
*
|
|
4
|
+
* Its children are of `SVGElement`
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import debug from 'debug'
|
|
8
|
+
|
|
9
|
+
const log = debug('center-center:view-box')
|
|
10
|
+
|
|
11
|
+
export const X = 0
|
|
12
|
+
export const Y = 1
|
|
13
|
+
export const W = 2
|
|
14
|
+
export const H = 3
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Transforms a string to a round number
|
|
18
|
+
*
|
|
19
|
+
* @param {string} s
|
|
20
|
+
* @returns {number}
|
|
21
|
+
*/
|
|
22
|
+
function toRound (s) {
|
|
23
|
+
return (
|
|
24
|
+
Math.round(Number(s))
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Get the number at position `H` for the SVG element `viewBox` attribute
|
|
30
|
+
*
|
|
31
|
+
* @param {Element} svg
|
|
32
|
+
* @returns {number}
|
|
33
|
+
*/
|
|
34
|
+
export function getViewBoxXFor (svg) {
|
|
35
|
+
return (
|
|
36
|
+
getViewBoxX(getViewBox(svg))
|
|
37
|
+
)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Get the number at position `H` for the SVG element `viewBox` attribute
|
|
42
|
+
*
|
|
43
|
+
* @param {Element} svg
|
|
44
|
+
* @returns {number}
|
|
45
|
+
*/
|
|
46
|
+
export function getViewBoxYFor (svg) {
|
|
47
|
+
return (
|
|
48
|
+
getViewBoxY(getViewBox(svg))
|
|
49
|
+
)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Get the number at position `H` for the SVG element `viewBox` attribute
|
|
54
|
+
*
|
|
55
|
+
* @param {Element} svg
|
|
56
|
+
* @returns {number}
|
|
57
|
+
*/
|
|
58
|
+
export function getViewBoxWFor (svg) {
|
|
59
|
+
return (
|
|
60
|
+
getViewBoxW(getViewBox(svg))
|
|
61
|
+
)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Get the number at position `H` for the SVG element `viewBox` attribute
|
|
66
|
+
*
|
|
67
|
+
* @param {Element} svg
|
|
68
|
+
* @returns {number}
|
|
69
|
+
*/
|
|
70
|
+
export function getViewBoxHFor (svg) {
|
|
71
|
+
return (
|
|
72
|
+
getViewBoxH(getViewBox(svg))
|
|
73
|
+
)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Get the number at position `X`
|
|
78
|
+
*
|
|
79
|
+
* @param {number[]}
|
|
80
|
+
* @returns {number}
|
|
81
|
+
*/
|
|
82
|
+
export function getViewBoxX ({
|
|
83
|
+
[X]: x = 0
|
|
84
|
+
}) {
|
|
85
|
+
return x
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Get the number at position `Y`
|
|
90
|
+
*
|
|
91
|
+
* @param {number[]}
|
|
92
|
+
* @returns {number}
|
|
93
|
+
*/
|
|
94
|
+
export function getViewBoxY ({ [Y]: y = 0 }) {
|
|
95
|
+
return y
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Get the number at position `W`
|
|
100
|
+
*
|
|
101
|
+
* @param {number[]}
|
|
102
|
+
* @returns {number}
|
|
103
|
+
*/
|
|
104
|
+
export function getViewBoxW ({ [W]: w = 0 }) {
|
|
105
|
+
return w
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Get the number at position `H`
|
|
110
|
+
*
|
|
111
|
+
* @param {number[]}
|
|
112
|
+
* @returns {number}
|
|
113
|
+
*/
|
|
114
|
+
export function getViewBoxH ({ [H]: h = 0 }) {
|
|
115
|
+
return h
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Get an SVG element `viewBox` attribute as an array of numbers
|
|
120
|
+
*
|
|
121
|
+
* @param {Element} svg
|
|
122
|
+
* @returns {number[]}
|
|
123
|
+
*/
|
|
124
|
+
export default function getViewBox (svg) {
|
|
125
|
+
log('getViewBox')
|
|
126
|
+
|
|
127
|
+
const viewBox = (
|
|
128
|
+
svg
|
|
129
|
+
.getAttribute('viewBox')
|
|
130
|
+
.split(String.fromCodePoint(32))
|
|
131
|
+
.map(toRound) // .map(Number).map(Math.round)
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
log(viewBox)
|
|
135
|
+
|
|
136
|
+
return viewBox
|
|
137
|
+
}
|
package/index.d.mts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
export function getParentElementForOffsetLeft (element: SVGElement): Element
|
|
2
|
-
export function getParentElementForOffsetTop (element: SVGElement): Element
|
|
3
|
-
export function getLeft (element: Element | SVGElement): number
|
|
4
|
-
export function getTop (element: Element | SVGElement): number
|
|
5
|
-
export function getScrollingElement (): ScrollingElement
|
|
6
|
-
export function getViewportRect (): CenterCenterRect
|
|
7
|
-
export function getDOMElementRect (element: Element): CenterCenterRect
|
|
8
|
-
export function getSVGElementRect (element: SVGElement): CenterCenterRect
|
|
9
|
-
export function getElementRect (element: Element | SVGElement): CenterCenterRect
|
|
10
|
-
export function getDOMRects (container: Element, target: Element): CenterCenterRects
|
|
11
|
-
export function getSVGRects (container: Element, target: SVGElement): CenterCenterRects
|
|
12
|
-
export function getRects (container: Element, target: Element | SVGElement): CenterCenterRects
|
|
13
|
-
export function calculateLeft (rects: CenterCenterRects): number
|
|
14
|
-
export function calculateTop (rects: CenterCenterRects): number
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* The document `scrollingElement`
|
|
18
|
-
*/
|
|
19
|
-
export interface ScrollingElement {
|
|
20
|
-
scrollLeft: number
|
|
21
|
-
scrollTop: number
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* A Center Center `Rect`
|
|
26
|
-
*/
|
|
27
|
-
export interface CenterCenterRect {
|
|
28
|
-
left: number
|
|
29
|
-
top: number
|
|
30
|
-
width: number
|
|
31
|
-
height: number
|
|
32
|
-
right: number
|
|
33
|
-
bottom: number
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Center Center `Rects`
|
|
38
|
-
*/
|
|
39
|
-
export interface CenterCenterRects {
|
|
40
|
-
viewport: CenterCenterRect
|
|
41
|
-
container: CenterCenterRect
|
|
42
|
-
target: CenterCenterRect
|
|
43
|
-
}
|