asciify-engine 1.0.3 → 1.0.4

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
@@ -758,6 +758,69 @@ async function asciify(source, canvas, { fontSize = 10, style = "classic", optio
758
758
  const { frame } = imageToAsciiFrame(el, merged, canvas.width, canvas.height);
759
759
  renderFrameToCanvas(ctx, frame, merged, canvas.width, canvas.height);
760
760
  }
761
+ async function asciifyGif(source, canvas, { fontSize = 10, style = "classic", options = {} } = {}) {
762
+ const buffer = typeof source === "string" ? await fetch(source).then((r) => r.arrayBuffer()) : source;
763
+ const merged = { ...DEFAULT_OPTIONS, ...ART_STYLE_PRESETS[style], ...options, fontSize };
764
+ const ctx = canvas.getContext("2d");
765
+ if (!ctx) throw new Error("Could not get 2d context from canvas");
766
+ const { frames, fps } = await gifToAsciiFrames(buffer, merged, canvas.width, canvas.height);
767
+ let cancelled = false;
768
+ let animId;
769
+ let i = 0;
770
+ let last = performance.now();
771
+ const interval = 1e3 / fps;
772
+ const tick = (now) => {
773
+ if (cancelled) return;
774
+ if (now - last >= interval) {
775
+ renderFrameToCanvas(ctx, frames[i], merged, canvas.width, canvas.height);
776
+ i = (i + 1) % frames.length;
777
+ last = now;
778
+ }
779
+ animId = requestAnimationFrame(tick);
780
+ };
781
+ animId = requestAnimationFrame(tick);
782
+ return () => {
783
+ cancelled = true;
784
+ cancelAnimationFrame(animId);
785
+ };
786
+ }
787
+ async function asciifyVideo(source, canvas, { fontSize = 10, style = "classic", options = {} } = {}) {
788
+ let video;
789
+ if (typeof source === "string") {
790
+ video = document.createElement("video");
791
+ video.crossOrigin = "anonymous";
792
+ video.src = source;
793
+ await new Promise((resolve, reject) => {
794
+ video.onloadeddata = () => resolve();
795
+ video.onerror = () => reject(new Error(`Failed to load video: ${source}`));
796
+ });
797
+ } else {
798
+ video = source;
799
+ }
800
+ const merged = { ...DEFAULT_OPTIONS, ...ART_STYLE_PRESETS[style], ...options, fontSize };
801
+ const ctx = canvas.getContext("2d");
802
+ if (!ctx) throw new Error("Could not get 2d context from canvas");
803
+ const { frames, fps } = await videoToAsciiFrames(video, merged, canvas.width, canvas.height);
804
+ let cancelled = false;
805
+ let animId;
806
+ let i = 0;
807
+ let last = performance.now();
808
+ const interval = 1e3 / fps;
809
+ const tick = (now) => {
810
+ if (cancelled) return;
811
+ if (now - last >= interval) {
812
+ renderFrameToCanvas(ctx, frames[i], merged, canvas.width, canvas.height);
813
+ i = (i + 1) % frames.length;
814
+ last = now;
815
+ }
816
+ animId = requestAnimationFrame(tick);
817
+ };
818
+ animId = requestAnimationFrame(tick);
819
+ return () => {
820
+ cancelled = true;
821
+ cancelAnimationFrame(animId);
822
+ };
823
+ }
761
824
  function generateEmbedCode(frame, options, width, height) {
762
825
  const rows = frame.length;
763
826
  if (rows === 0) return "";
@@ -1395,6 +1458,8 @@ exports.CHARSETS = CHARSETS;
1395
1458
  exports.DEFAULT_OPTIONS = DEFAULT_OPTIONS;
1396
1459
  exports.HOVER_PRESETS = HOVER_PRESETS;
1397
1460
  exports.asciify = asciify;
1461
+ exports.asciifyGif = asciifyGif;
1462
+ exports.asciifyVideo = asciifyVideo;
1398
1463
  exports.generateAnimatedEmbedCode = generateAnimatedEmbedCode;
1399
1464
  exports.generateEmbedCode = generateEmbedCode;
1400
1465
  exports.gifToAsciiFrames = gifToAsciiFrames;