@zumer/orbit 1.4.5 → 1.4.10

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/orbit.js CHANGED
@@ -1,27 +1,14 @@
1
-
2
1
  /*
3
2
  * orbit
4
- * v.1.4.5
3
+ * v.1.4.10
5
4
  * Author Juan Martin Muda - Zumerlab
6
5
  * License MIT
7
- **/
8
-
6
+ */
9
7
  (() => {
10
8
  // src/js/orbit-base.js
11
9
  var OrbitBase = class extends HTMLElement {
12
10
  constructor() {
13
11
  super();
14
- this.commonProperties = {
15
- orbitRadius: 0,
16
- arcHeight: 0,
17
- realRadius: 0,
18
- arcAngle: 0,
19
- shape: "none",
20
- arcHeightPercentage: 0,
21
- orbitNumber: 1,
22
- size: 1,
23
- strokeWidth: 1
24
- };
25
12
  }
26
13
  getCommonAttributes(element) {
27
14
  const orbitRadius = parseFloat(getComputedStyle(element).getPropertyValue("r") || 0);
@@ -30,7 +17,7 @@
30
17
  const strokeWidth = parseFloat(getComputedStyle(element).getPropertyValue("--o-stroke-width") || 1);
31
18
  const shape = element.getAttribute("shape") || "none";
32
19
  const arcHeight = orbitRadius / orbitNumber * size - strokeWidth + 0.3;
33
- const arcHeightPercentage = arcHeight / 2 * 100 / orbitRadius / 2;
20
+ const arcHeightPercentage = orbitRadius > 0 ? arcHeight / 2 * 100 / orbitRadius / 2 : 0;
34
21
  let innerOuter = 0;
35
22
  if (element.classList.contains("outer-orbit")) {
36
23
  innerOuter = arcHeightPercentage;
@@ -158,15 +145,11 @@
158
145
  const Q2 = this.getControlPoint(newInnerStart.x, newInnerStart.y, innerPointStart.x, innerPointStart.y);
159
146
  const Q3 = this.getControlPoint(upperPointStart.x, upperPointStart.y, newUpperStart.x, newUpperStart.y);
160
147
  let d = `M ${newUpperStart.x},${newUpperStart.y} A ${bigRadius},${bigRadius} 0 ${largeArcFlag} 1 ${newUpperEnd.x},${newUpperEnd.y}`;
161
- d += `Q ${Q.xc}, ${Q.yc} ${upperPointEnd.x} ${upperPointEnd.y} `;
162
- d += `L ${upperPointEnd.x} ${upperPointEnd.y}`;
163
- d += `L ${innerPointEnd.x} ${innerPointEnd.y}`;
164
- d += `Q ${Q1.xc}, ${Q1.yc} ${newInnerEnd.x} ${newInnerEnd.y} `;
148
+ d += `Q ${Q.xc},${Q.yc} ${upperPointEnd.x},${upperPointEnd.y} L ${innerPointEnd.x},${innerPointEnd.y}`;
149
+ d += `Q ${Q1.xc},${Q1.yc} ${newInnerEnd.x},${newInnerEnd.y}`;
165
150
  d += `A ${smallRadius},${smallRadius} 0 ${largeArcFlag} 0 ${newInnerStart.x},${newInnerStart.y}`;
166
- d += `Q ${Q2.xc}, ${Q2.yc} ${innerPointStart.x} ${innerPointStart.y} `;
167
- d += `L ${innerPointStart.x} ${innerPointStart.y}`;
168
- d += `L ${upperPointStart.x} ${upperPointStart.y}`;
169
- d += ` Q ${Q3.xc}, ${Q3.yc} ${newUpperStart.x} ${newUpperStart.y} `;
151
+ d += `Q ${Q2.xc},${Q2.yc} ${innerPointStart.x},${innerPointStart.y} L ${upperPointStart.x},${upperPointStart.y}`;
152
+ d += ` Q ${Q3.xc},${Q3.yc} ${newUpperStart.x},${newUpperStart.y}`;
170
153
  d += ` Z`;
171
154
  return d;
172
155
  }
@@ -220,12 +203,13 @@
220
203
  }
221
204
  generateZigzagPath(params, arcHeight, orbitNumber) {
222
205
  const { upperArcStart, upperArcEnd, innerArcStart, innerArcEnd, bigRadius, smallRadius, largeArcFlag, radius } = params;
223
- const s2 = this.arcPoint(radius, params.upperAngleStart, -arcHeight / orbitNumber / 2, 3);
224
- const s3 = this.arcPoint(radius, params.upperAngleStart, 0 / orbitNumber / 2, 0);
225
- const s4 = this.arcPoint(radius, params.upperAngleStart, arcHeight / orbitNumber / 2, 3);
226
- const e2 = this.arcPoint(radius, params.innerAngleEnd, arcHeight / orbitNumber / 2, 3);
227
- const e3 = this.arcPoint(radius, params.innerAngleEnd, 0 / orbitNumber / 2, 0);
228
- const e4 = this.arcPoint(radius, params.innerAngleEnd, -arcHeight / orbitNumber / 2, 3);
206
+ const h2 = arcHeight / orbitNumber / 2;
207
+ const s2 = this.arcPoint(radius, params.upperAngleStart, -h2, 3);
208
+ const s3 = this.arcPoint(radius, params.upperAngleStart, 0, 0);
209
+ const s4 = this.arcPoint(radius, params.upperAngleStart, h2, 3);
210
+ const e2 = this.arcPoint(radius, params.innerAngleEnd, h2, 3);
211
+ const e3 = this.arcPoint(radius, params.innerAngleEnd, 0, 0);
212
+ const e4 = this.arcPoint(radius, params.innerAngleEnd, -h2, 3);
229
213
  let d = `M ${upperArcStart.x},${upperArcStart.y} A ${bigRadius},${bigRadius} 0 ${largeArcFlag} 1 ${upperArcEnd.x},${upperArcEnd.y}`;
230
214
  d += `L ${e2.x} ${e2.y}`;
231
215
  d += `L ${e3.x} ${e3.y}`;
@@ -312,14 +296,11 @@
312
296
  this.observer.observe(this, { attributes: true, childList: true });
313
297
  }
314
298
  update() {
315
- const progressBg = this.shadowRoot.querySelector(".progress-bg");
316
- const progressBar = this.shadowRoot.querySelector(".progress-bar");
317
- this.updateArc(progressBg, true);
318
- this.updateArc(progressBar, false);
319
- }
320
- updateArc(arc, full) {
321
- const { d } = this.calculateArcParameters(full);
322
- arc.setAttribute("d", d);
299
+ const attrs = this.getAttributes();
300
+ const dBg = this.calculateArcParameters(attrs, true);
301
+ const dBar = this.calculateArcParameters(attrs, false);
302
+ this.shadowRoot.querySelector(".progress-bg").setAttribute("d", dBg);
303
+ this.shadowRoot.querySelector(".progress-bar").setAttribute("d", dBar);
323
304
  }
324
305
  getAttributes() {
325
306
  const common = super.getCommonAttributes(this);
@@ -333,13 +314,13 @@
333
314
  maxValue
334
315
  };
335
316
  }
336
- getProgressAngle(full) {
337
- const { range, progress, maxValue } = this.getAttributes();
317
+ getProgressAngle(attrs, full) {
318
+ const { range, progress, maxValue } = attrs;
338
319
  return full ? (maxValue - 1e-5) / maxValue * range : progress / maxValue * range;
339
320
  }
340
- calculateArcParameters(full) {
341
- const { shape, realRadius, arcHeightPercentage, orbitNumber, strokeWidth, arcHeight } = this.getAttributes();
342
- const arcAngle = this.getProgressAngle(full);
321
+ calculateArcParameters(attrs, full) {
322
+ const { shape, realRadius, arcHeightPercentage, orbitNumber, strokeWidth, arcHeight } = attrs;
323
+ const arcAngle = this.getProgressAngle(attrs, full);
343
324
  const params = super.calculateCommonArcParameters(
344
325
  arcAngle,
345
326
  realRadius,
@@ -349,8 +330,7 @@
349
330
  strokeWidth,
350
331
  arcHeight
351
332
  );
352
- const d = super.generatePathData(shape, params, arcHeight, orbitNumber);
353
- return { d };
333
+ return super.generatePathData(shape, params, arcHeight, orbitNumber);
354
334
  }
355
335
  };
356
336
 
@@ -413,8 +393,8 @@
413
393
  this.setupObservers();
414
394
  }
415
395
  disconnectedCallback() {
416
- this.observer?.disconnect();
417
- this.textObserver?.disconnect();
396
+ if (this.observer) this.observer.disconnect();
397
+ if (this.textObserver) this.textObserver.disconnect();
418
398
  }
419
399
  setupObservers() {
420
400
  this.observer = new MutationObserver((mutations) => {
@@ -430,15 +410,13 @@
430
410
  this.textObserver.observe(this, { characterData: true, subtree: true });
431
411
  }
432
412
  update() {
433
- const { length, fontSize, textAnchor, fitRange } = this.getAttributes();
434
- const orbitPath = this.shadowRoot.getElementById("orbitPath");
435
- const orbitShape = this.shadowRoot.getElementById("orbitShape");
436
- const text = this.shadowRoot.querySelector("text");
413
+ const attrs = this.getAttributes();
414
+ const { length, fontSize, textAnchor, fitRange } = attrs;
415
+ const orbitPath = this.shadowRoot.querySelector("#orbitPath");
416
+ const orbitShape = this.shadowRoot.querySelector("#orbitShape");
437
417
  const textPath = this.shadowRoot.querySelector("textPath");
438
- const { dShape } = this.calculateArcParameters();
439
- const { dPath } = this.calculateTextArcParameters();
440
- orbitShape.setAttribute("d", dShape);
441
- orbitPath.setAttribute("d", dPath);
418
+ orbitShape.setAttribute("d", this.calculateArcParameters(attrs).dShape);
419
+ orbitPath.setAttribute("d", this.calculateTextArcParameters(attrs).dPath);
442
420
  if (textAnchor === "start") {
443
421
  textPath.setAttribute("startOffset", "0%");
444
422
  textPath.setAttribute("text-anchor", "start");
@@ -452,7 +430,7 @@
452
430
  if (fitRange) {
453
431
  textPath.parentElement.setAttribute("textLength", orbitPath.getTotalLength());
454
432
  }
455
- text.style.fontSize = `calc(${fontSize} * (100 / (${length}) * (12 / var(--o-orbit-number)))`;
433
+ textPath.parentElement.style.fontSize = `calc(${fontSize} * (100 / (${length}) * (12 / var(--o-orbit-number)))`;
456
434
  textPath.textContent = this.textContent;
457
435
  }
458
436
  getAttributes() {
@@ -460,12 +438,12 @@
460
438
  let arcAngle;
461
439
  const flip = this.hasAttribute("flip") || this.classList.contains("flip");
462
440
  const fitRange = this.hasAttribute("fit-range") || this.classList.contains("fit-range") || false;
463
- const length = parseFloat(getComputedStyle(this).getPropertyValue("--o-force"));
441
+ const length = parseFloat(getComputedStyle(this).getPropertyValue("--o-force")) || 100;
464
442
  const textAnchor = this.getAttribute("text-anchor") || "middle";
465
443
  const fontSize = getComputedStyle(this).getPropertyValue("font-size") || getComputedStyle(this).getPropertyValue("--font-size");
466
444
  const range = parseFloat(getComputedStyle(this).getPropertyValue("--o-range") || 360);
467
445
  const value = parseFloat(this.getAttribute("value"));
468
- const gap = parseFloat(getComputedStyle(this).getPropertyValue("--o-gap"));
446
+ const gap = parseFloat(getComputedStyle(this).getPropertyValue("--o-gap")) || 1;
469
447
  if (value) {
470
448
  arcAngle = super.getProgressAngle(range, value);
471
449
  const prevElement = this.previousElementSibling;
@@ -492,8 +470,8 @@
492
470
  textAnchor
493
471
  };
494
472
  }
495
- calculateArcParameters() {
496
- const { arcAngle, realRadius, arcHeightPercentage, orbitNumber, shape, strokeWidth, arcHeight, gap } = this.getAttributes();
473
+ calculateArcParameters(attrs) {
474
+ const { arcAngle, realRadius, arcHeightPercentage, orbitNumber, shape, strokeWidth, arcHeight, gap } = attrs;
497
475
  const params = super.calculateCommonArcParameters(
498
476
  arcAngle,
499
477
  realRadius,
@@ -507,8 +485,8 @@
507
485
  const dShape = super.generatePathData(shape, params, arcHeight, orbitNumber);
508
486
  return { dShape };
509
487
  }
510
- calculateTextArcParameters() {
511
- const { arcAngle, realRadius, gap, flip } = this.getAttributes();
488
+ calculateTextArcParameters(attrs) {
489
+ const { arcAngle, realRadius, gap, flip } = attrs;
512
490
  const adjustedGap = gap * 0.5;
513
491
  const sweepFlag = flip ? 0 : 1;
514
492
  const largeArcFlag = arcAngle <= 180 ? 0 : 1;
@@ -534,39 +512,35 @@
534
512
  };
535
513
 
536
514
  // src/js/orbit-resize.js
537
- var Orbit = {};
538
- Orbit = {
539
- resize: (parentElementSelector) => {
540
- const parentElement = document.querySelector(parentElementSelector);
541
- if (!parentElement) {
542
- console.error(`Not found: ${parentElementSelector}`);
515
+ var Orbit = {
516
+ resize(parentElementSelector) {
517
+ const parent = document.querySelector(parentElementSelector);
518
+ if (!parent) {
519
+ console.error("Orbit.resize: element not found:", parentElementSelector);
543
520
  return;
544
521
  }
545
- const resizeObserver = new ResizeObserver((entries) => {
546
- for (let entry of entries) {
547
- const { width } = entry.contentRect;
548
- const childElements = parentElement.querySelectorAll(".gravity-spot");
549
- if (childElements) {
550
- childElements.forEach((childElement) => {
551
- let gravityForce = getComputedStyle(childElement).getPropertyValue("--o-force");
552
- let forceRatio = width / 500;
553
- childElement.style.setProperty("--o-force-ratio", `${forceRatio}`);
554
- });
555
- } else {
556
- console.error("No gravity-spot found");
557
- }
522
+ const applyRatio = (width) => {
523
+ parent.querySelectorAll(".gravity-spot").forEach((el) => {
524
+ el.style.setProperty("--o-force-ratio", String(width / 500));
525
+ });
526
+ };
527
+ let w = parent.offsetWidth || parent.getBoundingClientRect().width;
528
+ if (w > 0) applyRatio(w);
529
+ else requestAnimationFrame(() => {
530
+ w = parent.offsetWidth || parent.getBoundingClientRect().width;
531
+ if (w > 0) applyRatio(w);
532
+ });
533
+ const ro = new ResizeObserver((entries) => {
534
+ for (const e of entries) {
535
+ if (e.contentRect.width > 0) applyRatio(e.contentRect.width);
558
536
  }
559
537
  });
560
- resizeObserver.observe(parentElement);
538
+ ro.observe(parent);
561
539
  }
562
540
  };
563
541
 
564
542
  // src/orbit.js
565
- if (!customElements.get("o-progress")) {
566
- customElements.define("o-progress", OrbitProgress);
567
- }
568
- if (!customElements.get("o-arc")) {
569
- customElements.define("o-arc", OrbitArc);
570
- }
543
+ if (!customElements.get("o-progress")) customElements.define("o-progress", OrbitProgress);
544
+ if (!customElements.get("o-arc")) customElements.define("o-arc", OrbitArc);
571
545
  window.Orbit = Orbit;
572
546
  })();