asciify-engine 1.0.2 → 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
@@ -731,6 +731,96 @@ function buildColorFnJS(options) {
731
731
  return `function(c){var g=Math.floor(.299*c.r+.587*c.g+.114*c.b);return 'rgb('+g+','+g+','+g+')'}`;
732
732
  }
733
733
  }
734
+ async function asciify(source, canvas, { fontSize = 10, style = "classic", options = {} } = {}) {
735
+ let el;
736
+ if (typeof source === "string") {
737
+ const img = new Image();
738
+ img.crossOrigin = "anonymous";
739
+ await new Promise((resolve, reject) => {
740
+ img.onload = () => resolve();
741
+ img.onerror = () => reject(new Error(`Failed to load image: ${source}`));
742
+ img.src = source;
743
+ });
744
+ el = img;
745
+ } else if (source instanceof HTMLImageElement && !source.complete) {
746
+ await new Promise((resolve, reject) => {
747
+ source.onload = () => resolve();
748
+ source.onerror = () => reject(new Error("Image failed to load"));
749
+ });
750
+ el = source;
751
+ } else {
752
+ el = source;
753
+ }
754
+ const preset = ART_STYLE_PRESETS[style];
755
+ const merged = { ...DEFAULT_OPTIONS, ...preset, ...options, fontSize };
756
+ const ctx = canvas.getContext("2d");
757
+ if (!ctx) throw new Error("Could not get 2d context from canvas");
758
+ const { frame } = imageToAsciiFrame(el, merged, canvas.width, canvas.height);
759
+ renderFrameToCanvas(ctx, frame, merged, canvas.width, canvas.height);
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
+ }
734
824
  function generateEmbedCode(frame, options, width, height) {
735
825
  const rows = frame.length;
736
826
  if (rows === 0) return "";
@@ -1367,6 +1457,9 @@ exports.ART_STYLE_PRESETS = ART_STYLE_PRESETS;
1367
1457
  exports.CHARSETS = CHARSETS;
1368
1458
  exports.DEFAULT_OPTIONS = DEFAULT_OPTIONS;
1369
1459
  exports.HOVER_PRESETS = HOVER_PRESETS;
1460
+ exports.asciify = asciify;
1461
+ exports.asciifyGif = asciifyGif;
1462
+ exports.asciifyVideo = asciifyVideo;
1370
1463
  exports.generateAnimatedEmbedCode = generateAnimatedEmbedCode;
1371
1464
  exports.generateEmbedCode = generateEmbedCode;
1372
1465
  exports.gifToAsciiFrames = gifToAsciiFrames;