asciify-engine 1.0.10 → 1.0.12

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.10";
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,6 +999,107 @@ function renderWaveBackground(ctx, width, height, time, mousePos = { x: 0.5, y:
999
999
  }
1000
1000
  }
1001
1001
  }
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;
1021
+ const container = typeof target === "string" ? document.querySelector(target) : target;
1022
+ if (!container) {
1023
+ console.warn("[asciify] asciiBackground: target not found", target);
1024
+ return { destroy: () => {
1025
+ } };
1026
+ }
1027
+ const prevPosition = container.style.position;
1028
+ if (getComputedStyle(container).position === "static") {
1029
+ container.style.position = "relative";
1030
+ }
1031
+ const canvas = document.createElement("canvas");
1032
+ canvas.style.cssText = [
1033
+ "position:absolute",
1034
+ "inset:0",
1035
+ "width:100%",
1036
+ "height:100%",
1037
+ `opacity:${opacity}`,
1038
+ "pointer-events:none",
1039
+ `z-index:${zIndex}`
1040
+ ].join(";");
1041
+ if (className) canvas.className = className;
1042
+ container.prepend(canvas);
1043
+ const ctx = canvas.getContext("2d");
1044
+ const dpr = window.devicePixelRatio || 1;
1045
+ const mouse = { x: 0.5, y: 0.5 };
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);
1065
+ const resize = () => {
1066
+ const r = container.getBoundingClientRect();
1067
+ canvas.width = r.width * dpr;
1068
+ canvas.height = r.height * dpr;
1069
+ ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
1070
+ };
1071
+ resize();
1072
+ const onMouseMove = (e) => {
1073
+ const r = container.getBoundingClientRect();
1074
+ mouse.x = (e.clientX - r.left) / r.width;
1075
+ mouse.y = (e.clientY - r.top) / r.height;
1076
+ };
1077
+ const ro = new ResizeObserver(resize);
1078
+ ro.observe(container);
1079
+ window.addEventListener("mousemove", onMouseMove);
1080
+ let time = 0;
1081
+ let raf = 0;
1082
+ const tick = () => {
1083
+ smoothMouse.x += (mouse.x - smoothMouse.x) * 0.07;
1084
+ smoothMouse.y += (mouse.y - smoothMouse.y) * 0.07;
1085
+ const r = container.getBoundingClientRect();
1086
+ renderWaveBackground(ctx, r.width, r.height, time, smoothMouse, optsRef.current);
1087
+ time += 0.016;
1088
+ raf = requestAnimationFrame(tick);
1089
+ };
1090
+ raf = requestAnimationFrame(tick);
1091
+ return {
1092
+ destroy: () => {
1093
+ cancelAnimationFrame(raf);
1094
+ ro.disconnect();
1095
+ if (colorScheme === "auto") mq.removeEventListener("change", onSchemeChange);
1096
+ window.removeEventListener("mousemove", onMouseMove);
1097
+ canvas.remove();
1098
+ container.style.position = prevPosition;
1099
+ }
1100
+ };
1101
+ }
1102
+ var mountWaveBackground = asciiBackground;
1002
1103
 
1003
1104
  // src/webgl-engine.ts
1004
1105
  var VERT_SRC = (
@@ -1452,6 +1553,7 @@ exports.ART_STYLE_PRESETS = ART_STYLE_PRESETS;
1452
1553
  exports.CHARSETS = CHARSETS;
1453
1554
  exports.DEFAULT_OPTIONS = DEFAULT_OPTIONS;
1454
1555
  exports.HOVER_PRESETS = HOVER_PRESETS;
1556
+ exports.asciiBackground = asciiBackground;
1455
1557
  exports.asciify = asciify;
1456
1558
  exports.asciifyGif = asciifyGif;
1457
1559
  exports.asciifyVideo = asciifyVideo;
@@ -1459,6 +1561,7 @@ exports.generateAnimatedEmbedCode = generateAnimatedEmbedCode;
1459
1561
  exports.generateEmbedCode = generateEmbedCode;
1460
1562
  exports.gifToAsciiFrames = gifToAsciiFrames;
1461
1563
  exports.imageToAsciiFrame = imageToAsciiFrame;
1564
+ exports.mountWaveBackground = mountWaveBackground;
1462
1565
  exports.renderFrameToCanvas = renderFrameToCanvas;
1463
1566
  exports.renderWaveBackground = renderWaveBackground;
1464
1567
  exports.tryCreateWebGLRenderer = tryCreateWebGLRenderer;