asciify-engine 1.0.2 → 1.0.3
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 +28 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +21 -1
- package/dist/index.d.ts +21 -1
- package/dist/index.js +28 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -731,6 +731,33 @@ 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
|
+
}
|
|
734
761
|
function generateEmbedCode(frame, options, width, height) {
|
|
735
762
|
const rows = frame.length;
|
|
736
763
|
if (rows === 0) return "";
|
|
@@ -1367,6 +1394,7 @@ exports.ART_STYLE_PRESETS = ART_STYLE_PRESETS;
|
|
|
1367
1394
|
exports.CHARSETS = CHARSETS;
|
|
1368
1395
|
exports.DEFAULT_OPTIONS = DEFAULT_OPTIONS;
|
|
1369
1396
|
exports.HOVER_PRESETS = HOVER_PRESETS;
|
|
1397
|
+
exports.asciify = asciify;
|
|
1370
1398
|
exports.generateAnimatedEmbedCode = generateAnimatedEmbedCode;
|
|
1371
1399
|
exports.generateEmbedCode = generateEmbedCode;
|
|
1372
1400
|
exports.gifToAsciiFrames = gifToAsciiFrames;
|