asciify-engine 1.0.67 → 1.0.69

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
@@ -1162,23 +1162,33 @@ function renderFrameToCanvas(ctx, frame, options, canvasWidth, canvasHeight, tim
1162
1162
  }
1163
1163
 
1164
1164
  // src/core/simple-api.ts
1165
- function sizeCanvasToContainer(canvas, container, aspect) {
1165
+ function sizeCanvasToContainer(canvas, container, aspect, srcW, srcH) {
1166
1166
  const { width, height } = container.getBoundingClientRect();
1167
1167
  if (!width || !height) return;
1168
- let w = width, h = w / aspect;
1169
- if (h > height) {
1170
- h = height;
1171
- w = h * aspect;
1172
- }
1173
- const dpr = typeof window !== "undefined" ? window.devicePixelRatio || 1 : 1;
1174
- const cssW = Math.round(w);
1175
- const cssH = Math.round(h);
1176
- canvas.width = Math.round(cssW * dpr);
1177
- canvas.height = Math.round(cssH * dpr);
1168
+ let cssW = width, cssH = cssW / aspect;
1169
+ if (cssH > height) {
1170
+ cssH = height;
1171
+ cssW = cssH * aspect;
1172
+ }
1173
+ cssW = Math.round(cssW);
1174
+ cssH = Math.round(cssH);
1175
+ const MAX_BUF = 2048;
1176
+ let bufW, bufH;
1177
+ if (srcW && srcH && (srcW > cssW || srcH > cssH)) {
1178
+ const scale = Math.min(1, MAX_BUF / Math.max(srcW, srcH));
1179
+ bufW = Math.round(srcW * scale);
1180
+ bufH = Math.round(srcH * scale);
1181
+ } else {
1182
+ const dpr = typeof window !== "undefined" ? window.devicePixelRatio || 1 : 1;
1183
+ bufW = Math.round(cssW * dpr);
1184
+ bufH = Math.round(cssH * dpr);
1185
+ }
1186
+ canvas.width = bufW;
1187
+ canvas.height = bufH;
1178
1188
  canvas.style.width = cssW + "px";
1179
1189
  canvas.style.height = cssH + "px";
1180
1190
  }
1181
- async function asciify(source, canvas, { fontSize = 10, artStyle = "classic", options = {} } = {}) {
1191
+ async function asciify(source, canvas, { fontSize, artStyle = "classic", options = {} } = {}) {
1182
1192
  let el;
1183
1193
  if (typeof source === "string") {
1184
1194
  const img = new Image();
@@ -1199,15 +1209,17 @@ async function asciify(source, canvas, { fontSize = 10, artStyle = "classic", op
1199
1209
  el = source;
1200
1210
  }
1201
1211
  const preset = ART_STYLE_PRESETS[artStyle];
1202
- const merged = { ...DEFAULT_OPTIONS, ...preset, ...options, fontSize };
1212
+ const resolvedFontSize = fontSize ?? options.fontSize ?? 10;
1213
+ const merged = { ...DEFAULT_OPTIONS, ...preset, ...options, fontSize: resolvedFontSize };
1203
1214
  const ctx = canvas.getContext("2d");
1204
1215
  if (!ctx) throw new Error("Could not get 2d context from canvas");
1205
1216
  const { frame } = imageToAsciiFrame(el, merged, canvas.width, canvas.height);
1206
1217
  renderFrameToCanvas(ctx, frame, merged, canvas.width, canvas.height);
1207
1218
  }
1208
- async function asciifyGif(source, canvas, { fontSize = 10, artStyle = "classic", options = {} } = {}) {
1219
+ async function asciifyGif(source, canvas, { fontSize, artStyle = "classic", options = {} } = {}) {
1209
1220
  const buffer = typeof source === "string" ? await fetch(source).then((r) => r.arrayBuffer()) : source;
1210
- const merged = { ...DEFAULT_OPTIONS, ...ART_STYLE_PRESETS[artStyle], ...options, fontSize };
1221
+ const resolvedFontSize = fontSize ?? options.fontSize ?? 10;
1222
+ const merged = { ...DEFAULT_OPTIONS, ...ART_STYLE_PRESETS[artStyle], ...options, fontSize: resolvedFontSize };
1211
1223
  const ctx = canvas.getContext("2d");
1212
1224
  if (!ctx) throw new Error("Could not get 2d context from canvas");
1213
1225
  const { frames, fps } = await gifToAsciiFrames(buffer, merged, canvas.width, canvas.height);
@@ -1231,10 +1243,11 @@ async function asciifyGif(source, canvas, { fontSize = 10, artStyle = "classic",
1231
1243
  cancelAnimationFrame(animId);
1232
1244
  };
1233
1245
  }
1234
- async function asciifyVideo(source, canvas, { fontSize = 10, artStyle = "classic", options = {}, fitTo, preExtract = false, trim, onReady, onFrame } = {}) {
1246
+ async function asciifyVideo(source, canvas, { fontSize, artStyle = "classic", options = {}, fitTo, preExtract = false, trim, onReady, onFrame } = {}) {
1235
1247
  const trimStart = trim?.start ?? 0;
1236
1248
  const trimEnd = trim?.end;
1237
- const merged = { ...DEFAULT_OPTIONS, ...ART_STYLE_PRESETS[artStyle], ...options, fontSize };
1249
+ const resolvedFontSize = fontSize ?? options.fontSize ?? 10;
1250
+ const merged = { ...DEFAULT_OPTIONS, ...ART_STYLE_PRESETS[artStyle], ...options, fontSize: resolvedFontSize };
1238
1251
  const ctx = canvas.getContext("2d");
1239
1252
  if (!ctx) throw new Error("asciifyVideo: could not get 2d context from canvas.");
1240
1253
  const container = typeof fitTo === "string" ? document.querySelector(fitTo) : fitTo instanceof HTMLElement ? fitTo : null;
@@ -1253,7 +1266,7 @@ async function asciifyVideo(source, canvas, { fontSize = 10, artStyle = "classic
1253
1266
  } else {
1254
1267
  video2 = source;
1255
1268
  }
1256
- if (container) sizeCanvasToContainer(canvas, container, video2.videoWidth / video2.videoHeight);
1269
+ if (container) sizeCanvasToContainer(canvas, container, video2.videoWidth / video2.videoHeight, video2.videoWidth, video2.videoHeight);
1257
1270
  const maxDur = trimEnd !== void 0 ? trimEnd - trimStart : 10;
1258
1271
  const { frames, fps } = await videoToAsciiFrames(video2, merged, canvas.width, canvas.height, void 0, maxDur, void 0, trimStart);
1259
1272
  let cancelled2 = false, animId2, i = 0, last = performance.now();
@@ -1335,8 +1348,9 @@ async function asciifyVideo(source, canvas, { fontSize = 10, artStyle = "classic
1335
1348
  let ro = null;
1336
1349
  if (container) {
1337
1350
  const aspect = video.videoWidth / video.videoHeight;
1338
- sizeCanvasToContainer(canvas, container, aspect);
1339
- ro = new ResizeObserver(() => sizeCanvasToContainer(canvas, container, aspect));
1351
+ const vw = video.videoWidth, vh = video.videoHeight;
1352
+ sizeCanvasToContainer(canvas, container, aspect, vw, vh);
1353
+ ro = new ResizeObserver(() => sizeCanvasToContainer(canvas, container, aspect, vw, vh));
1340
1354
  ro.observe(container);
1341
1355
  }
1342
1356
  let cancelled = false;