snowies 0.3.0 → 0.4.0

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.cjs CHANGED
@@ -313,7 +313,10 @@ function createLights(options) {
313
313
  const colors = resolvePalette(options.palette);
314
314
  const bulbCount = Math.max(6, Math.min(64, Math.round(options.bulbs)));
315
315
  const mobileCutoff = Math.max(10, Math.round(bulbCount * 0.45));
316
- const bg = BACKDROP_STYLES[options.backdrop] ?? BACKDROP_STYLES.pine;
316
+ const bg = BACKDROP_STYLES[options.backdrop] ?? BACKDROP_STYLES.transparent;
317
+ const animMode = options.animation ?? "twinkle";
318
+ const glassAnimCss = animMode === "steady" ? "opacity: 0.96;" : animMode === "wave" ? "animation: glass-wave 2.4s ease-in-out var(--wave-delay) infinite;" : "animation: glass-glow var(--bulb-duration) ease-in-out var(--bulb-delay) infinite alternate;";
319
+ const haloAnimCss = animMode === "steady" ? "opacity: 0.28;" : animMode === "wave" ? "animation: halo-wave 2.4s ease-in-out var(--wave-delay) infinite;" : "animation: halo-glow var(--bulb-duration) ease-in-out var(--bulb-delay) infinite alternate;";
317
320
  const style = document.createElement("style");
318
321
  style.textContent = `
319
322
  :host { display: block; line-height: 0; }
@@ -386,7 +389,7 @@ function createLights(options) {
386
389
  radial-gradient(ellipse at 50% 35%, var(--bulb-core) 5%, var(--bulb-color) 80%);
387
390
  box-shadow: 0 0 5px var(--bulb-color), 0 0 12px var(--bulb-color);
388
391
  will-change: opacity, transform;
389
- animation: glass-glow var(--bulb-duration) ease-in-out var(--bulb-delay) infinite alternate;
392
+ ${glassAnimCss}
390
393
  transition: transform 0.25s ease, opacity 0.25s ease;
391
394
  }
392
395
  .filament {
@@ -408,7 +411,7 @@ function createLights(options) {
408
411
  opacity: 0.2;
409
412
  pointer-events: none;
410
413
  will-change: opacity;
411
- animation: halo-glow var(--bulb-duration) ease-in-out var(--bulb-delay) infinite alternate;
414
+ ${haloAnimCss}
412
415
  transition: opacity 0.25s ease;
413
416
  }
414
417
  .lamp.is-near .glass,
@@ -434,6 +437,14 @@ function createLights(options) {
434
437
  from { opacity: 0.12; }
435
438
  to { opacity: 0.34; }
436
439
  }
440
+ @keyframes glass-wave {
441
+ 0%, 58%, 100% { opacity: 0.58; transform: scale(0.97); }
442
+ 22% { opacity: 1; transform: scale(1.1); }
443
+ }
444
+ @keyframes halo-wave {
445
+ 0%, 58%, 100% { opacity: 0.08; }
446
+ 22% { opacity: 0.52; }
447
+ }
437
448
  @media (max-width: 640px) {
438
449
  .lamp:nth-of-type(n + ${mobileCutoff + 1}) { display: none; }
439
450
  }
@@ -443,9 +454,10 @@ function createLights(options) {
443
454
  `;
444
455
  const strip = document.createElement(options.interactive ? "button" : "div");
445
456
  strip.className = "strip";
457
+ strip.setAttribute("data-animation", animMode);
446
458
  if (options.interactive) {
447
459
  strip.setAttribute("type", "button");
448
- strip.setAttribute("aria-label", options.ariaLabel ?? "Illuminate Christmas lights");
460
+ strip.setAttribute("aria-label", options.ariaLabel || "Illuminate Christmas lights");
449
461
  } else {
450
462
  strip.setAttribute("aria-hidden", "true");
451
463
  }
@@ -464,6 +476,7 @@ function createLights(options) {
464
476
  lamp.style.setProperty("--bulb-color", c.color);
465
477
  lamp.style.setProperty("--bulb-core", c.core);
466
478
  lamp.style.setProperty("--bulb-delay", `${i * -0.43}s`);
479
+ lamp.style.setProperty("--wave-delay", `${(i * -0.1).toFixed(2)}s`);
467
480
  lamp.style.setProperty("--bulb-duration", `${3.4 + i % 5 * 0.35}s`);
468
481
  lamp.style.setProperty("--bulb-cord", `${4 + i % 3 * 2}px`);
469
482
  lamp.style.setProperty("--bulb-tilt", `${i % 2 ? 5 : -5}deg`);
@@ -546,6 +559,37 @@ function createPrng(initialSeed = 202611) {
546
559
  return seed / 4294967296;
547
560
  };
548
561
  }
562
+ function parseRgbLuminance(color) {
563
+ const match = /rgba?\(\s*(\d+(?:\.\d+)?)[,\s]+(\d+(?:\.\d+)?)[,\s]+(\d+(?:\.\d+)?)(?:\s*[,/]\s*(\d*(?:\.\d+)?))?\s*\)/i.exec(
564
+ color
565
+ );
566
+ if (!match) return null;
567
+ const alpha = match[4] !== void 0 && match[4] !== "" ? Number(match[4]) : 1;
568
+ if (Number.isNaN(alpha) || alpha < 0.1) return null;
569
+ const r = Number(match[1]);
570
+ const g = Number(match[2]);
571
+ const b = Number(match[3]);
572
+ return (0.299 * r + 0.587 * g + 0.114 * b) / 255;
573
+ }
574
+ function detectLightBackground() {
575
+ if (typeof window === "undefined" || typeof document === "undefined") return false;
576
+ const candidates = [document.body, document.documentElement];
577
+ for (const el of candidates) {
578
+ if (!el) continue;
579
+ const bg = window.getComputedStyle(el).backgroundColor;
580
+ const lum = parseRgbLuminance(bg);
581
+ if (lum !== null) {
582
+ return lum > 0.55;
583
+ }
584
+ }
585
+ if (typeof window.matchMedia === "function") {
586
+ return window.matchMedia("(prefers-color-scheme: light)").matches;
587
+ }
588
+ return false;
589
+ }
590
+ function resolveContrasting(setting) {
591
+ return setting === "auto" ? detectLightBackground() : Boolean(setting);
592
+ }
549
593
  function createFlakeSprite(contrasting, soft) {
550
594
  if (typeof document === "undefined") return null;
551
595
  const size = soft ? 32 : 20;
@@ -594,8 +638,18 @@ function createSnow(options) {
594
638
  return { destroy() {
595
639
  } };
596
640
  }
597
- const sharpSprite = createFlakeSprite(options.contrasting, false);
598
- const softSprite = createFlakeSprite(options.contrasting, true);
641
+ let isContrasting = resolveContrasting(options.contrasting);
642
+ let sharpSprite = createFlakeSprite(isContrasting, false);
643
+ let softSprite = createFlakeSprite(isContrasting, true);
644
+ const refreshContrasting = () => {
645
+ if (options.contrasting !== "auto") return;
646
+ const next = detectLightBackground();
647
+ if (next !== isContrasting) {
648
+ isContrasting = next;
649
+ sharpSprite = createFlakeSprite(isContrasting, false);
650
+ softSprite = createFlakeSprite(isContrasting, true);
651
+ }
652
+ };
599
653
  let width = window.innerWidth;
600
654
  let height = window.innerHeight;
601
655
  let flakes = [];
@@ -603,6 +657,8 @@ function createSnow(options) {
603
657
  let lastTime = 0;
604
658
  let paused = document.visibilityState === "hidden";
605
659
  let destroyed = false;
660
+ let targetWind = 0;
661
+ let currentWind = 0;
606
662
  const initFlakes = () => {
607
663
  const random = createPrng(202611);
608
664
  const count = resolveCount(options.density, width);
@@ -641,10 +697,18 @@ function createSnow(options) {
641
697
  const dt = Math.min((now - lastTime) / 1e3, 0.05);
642
698
  lastTime = now;
643
699
  const t = now / 1e3;
700
+ if (options.mouseWind) {
701
+ currentWind += (targetWind - currentWind) * Math.min(1, dt * 3.2);
702
+ }
644
703
  ctx.clearRect(0, 0, width, height);
645
704
  for (let i = 0; i < flakes.length; i++) {
646
705
  const f = flakes[i];
647
706
  f.y += f.speedY * dt;
707
+ if (options.mouseWind && Math.abs(currentWind) > 2e-3) {
708
+ f.x += currentWind * 58 * (0.45 + f.layer * 0.42) * dt;
709
+ if (f.x < -24) f.x += width + 48;
710
+ else if (f.x > width + 24) f.x -= width + 48;
711
+ }
648
712
  const xOffset = options.wind ? Math.sin(t * f.driftFreq + f.phase) * f.driftAmp : 0;
649
713
  const drawX = f.x + xOffset;
650
714
  if (f.y > height + f.radius * 2) {
@@ -660,7 +724,7 @@ function createSnow(options) {
660
724
  const drawSize = f.radius * (f.layer === 2 ? 3.2 : 2.5);
661
725
  ctx.drawImage(sprite, drawX - drawSize / 2, f.y - drawSize / 2, drawSize, drawSize);
662
726
  } else {
663
- ctx.fillStyle = options.contrasting ? "#d7edff" : "#ffffff";
727
+ ctx.fillStyle = isContrasting ? "#d7edff" : "#ffffff";
664
728
  ctx.beginPath();
665
729
  ctx.arc(drawX, f.y, f.radius, 0, Math.PI * 2);
666
730
  ctx.fill();
@@ -680,9 +744,33 @@ function createSnow(options) {
680
744
  rafId = window.requestAnimationFrame(render);
681
745
  }
682
746
  };
747
+ const handlePointerMove = (event) => {
748
+ targetWind = (event.clientX / Math.max(1, width) - 0.5) * 2;
749
+ };
750
+ const handlePointerLeave = () => {
751
+ targetWind = 0;
752
+ };
683
753
  resize();
684
754
  window.addEventListener("resize", resize, { passive: true });
685
755
  document.addEventListener("visibilitychange", handleVisibility);
756
+ if (options.mouseWind) {
757
+ window.addEventListener("pointermove", handlePointerMove, { passive: true });
758
+ document.documentElement.addEventListener("pointerleave", handlePointerLeave, { passive: true });
759
+ }
760
+ let observer = null;
761
+ if (options.contrasting === "auto" && typeof MutationObserver !== "undefined") {
762
+ observer = new MutationObserver(refreshContrasting);
763
+ observer.observe(document.documentElement, {
764
+ attributes: true,
765
+ attributeFilter: ["class", "style", "data-theme"]
766
+ });
767
+ if (document.body) {
768
+ observer.observe(document.body, {
769
+ attributes: true,
770
+ attributeFilter: ["class", "style", "data-theme"]
771
+ });
772
+ }
773
+ }
686
774
  if (!paused) {
687
775
  rafId = window.requestAnimationFrame(render);
688
776
  }
@@ -692,6 +780,11 @@ function createSnow(options) {
692
780
  if (rafId) window.cancelAnimationFrame(rafId);
693
781
  window.removeEventListener("resize", resize);
694
782
  document.removeEventListener("visibilitychange", handleVisibility);
783
+ if (options.mouseWind) {
784
+ window.removeEventListener("pointermove", handlePointerMove);
785
+ document.documentElement.removeEventListener("pointerleave", handlePointerLeave);
786
+ }
787
+ observer?.disconnect();
695
788
  canvas.remove();
696
789
  }
697
790
  };
@@ -886,16 +979,16 @@ function createToggleButton(options, initialActive, onToggle) {
886
979
  // src/presets.ts
887
980
  var DEFAULT_CONFIG = {
888
981
  preset: "classic",
889
- locale: "en",
890
982
  enabled: true,
891
983
  respectReducedMotion: true,
892
984
  schedule: false,
893
985
  snow: {
894
986
  enabled: true,
895
987
  density: "medium",
896
- contrasting: false,
988
+ contrasting: "auto",
897
989
  speed: 1,
898
990
  wind: true,
991
+ mouseWind: true,
899
992
  zIndex: 9990
900
993
  },
901
994
  lights: {
@@ -904,9 +997,11 @@ var DEFAULT_CONFIG = {
904
997
  backdrop: "transparent",
905
998
  bulbs: 32,
906
999
  palette: "classic",
1000
+ animation: "twinkle",
907
1001
  interactive: true,
908
1002
  pushBody: false,
909
1003
  zIndex: 9995,
1004
+ ariaLabel: "Illuminate Christmas lights",
910
1005
  container: null
911
1006
  },
912
1007
  buttonBurst: {
@@ -931,51 +1026,75 @@ var DEFAULT_CONFIG = {
931
1026
  offset: 16,
932
1027
  persist: true,
933
1028
  storageKey: "snowies:enabled",
934
- labelOn: "Desactivar efectos navide\xF1os",
935
- labelOff: "Activar efectos navide\xF1os",
936
- textOn: "Navidad",
937
- textOff: "Pausado",
1029
+ labelOn: "Disable Christmas effects",
1030
+ labelOff: "Enable Christmas effects",
1031
+ textOn: "Holiday",
1032
+ textOff: "Paused",
938
1033
  zIndex: 9999
939
1034
  }
940
1035
  };
941
1036
  var PRESETS = {
942
1037
  classic: {
943
- snow: { enabled: true, density: "medium", contrasting: false },
944
- lights: { enabled: true, position: "fixed", backdrop: "transparent", palette: "classic" },
1038
+ snow: { enabled: true, density: "medium", contrasting: "auto", mouseWind: true },
1039
+ lights: {
1040
+ enabled: true,
1041
+ position: "fixed",
1042
+ backdrop: "transparent",
1043
+ palette: "classic",
1044
+ animation: "twinkle"
1045
+ },
945
1046
  buttonBurst: { enabled: true },
946
1047
  buttonSnow: { enabled: true, mode: "manual", style: "subtle" },
947
1048
  toggleButton: { enabled: true, variant: "icon", theme: "minimal" }
948
1049
  },
949
1050
  minimal: {
950
- snow: { enabled: true, density: "light", contrasting: false, speed: 0.85 },
1051
+ snow: { enabled: true, density: "light", contrasting: "auto", speed: 0.85, mouseWind: false },
951
1052
  lights: { enabled: false },
952
1053
  buttonBurst: { enabled: false },
953
1054
  buttonSnow: { enabled: false },
954
1055
  toggleButton: { enabled: true, variant: "icon", theme: "minimal" }
955
1056
  },
956
1057
  festive: {
957
- snow: { enabled: true, density: "heavy", contrasting: false, speed: 1.1 },
958
- lights: { enabled: true, position: "fixed", backdrop: "pine", palette: "classic" },
1058
+ snow: { enabled: true, density: "heavy", contrasting: "auto", speed: 1.1, mouseWind: true },
1059
+ lights: {
1060
+ enabled: true,
1061
+ position: "fixed",
1062
+ backdrop: "pine",
1063
+ palette: "classic",
1064
+ animation: "wave"
1065
+ },
959
1066
  buttonBurst: { enabled: true, particles: 56, crystals: true },
960
1067
  buttonSnow: { enabled: true, mode: "manual", style: "fluffy" },
961
1068
  toggleButton: { enabled: true, variant: "pill", theme: "festive" }
962
1069
  },
963
1070
  light: {
964
- snow: { enabled: true, density: "medium", contrasting: true },
965
- lights: { enabled: true, position: "fixed", backdrop: "transparent", palette: "ice" },
1071
+ snow: { enabled: true, density: "medium", contrasting: true, mouseWind: true },
1072
+ lights: {
1073
+ enabled: true,
1074
+ position: "fixed",
1075
+ backdrop: "transparent",
1076
+ palette: "ice",
1077
+ animation: "twinkle"
1078
+ },
966
1079
  buttonBurst: { enabled: true },
967
1080
  buttonSnow: { enabled: true, mode: "manual", style: "subtle" },
968
1081
  toggleButton: { enabled: true, variant: "icon", theme: "light" }
969
1082
  },
970
1083
  "lights-only": {
971
1084
  snow: { enabled: false },
972
- lights: { enabled: true, position: "fixed", backdrop: "transparent", palette: "classic" },
1085
+ lights: {
1086
+ enabled: true,
1087
+ position: "fixed",
1088
+ backdrop: "transparent",
1089
+ palette: "classic",
1090
+ animation: "twinkle"
1091
+ },
973
1092
  buttonBurst: { enabled: false },
974
1093
  buttonSnow: { enabled: false },
975
1094
  toggleButton: { enabled: true, variant: "icon", theme: "minimal" }
976
1095
  },
977
1096
  "snow-only": {
978
- snow: { enabled: true, density: "medium", contrasting: false },
1097
+ snow: { enabled: true, density: "medium", contrasting: "auto", mouseWind: true },
979
1098
  lights: { enabled: false },
980
1099
  buttonBurst: { enabled: false },
981
1100
  buttonSnow: { enabled: false },
@@ -983,40 +1102,6 @@ var PRESETS = {
983
1102
  }
984
1103
  };
985
1104
 
986
- // src/utils/i18n.ts
987
- var MESSAGES = {
988
- en: {
989
- toggleLabelOn: "Disable Christmas effects",
990
- toggleLabelOff: "Enable Christmas effects",
991
- toggleTextOn: "Holiday",
992
- toggleTextOff: "Paused",
993
- lightsAriaLabel: "Illuminate Christmas lights"
994
- },
995
- es: {
996
- toggleLabelOn: "Desactivar efectos navide\xF1os",
997
- toggleLabelOff: "Activar efectos navide\xF1os",
998
- toggleTextOn: "Navidad",
999
- toggleTextOff: "Pausado",
1000
- lightsAriaLabel: "Hacer brillar las luces navide\xF1as"
1001
- }
1002
- };
1003
- function resolveLocale(locale = "auto") {
1004
- if (locale === "es" || locale === "en") return locale;
1005
- if (typeof document !== "undefined") {
1006
- const docLang = document.documentElement?.lang?.toLowerCase();
1007
- if (docLang) {
1008
- return docLang.startsWith("es") ? "es" : "en";
1009
- }
1010
- }
1011
- if (typeof navigator !== "undefined" && navigator.language) {
1012
- return navigator.language.toLowerCase().startsWith("es") ? "es" : "en";
1013
- }
1014
- return "en";
1015
- }
1016
- function getMessages(locale = "auto") {
1017
- return MESSAGES[resolveLocale(locale)];
1018
- }
1019
-
1020
1105
  // src/utils/config.ts
1021
1106
  function mergeFeature(base, presetOverride, userOverride) {
1022
1107
  let result = { ...base };
@@ -1036,33 +1121,18 @@ function resolveConfig(options = {}, previous) {
1036
1121
  const presetName = options.preset ?? previous?.preset ?? DEFAULT_CONFIG.preset;
1037
1122
  const presetDef = options.preset ? PRESETS[presetName] ?? {} : previous ? {} : PRESETS[presetName] ?? {};
1038
1123
  const base = previous && !options.preset ? previous : DEFAULT_CONFIG;
1039
- const locale = resolveLocale(options.locale ?? previous?.locale ?? "auto");
1040
- const msg = MESSAGES[locale];
1041
- const localeChanged = !previous || options.locale !== void 0 || options.preset !== void 0;
1042
- const lights = mergeFeature(base.lights, presetDef.lights, options.lights);
1043
- const userLights = typeof options.lights === "object" ? options.lights : void 0;
1044
- if (localeChanged && !userLights?.ariaLabel) {
1045
- lights.ariaLabel = msg.lightsAriaLabel;
1046
- }
1047
1124
  const toggleButton = mergeFeature(base.toggleButton, presetDef.toggleButton, options.toggleButton);
1048
1125
  const userToggle = typeof options.toggleButton === "object" ? options.toggleButton : void 0;
1049
- if (localeChanged) {
1050
- if (!userToggle?.labelOn) toggleButton.labelOn = msg.toggleLabelOn;
1051
- if (!userToggle?.labelOff) toggleButton.labelOff = msg.toggleLabelOff;
1052
- if (!userToggle?.textOn) toggleButton.textOn = msg.toggleTextOn;
1053
- if (!userToggle?.textOff) toggleButton.textOff = msg.toggleTextOff;
1054
- }
1055
1126
  if (userToggle?.style && base.toggleButton.style) {
1056
1127
  toggleButton.style = { ...base.toggleButton.style, ...userToggle.style };
1057
1128
  }
1058
1129
  return {
1059
1130
  preset: presetName,
1060
- locale,
1061
1131
  enabled: options.enabled ?? base.enabled,
1062
1132
  respectReducedMotion: options.respectReducedMotion ?? base.respectReducedMotion,
1063
1133
  schedule: options.schedule !== void 0 ? options.schedule : base.schedule,
1064
1134
  snow: mergeFeature(base.snow, presetDef.snow, options.snow),
1065
- lights,
1135
+ lights: mergeFeature(base.lights, presetDef.lights, options.lights),
1066
1136
  buttonBurst: mergeFeature(base.buttonBurst, presetDef.buttonBurst, options.buttonBurst),
1067
1137
  buttonSnow: mergeFeature(base.buttonSnow, presetDef.buttonSnow, options.buttonSnow),
1068
1138
  toggleButton,
@@ -1257,7 +1327,6 @@ function createSnowies(options = {}) {
1257
1327
  }
1258
1328
 
1259
1329
  exports.DEFAULT_CONFIG = DEFAULT_CONFIG;
1260
- exports.MESSAGES = MESSAGES;
1261
1330
  exports.PRESETS = PRESETS;
1262
1331
  exports.createButtonBurst = createButtonBurst;
1263
1332
  exports.createButtonSnow = createButtonSnow;
@@ -1265,8 +1334,6 @@ exports.createLights = createLights;
1265
1334
  exports.createSnow = createSnow;
1266
1335
  exports.createSnowies = createSnowies;
1267
1336
  exports.createToggleButton = createToggleButton;
1268
- exports.getMessages = getMessages;
1269
1337
  exports.isWithinSchedule = isWithinSchedule;
1270
- exports.resolveLocale = resolveLocale;
1271
1338
  //# sourceMappingURL=index.cjs.map
1272
1339
  //# sourceMappingURL=index.cjs.map