center-center 0.0.40 → 0.0.41

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 CHANGED
@@ -15,7 +15,7 @@
15
15
  import debug from 'debug'
16
16
 
17
17
  import {
18
- createRects,
18
+ createDOMRects,
19
19
  calculateLeft,
20
20
  calculateTop
21
21
  } from 'center-center'
@@ -73,7 +73,7 @@
73
73
  log('render')
74
74
 
75
75
  window.requestAnimationFrame(() => {
76
- const rects = createRects(container, target)
76
+ const rects = createDOMRects(container, target)
77
77
  const left = calculateLeft(rects)
78
78
  const top = calculateTop(rects)
79
79
 
package/index.mjs CHANGED
@@ -4,97 +4,6 @@
4
4
  * @typedef {import('.').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
  *
@@ -181,7 +90,7 @@ export function createViewportRect () {
181
90
  /**
182
91
  * Creates a `Rect` for an SVG element
183
92
  *
184
- * @param {Element} element
93
+ * @param {SVGElement} element
185
94
  * @returns {CenterCenterRect}
186
95
  */
187
96
  export function createSVGElementRect (element) {
@@ -235,22 +144,10 @@ export function createDOMElementRect (element) {
235
144
  }
236
145
 
237
146
  /**
238
- * Creates a `Rect` for a DOM element
239
- *
240
- * @param {Element} element
241
- * @returns {CenterCenterRect}
242
- */
243
- export function createElementRect (element) {
244
- return (
245
- createDOMElementRect(element)
246
- )
247
- }
248
-
249
- /**
250
- * Creates `Rects` for the viewport as well as the DOM container and DOM target elements
147
+ * Creates `Rects` for the viewport as well as the DOM container and SVG target elements
251
148
  *
252
149
  * @param {Element} container
253
- * @param {Element} target
150
+ * @param {SVGElement} target
254
151
  * @returns {CenterCenterRects}
255
152
  */
256
153
  export function createSVGRects (container, target) {
@@ -277,22 +174,11 @@ export function createDOMRects (container, target) {
277
174
  }
278
175
 
279
176
  /**
280
- * Creates `Rects` for the viewport as well as the container and target elements
177
+ * Calculates the right-most boundary `x` coordinate of the target in the container
281
178
  *
282
- * @param {Element} container
283
- * @param {Element} target
284
- * @returns {CenterCenterRects}
285
- */
286
- export function createRects (container, target) {
287
- return (
288
- createDOMRects(container, target)
289
- )
290
- }
291
-
292
- /**
293
- * Calculates the boundary `x` coordinate of the target in the container
179
+ * The range for target `x` is from left-most 0 to right-most boundary, inclusive
294
180
  *
295
- * @param {CenterCenterRects}
181
+ * @param {CenterCenterRects} rects
296
182
  * @returns {number}
297
183
  */
298
184
  export function calculateBoundaryX (rects) {
@@ -303,9 +189,11 @@ export function calculateBoundaryX (rects) {
303
189
  }
304
190
 
305
191
  /**
306
- * Calculates the boundary `y` coordinate of the target in the container
192
+ * Calculates the bottom-most boundary `y` coordinate of the target in the container
193
+ *
194
+ * The range for target `y` is from top-most 0 to bottom-most boundary, inclusive
307
195
  *
308
- * @param {CenterCenterRects}
196
+ * @param {CenterCenterRects} rects
309
197
  * @returns {number}
310
198
  */
311
199
  export function calculateBoundaryY (rects) {
@@ -315,10 +203,30 @@ export function calculateBoundaryY (rects) {
315
203
  return (containerH - targetH)
316
204
  }
317
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}
221
+ */
222
+ export function calculateRectY ({ top: t, height: h }) {
223
+ return (t + (h / 2))
224
+ }
225
+
318
226
  /**
319
227
  * Calculates the `x` coordinate of the container
320
228
  *
321
- * @param {CenterCenterRects}
229
+ * @param {CenterCenterRects} rects
322
230
  * @returns {number}
323
231
  */
324
232
  export function calculateContainerX ({
@@ -342,7 +250,7 @@ export function calculateContainerX ({
342
250
  /**
343
251
  * Calculates the `y` coordinate of the container
344
252
  *
345
- * @param {CenterCenterRects}
253
+ * @param {CenterCenterRects} rects
346
254
  * @returns {number}
347
255
  */
348
256
  export function calculateContainerY ({
@@ -366,7 +274,7 @@ export function calculateContainerY ({
366
274
  /**
367
275
  * Calculates the `x` coordinate of the target
368
276
  *
369
- * @param {CenterCenterRects}
277
+ * @param {CenterCenterRects} rects
370
278
  * @returns {number}
371
279
  */
372
280
  export function calculateTargetX (rects) {
@@ -379,7 +287,7 @@ export function calculateTargetX (rects) {
379
287
  /**
380
288
  * Calculates the `y` coordinate of the target
381
289
  *
382
- * @param {CenterCenterRects}
290
+ * @param {CenterCenterRects} rects
383
291
  * @returns {number}
384
292
  */
385
293
  export function calculateTargetY (rects) {
@@ -392,7 +300,7 @@ export function calculateTargetY (rects) {
392
300
  /**
393
301
  * Destructures the viewport `Rect`
394
302
  *
395
- * @param {CenterCenterRects}
303
+ * @param {CenterCenterRects} rects
396
304
  * @returns {CenterCenterRect}
397
305
  */
398
306
  export function getViewportRect ({ viewport }) {
@@ -402,7 +310,7 @@ export function getViewportRect ({ viewport }) {
402
310
  /**
403
311
  * Destructures the container `Rect`
404
312
  *
405
- * @param {CenterCenterRects}
313
+ * @param {CenterCenterRects} rects
406
314
  * @returns {CenterCenterRect}
407
315
  */
408
316
  export function getContainerRect ({ container }) {
@@ -412,17 +320,55 @@ export function getContainerRect ({ container }) {
412
320
  /**
413
321
  * Destructures the target `Rect`
414
322
  *
415
- * @param {CenterCenterRects}
323
+ * @param {CenterCenterRects} rects
416
324
  * @returns {CenterCenterRect}
417
325
  */
418
326
  export function getTargetRect ({ target }) {
419
327
  return target
420
328
  }
421
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
+
422
368
  /**
423
369
  * Calculates the target `left` position
424
370
  *
425
- * @param {CenterCenterRects}
371
+ * @param {CenterCenterRects} rects
426
372
  * @returns {number}
427
373
  */
428
374
  export function calculateLeft (rects) {
@@ -437,7 +383,7 @@ export function calculateLeft (rects) {
437
383
  /**
438
384
  * Calculates the target `top` position
439
385
  *
440
- * @param {CenterCenterRects}
386
+ * @param {CenterCenterRects} rects
441
387
  * @returns {number}
442
388
  */
443
389
  export function calculateTop (rects) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "center-center",
3
- "version": "0.0.40",
3
+ "version": "0.0.41",
4
4
  "description": "Position an element at the center center of a container",
5
5
  "main": "./index.mjs",
6
6
  "type": "module",
@@ -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
+ }