asciify-engine 1.0.11 → 1.0.13

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
@@ -832,7 +832,7 @@ async function asciifyVideo(source, canvas, { fontSize = 10, style = "classic",
832
832
  cancelAnimationFrame(animId);
833
833
  };
834
834
  }
835
- var EMBED_CDN_VERSION = "1.0.11";
835
+ var EMBED_CDN_VERSION = "1.0.12";
836
836
  function buildEmbedOpts(options, rows, cols, width, height, fps, animated) {
837
837
  const o = {
838
838
  r: rows,
@@ -999,11 +999,28 @@ function renderWaveBackground(ctx, width, height, time, mousePos = { x: 0.5, y:
999
999
  }
1000
1000
  }
1001
1001
  }
1002
- function mountWaveBackground(target, options = {}) {
1003
- const { opacity = 0.2, className, zIndex = 0, ...renderOpts } = options;
1002
+ function _parseColor(c) {
1003
+ const hex = c.match(/^#([0-9a-f]{3,8})$/i)?.[1];
1004
+ if (hex) {
1005
+ const h = hex.length <= 4 ? hex.split("").map((x) => parseInt(x + x, 16)) : [parseInt(hex.slice(0, 2), 16), parseInt(hex.slice(2, 4), 16), parseInt(hex.slice(4, 6), 16)];
1006
+ return { r: h[0], g: h[1], b: h[2] };
1007
+ }
1008
+ const rgb = c.match(/rgba?\(\s*(\d+)[,\s]+(\d+)[,\s]+(\d+)/i);
1009
+ if (rgb) return { r: +rgb[1], g: +rgb[2], b: +rgb[3] };
1010
+ return null;
1011
+ }
1012
+ function asciiBackground(target, options = {}) {
1013
+ const {
1014
+ opacity = 0.2,
1015
+ className,
1016
+ zIndex = 0,
1017
+ colorScheme = "auto",
1018
+ color,
1019
+ ...renderOpts
1020
+ } = options;
1004
1021
  const container = typeof target === "string" ? document.querySelector(target) : target;
1005
1022
  if (!container) {
1006
- console.warn("[asciify] mountWaveBackground: target not found", target);
1023
+ console.warn("[asciify] asciiBackground: target not found", target);
1007
1024
  return { destroy: () => {
1008
1025
  } };
1009
1026
  }
@@ -1027,6 +1044,24 @@ function mountWaveBackground(target, options = {}) {
1027
1044
  const dpr = window.devicePixelRatio || 1;
1028
1045
  const mouse = { x: 0.5, y: 0.5 };
1029
1046
  const smoothMouse = { x: 0.5, y: 0.5 };
1047
+ const mq = window.matchMedia("(prefers-color-scheme: light)");
1048
+ const isLight = () => {
1049
+ if (colorScheme === "light") return true;
1050
+ if (colorScheme === "dark") return false;
1051
+ return mq.matches;
1052
+ };
1053
+ let parsedColor = null;
1054
+ if (color) parsedColor = _parseColor(color);
1055
+ const buildOpts = () => ({
1056
+ ...renderOpts,
1057
+ lightMode: renderOpts.lightMode !== void 0 ? renderOpts.lightMode : isLight(),
1058
+ baseColor: parsedColor ? `rgba(${parsedColor.r},${parsedColor.g},${parsedColor.b},{a})` : renderOpts.baseColor
1059
+ });
1060
+ const optsRef = { current: buildOpts() };
1061
+ const onSchemeChange = () => {
1062
+ optsRef.current = buildOpts();
1063
+ };
1064
+ if (colorScheme === "auto") mq.addEventListener("change", onSchemeChange);
1030
1065
  const resize = () => {
1031
1066
  const r = container.getBoundingClientRect();
1032
1067
  canvas.width = r.width * dpr;
@@ -1048,7 +1083,7 @@ function mountWaveBackground(target, options = {}) {
1048
1083
  smoothMouse.x += (mouse.x - smoothMouse.x) * 0.07;
1049
1084
  smoothMouse.y += (mouse.y - smoothMouse.y) * 0.07;
1050
1085
  const r = container.getBoundingClientRect();
1051
- renderWaveBackground(ctx, r.width, r.height, time, smoothMouse, renderOpts);
1086
+ renderWaveBackground(ctx, r.width, r.height, time, smoothMouse, optsRef.current);
1052
1087
  time += 0.016;
1053
1088
  raf = requestAnimationFrame(tick);
1054
1089
  };
@@ -1057,12 +1092,14 @@ function mountWaveBackground(target, options = {}) {
1057
1092
  destroy: () => {
1058
1093
  cancelAnimationFrame(raf);
1059
1094
  ro.disconnect();
1095
+ if (colorScheme === "auto") mq.removeEventListener("change", onSchemeChange);
1060
1096
  window.removeEventListener("mousemove", onMouseMove);
1061
1097
  canvas.remove();
1062
1098
  container.style.position = prevPosition;
1063
1099
  }
1064
1100
  };
1065
1101
  }
1102
+ var mountWaveBackground = asciiBackground;
1066
1103
 
1067
1104
  // src/webgl-engine.ts
1068
1105
  var VERT_SRC = (
@@ -1516,6 +1553,7 @@ exports.ART_STYLE_PRESETS = ART_STYLE_PRESETS;
1516
1553
  exports.CHARSETS = CHARSETS;
1517
1554
  exports.DEFAULT_OPTIONS = DEFAULT_OPTIONS;
1518
1555
  exports.HOVER_PRESETS = HOVER_PRESETS;
1556
+ exports.asciiBackground = asciiBackground;
1519
1557
  exports.asciify = asciify;
1520
1558
  exports.asciifyGif = asciifyGif;
1521
1559
  exports.asciifyVideo = asciifyVideo;