asciify-engine 1.0.10 → 1.0.11

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.11";
836
836
  function buildEmbedOpts(options, rows, cols, width, height, fps, animated) {
837
837
  const o = {
838
838
  r: rows,
@@ -999,6 +999,70 @@ 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;
1004
+ const container = typeof target === "string" ? document.querySelector(target) : target;
1005
+ if (!container) {
1006
+ console.warn("[asciify] mountWaveBackground: target not found", target);
1007
+ return { destroy: () => {
1008
+ } };
1009
+ }
1010
+ const prevPosition = container.style.position;
1011
+ if (getComputedStyle(container).position === "static") {
1012
+ container.style.position = "relative";
1013
+ }
1014
+ const canvas = document.createElement("canvas");
1015
+ canvas.style.cssText = [
1016
+ "position:absolute",
1017
+ "inset:0",
1018
+ "width:100%",
1019
+ "height:100%",
1020
+ `opacity:${opacity}`,
1021
+ "pointer-events:none",
1022
+ `z-index:${zIndex}`
1023
+ ].join(";");
1024
+ if (className) canvas.className = className;
1025
+ container.prepend(canvas);
1026
+ const ctx = canvas.getContext("2d");
1027
+ const dpr = window.devicePixelRatio || 1;
1028
+ const mouse = { x: 0.5, y: 0.5 };
1029
+ const smoothMouse = { x: 0.5, y: 0.5 };
1030
+ const resize = () => {
1031
+ const r = container.getBoundingClientRect();
1032
+ canvas.width = r.width * dpr;
1033
+ canvas.height = r.height * dpr;
1034
+ ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
1035
+ };
1036
+ resize();
1037
+ const onMouseMove = (e) => {
1038
+ const r = container.getBoundingClientRect();
1039
+ mouse.x = (e.clientX - r.left) / r.width;
1040
+ mouse.y = (e.clientY - r.top) / r.height;
1041
+ };
1042
+ const ro = new ResizeObserver(resize);
1043
+ ro.observe(container);
1044
+ window.addEventListener("mousemove", onMouseMove);
1045
+ let time = 0;
1046
+ let raf = 0;
1047
+ const tick = () => {
1048
+ smoothMouse.x += (mouse.x - smoothMouse.x) * 0.07;
1049
+ smoothMouse.y += (mouse.y - smoothMouse.y) * 0.07;
1050
+ const r = container.getBoundingClientRect();
1051
+ renderWaveBackground(ctx, r.width, r.height, time, smoothMouse, renderOpts);
1052
+ time += 0.016;
1053
+ raf = requestAnimationFrame(tick);
1054
+ };
1055
+ raf = requestAnimationFrame(tick);
1056
+ return {
1057
+ destroy: () => {
1058
+ cancelAnimationFrame(raf);
1059
+ ro.disconnect();
1060
+ window.removeEventListener("mousemove", onMouseMove);
1061
+ canvas.remove();
1062
+ container.style.position = prevPosition;
1063
+ }
1064
+ };
1065
+ }
1002
1066
 
1003
1067
  // src/webgl-engine.ts
1004
1068
  var VERT_SRC = (
@@ -1459,6 +1523,7 @@ exports.generateAnimatedEmbedCode = generateAnimatedEmbedCode;
1459
1523
  exports.generateEmbedCode = generateEmbedCode;
1460
1524
  exports.gifToAsciiFrames = gifToAsciiFrames;
1461
1525
  exports.imageToAsciiFrame = imageToAsciiFrame;
1526
+ exports.mountWaveBackground = mountWaveBackground;
1462
1527
  exports.renderFrameToCanvas = renderFrameToCanvas;
1463
1528
  exports.renderWaveBackground = renderWaveBackground;
1464
1529
  exports.tryCreateWebGLRenderer = tryCreateWebGLRenderer;