center-center 0.0.41 → 0.0.44
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 +6 -3
- 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} +115 -183
- package/src/dom/index.d.mts +7 -0
- package/src/dom/index.mjs +44 -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 +56 -0
- package/index.d.mts +0 -43
- /package/src/{view-box → svg/view-box}/index.mjs +0 -0
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>
|
|
@@ -15,7 +18,7 @@
|
|
|
15
18
|
import debug from 'debug'
|
|
16
19
|
|
|
17
20
|
import {
|
|
18
|
-
|
|
21
|
+
createRects,
|
|
19
22
|
calculateLeft,
|
|
20
23
|
calculateTop
|
|
21
24
|
} from 'center-center'
|
|
@@ -73,7 +76,7 @@
|
|
|
73
76
|
log('render')
|
|
74
77
|
|
|
75
78
|
window.requestAnimationFrame(() => {
|
|
76
|
-
const rects =
|
|
79
|
+
const rects = createRects(container, target)
|
|
77
80
|
const left = calculateLeft(rects)
|
|
78
81
|
const top = calculateTop(rects)
|
|
79
82
|
|
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.44",
|
|
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,7 +1,7 @@
|
|
|
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
7
|
/**
|
|
@@ -21,46 +21,6 @@ export function getScrollingElement () {
|
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
/**
|
|
25
|
-
* Destructures the `left` field from a `Rect`
|
|
26
|
-
*
|
|
27
|
-
* @param {CenterCenterRect}
|
|
28
|
-
* @returns {number}
|
|
29
|
-
*/
|
|
30
|
-
export function getRectLeft ({ left }) {
|
|
31
|
-
return left
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Destructures the `top` field from a `Rect`
|
|
36
|
-
*
|
|
37
|
-
* @param {CenterCenterRect}
|
|
38
|
-
* @returns {number}
|
|
39
|
-
*/
|
|
40
|
-
export function getRectTop ({ top }) {
|
|
41
|
-
return top
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Destructures the `width` field from a `Rect`
|
|
46
|
-
*
|
|
47
|
-
* @param {CenterCenterRect}
|
|
48
|
-
* @returns {number}
|
|
49
|
-
*/
|
|
50
|
-
export function getRectWidth ({ width }) {
|
|
51
|
-
return width
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Destructures the `height` field from a `Rect`
|
|
56
|
-
*
|
|
57
|
-
* @param {CenterCenterRect}
|
|
58
|
-
* @returns {number}
|
|
59
|
-
*/
|
|
60
|
-
export function getRectHeight ({ height }) {
|
|
61
|
-
return height
|
|
62
|
-
}
|
|
63
|
-
|
|
64
24
|
/**
|
|
65
25
|
* Creates a `Rect` for the viewport
|
|
66
26
|
*
|
|
@@ -88,18 +48,26 @@ export function createViewportRect () {
|
|
|
88
48
|
}
|
|
89
49
|
|
|
90
50
|
/**
|
|
91
|
-
* Creates a `Rect` for
|
|
51
|
+
* Creates a `Rect` for a DOM element
|
|
92
52
|
*
|
|
93
|
-
* @param {
|
|
53
|
+
* @param {Element} element
|
|
94
54
|
* @returns {CenterCenterRect}
|
|
95
55
|
*/
|
|
96
|
-
export function
|
|
56
|
+
export function createDOMElementRect (element) {
|
|
97
57
|
const {
|
|
98
|
-
|
|
99
|
-
|
|
58
|
+
scrollLeft,
|
|
59
|
+
scrollTop
|
|
60
|
+
} = getScrollingElement()
|
|
61
|
+
|
|
62
|
+
const {
|
|
63
|
+
x,
|
|
64
|
+
y,
|
|
100
65
|
width,
|
|
101
66
|
height
|
|
102
|
-
} = element.
|
|
67
|
+
} = element.getBoundingClientRect()
|
|
68
|
+
|
|
69
|
+
const left = (scrollLeft + x)
|
|
70
|
+
const top = (scrollTop + y)
|
|
103
71
|
|
|
104
72
|
return {
|
|
105
73
|
left,
|
|
@@ -112,26 +80,18 @@ export function createSVGElementRect (element) {
|
|
|
112
80
|
}
|
|
113
81
|
|
|
114
82
|
/**
|
|
115
|
-
* Creates a `Rect` for
|
|
83
|
+
* Creates a `Rect` for an SVG element
|
|
116
84
|
*
|
|
117
|
-
* @param {
|
|
85
|
+
* @param {SVGElement} element
|
|
118
86
|
* @returns {CenterCenterRect}
|
|
119
87
|
*/
|
|
120
|
-
export function
|
|
121
|
-
const {
|
|
122
|
-
scrollLeft,
|
|
123
|
-
scrollTop
|
|
124
|
-
} = getScrollingElement()
|
|
125
|
-
|
|
88
|
+
export function createSVGElementRect (element) {
|
|
126
89
|
const {
|
|
127
|
-
x,
|
|
128
|
-
y,
|
|
90
|
+
x: left,
|
|
91
|
+
y: top,
|
|
129
92
|
width,
|
|
130
93
|
height
|
|
131
|
-
} = element.
|
|
132
|
-
|
|
133
|
-
const left = (scrollLeft + x)
|
|
134
|
-
const top = (scrollTop + y)
|
|
94
|
+
} = element.getBBox()
|
|
135
95
|
|
|
136
96
|
return {
|
|
137
97
|
left,
|
|
@@ -144,63 +104,103 @@ export function createDOMElementRect (element) {
|
|
|
144
104
|
}
|
|
145
105
|
|
|
146
106
|
/**
|
|
147
|
-
* Creates `Rects` for the viewport as well as the DOM container and
|
|
107
|
+
* Creates `Rects` for the viewport as well as the DOM container and DOM target elements
|
|
148
108
|
*
|
|
149
109
|
* @param {Element} container
|
|
150
|
-
* @param {
|
|
110
|
+
* @param {Element} target
|
|
151
111
|
* @returns {CenterCenterRects}
|
|
152
112
|
*/
|
|
153
|
-
export function
|
|
113
|
+
export function createDOMRects (container, target) {
|
|
154
114
|
return {
|
|
155
115
|
viewport: createViewportRect(),
|
|
156
116
|
container: createDOMElementRect(container),
|
|
157
|
-
target:
|
|
117
|
+
target: createDOMElementRect(target)
|
|
158
118
|
}
|
|
159
119
|
}
|
|
160
120
|
|
|
161
121
|
/**
|
|
162
|
-
* 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
|
|
163
123
|
*
|
|
164
124
|
* @param {Element} container
|
|
165
|
-
* @param {
|
|
125
|
+
* @param {SVGElement} target
|
|
166
126
|
* @returns {CenterCenterRects}
|
|
167
127
|
*/
|
|
168
|
-
export function
|
|
128
|
+
export function createSVGRects (container, target) {
|
|
169
129
|
return {
|
|
170
130
|
viewport: createViewportRect(),
|
|
171
131
|
container: createDOMElementRect(container),
|
|
172
|
-
target:
|
|
132
|
+
target: createSVGElementRect(target)
|
|
173
133
|
}
|
|
174
134
|
}
|
|
175
135
|
|
|
176
136
|
/**
|
|
177
|
-
*
|
|
137
|
+
* Destructures the `left` field from a `Rect`
|
|
178
138
|
*
|
|
179
|
-
*
|
|
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`
|
|
180
148
|
*
|
|
181
|
-
* @param {
|
|
149
|
+
* @param {CenterCenterRect} rect
|
|
182
150
|
* @returns {number}
|
|
183
151
|
*/
|
|
184
|
-
export function
|
|
185
|
-
|
|
186
|
-
|
|
152
|
+
export function getRectTop ({ top }) {
|
|
153
|
+
return top
|
|
154
|
+
}
|
|
187
155
|
|
|
188
|
-
|
|
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
|
|
189
164
|
}
|
|
190
165
|
|
|
191
166
|
/**
|
|
192
|
-
*
|
|
167
|
+
* Destructures the `height` field from a `Rect`
|
|
193
168
|
*
|
|
194
|
-
*
|
|
169
|
+
* @param {CenterCenterRect} rect
|
|
170
|
+
* @returns {number}
|
|
171
|
+
*/
|
|
172
|
+
export function getRectHeight ({ height }) {
|
|
173
|
+
return height
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Destructures the viewport `Rect`
|
|
195
178
|
*
|
|
196
179
|
* @param {CenterCenterRects} rects
|
|
197
|
-
* @returns {
|
|
180
|
+
* @returns {CenterCenterRect}
|
|
198
181
|
*/
|
|
199
|
-
export function
|
|
200
|
-
|
|
201
|
-
|
|
182
|
+
export function getViewportRect ({ viewport }) {
|
|
183
|
+
return viewport
|
|
184
|
+
}
|
|
202
185
|
|
|
203
|
-
|
|
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
204
|
}
|
|
205
205
|
|
|
206
206
|
/**
|
|
@@ -223,6 +223,36 @@ export function calculateRectY ({ top: t, height: h }) {
|
|
|
223
223
|
return (t + (h / 2))
|
|
224
224
|
}
|
|
225
225
|
|
|
226
|
+
/**
|
|
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
|
|
230
|
+
*
|
|
231
|
+
* @param {CenterCenterRects} rects
|
|
232
|
+
* @returns {number}
|
|
233
|
+
*/
|
|
234
|
+
export function calculateBoundaryX (rects) {
|
|
235
|
+
const containerW = getRectWidth(getContainerRect(rects))
|
|
236
|
+
const targetW = getRectWidth(getTargetRect(rects))
|
|
237
|
+
|
|
238
|
+
return (containerW - targetW)
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Calculates the bottom-most boundary `y` coordinate of the target in the container
|
|
243
|
+
*
|
|
244
|
+
* The range for target `y` is from top-most 0 to bottom-most boundary, inclusive
|
|
245
|
+
*
|
|
246
|
+
* @param {CenterCenterRects} rects
|
|
247
|
+
* @returns {number}
|
|
248
|
+
*/
|
|
249
|
+
export function calculateBoundaryY (rects) {
|
|
250
|
+
const containerH = getRectHeight(getContainerRect(rects))
|
|
251
|
+
const targetH = getRectHeight(getTargetRect(rects))
|
|
252
|
+
|
|
253
|
+
return (containerH - targetH)
|
|
254
|
+
}
|
|
255
|
+
|
|
226
256
|
/**
|
|
227
257
|
* Calculates the `x` coordinate of the container
|
|
228
258
|
*
|
|
@@ -296,101 +326,3 @@ export function calculateTargetY (rects) {
|
|
|
296
326
|
|
|
297
327
|
return (y - (h / 2))
|
|
298
328
|
}
|
|
299
|
-
|
|
300
|
-
/**
|
|
301
|
-
* Destructures the viewport `Rect`
|
|
302
|
-
*
|
|
303
|
-
* @param {CenterCenterRects} rects
|
|
304
|
-
* @returns {CenterCenterRect}
|
|
305
|
-
*/
|
|
306
|
-
export function getViewportRect ({ viewport }) {
|
|
307
|
-
return viewport
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
/**
|
|
311
|
-
* Destructures the container `Rect`
|
|
312
|
-
*
|
|
313
|
-
* @param {CenterCenterRects} rects
|
|
314
|
-
* @returns {CenterCenterRect}
|
|
315
|
-
*/
|
|
316
|
-
export function getContainerRect ({ container }) {
|
|
317
|
-
return container
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
/**
|
|
321
|
-
* Destructures the target `Rect`
|
|
322
|
-
*
|
|
323
|
-
* @param {CenterCenterRects} rects
|
|
324
|
-
* @returns {CenterCenterRect}
|
|
325
|
-
*/
|
|
326
|
-
export function getTargetRect ({ target }) {
|
|
327
|
-
return target
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
/**
|
|
331
|
-
* Calculates the target `x` position
|
|
332
|
-
*
|
|
333
|
-
* @param {CenterCenterRects} rects
|
|
334
|
-
* @param {number | undefined} viewBoxW
|
|
335
|
-
* @param {number | undefined} scale
|
|
336
|
-
* @returns {number}
|
|
337
|
-
*/
|
|
338
|
-
export function calculateX (rects, viewBoxW = 0, scale = 1) {
|
|
339
|
-
const targetX = calculateRectX(getTargetRect(rects))
|
|
340
|
-
const containerX = calculateContainerX(rects)
|
|
341
|
-
const w = getRectWidth(getContainerRect(rects))
|
|
342
|
-
const ratio = (viewBoxW / w)
|
|
343
|
-
|
|
344
|
-
return (
|
|
345
|
-
targetX - ((containerX * ratio) / scale)
|
|
346
|
-
)
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
/**
|
|
350
|
-
* Calculates the target `y` position
|
|
351
|
-
*
|
|
352
|
-
* @param {CenterCenterRects} rects
|
|
353
|
-
* @param {number | undefined} viewBoxH
|
|
354
|
-
* @param {number | undefined} scale
|
|
355
|
-
* @returns {number}
|
|
356
|
-
*/
|
|
357
|
-
export function calculateY (rects, viewBoxH = 0, scale = 1) {
|
|
358
|
-
const targetY = calculateRectY(getTargetRect(rects))
|
|
359
|
-
const containerY = calculateContainerY(rects)
|
|
360
|
-
const h = getRectHeight(getContainerRect(rects))
|
|
361
|
-
const ratio = (viewBoxH / h)
|
|
362
|
-
|
|
363
|
-
return (
|
|
364
|
-
targetY - ((containerY * ratio) / scale)
|
|
365
|
-
)
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
/**
|
|
369
|
-
* Calculates the target `left` position
|
|
370
|
-
*
|
|
371
|
-
* @param {CenterCenterRects} rects
|
|
372
|
-
* @returns {number}
|
|
373
|
-
*/
|
|
374
|
-
export function calculateLeft (rects) {
|
|
375
|
-
const boundary = calculateBoundaryX(rects)
|
|
376
|
-
const x = calculateTargetX(rects)
|
|
377
|
-
|
|
378
|
-
return (
|
|
379
|
-
Math.max(0, Math.min(boundary, x))
|
|
380
|
-
)
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
/**
|
|
384
|
-
* Calculates the target `top` position
|
|
385
|
-
*
|
|
386
|
-
* @param {CenterCenterRects} rects
|
|
387
|
-
* @returns {number}
|
|
388
|
-
*/
|
|
389
|
-
export function calculateTop (rects) {
|
|
390
|
-
const boundary = calculateBoundaryY(rects)
|
|
391
|
-
const y = calculateTargetY(rects)
|
|
392
|
-
|
|
393
|
-
return (
|
|
394
|
-
Math.max(0, Math.min(boundary, y))
|
|
395
|
-
)
|
|
396
|
-
}
|
|
@@ -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,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {import('../common/index.mjs').CenterCenterRects} CenterCenterRects
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
calculateBoundaryX,
|
|
7
|
+
calculateBoundaryY,
|
|
8
|
+
calculateTargetX,
|
|
9
|
+
calculateTargetY
|
|
10
|
+
} from '#center-center/common'
|
|
11
|
+
|
|
12
|
+
export {
|
|
13
|
+
createDOMRects as createRects
|
|
14
|
+
} from '#center-center/common'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Calculates the target `left` position
|
|
18
|
+
*
|
|
19
|
+
* @param {CenterCenterRects} rects
|
|
20
|
+
* @returns {number}
|
|
21
|
+
*/
|
|
22
|
+
export function calculateLeft (rects) {
|
|
23
|
+
const boundary = calculateBoundaryX(rects)
|
|
24
|
+
const x = calculateTargetX(rects)
|
|
25
|
+
|
|
26
|
+
return (
|
|
27
|
+
Math.max(0, Math.min(boundary, x))
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Calculates the target `top` position
|
|
33
|
+
*
|
|
34
|
+
* @param {CenterCenterRects} rects
|
|
35
|
+
* @returns {number}
|
|
36
|
+
*/
|
|
37
|
+
export function calculateTop (rects) {
|
|
38
|
+
const boundary = calculateBoundaryY(rects)
|
|
39
|
+
const y = calculateTargetY(rects)
|
|
40
|
+
|
|
41
|
+
return (
|
|
42
|
+
Math.max(0, Math.min(boundary, y))
|
|
43
|
+
)
|
|
44
|
+
}
|
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,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {import('../common/index.mjs').CenterCenterRects} CenterCenterRects
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
getTargetRect,
|
|
7
|
+
calculateRectX,
|
|
8
|
+
calculateRectY,
|
|
9
|
+
calculateContainerX,
|
|
10
|
+
calculateContainerY,
|
|
11
|
+
getContainerRect,
|
|
12
|
+
getRectWidth,
|
|
13
|
+
getRectHeight
|
|
14
|
+
} from '#center-center/common'
|
|
15
|
+
|
|
16
|
+
export {
|
|
17
|
+
createSVGRects as createRects
|
|
18
|
+
} from '#center-center/common'
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Calculates the target `x` position
|
|
22
|
+
*
|
|
23
|
+
* @param {CenterCenterRects} rects
|
|
24
|
+
* @param {number | undefined} viewBoxW
|
|
25
|
+
* @param {number | undefined} scale
|
|
26
|
+
* @returns {number}
|
|
27
|
+
*/
|
|
28
|
+
export function calculateX (rects, viewBoxW = 0, scale = 1) {
|
|
29
|
+
const targetX = calculateRectX(getTargetRect(rects))
|
|
30
|
+
const containerX = calculateContainerX(rects)
|
|
31
|
+
const w = getRectWidth(getContainerRect(rects))
|
|
32
|
+
const ratio = (viewBoxW / w)
|
|
33
|
+
|
|
34
|
+
return (
|
|
35
|
+
targetX - ((containerX * ratio) / scale)
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Calculates the target `y` position
|
|
41
|
+
*
|
|
42
|
+
* @param {CenterCenterRects} rects
|
|
43
|
+
* @param {number | undefined} viewBoxH
|
|
44
|
+
* @param {number | undefined} scale
|
|
45
|
+
* @returns {number}
|
|
46
|
+
*/
|
|
47
|
+
export function calculateY (rects, viewBoxH = 0, scale = 1) {
|
|
48
|
+
const targetY = calculateRectY(getTargetRect(rects))
|
|
49
|
+
const containerY = calculateContainerY(rects)
|
|
50
|
+
const h = getRectHeight(getContainerRect(rects))
|
|
51
|
+
const ratio = (viewBoxH / h)
|
|
52
|
+
|
|
53
|
+
return (
|
|
54
|
+
targetY - ((containerY * ratio) / scale)
|
|
55
|
+
)
|
|
56
|
+
}
|
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
|
-
}
|
|
File without changes
|