asciify-engine 1.0.66 → 1.0.68

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.js CHANGED
@@ -1160,23 +1160,31 @@ function renderFrameToCanvas(ctx, frame, options, canvasWidth, canvasHeight, tim
1160
1160
  }
1161
1161
 
1162
1162
  // src/core/simple-api.ts
1163
- function sizeCanvasToContainer(canvas, container, aspect) {
1163
+ function sizeCanvasToContainer(canvas, container, aspect, srcW, srcH) {
1164
1164
  const { width, height } = container.getBoundingClientRect();
1165
1165
  if (!width || !height) return;
1166
- let w = width, h = w / aspect;
1167
- if (h > height) {
1168
- h = height;
1169
- w = h * aspect;
1170
- }
1171
- const dpr = typeof window !== "undefined" ? window.devicePixelRatio || 1 : 1;
1172
- const cssW = Math.round(w);
1173
- const cssH = Math.round(h);
1174
- canvas.width = Math.round(cssW * dpr);
1175
- canvas.height = Math.round(cssH * dpr);
1166
+ let cssW = width, cssH = cssW / aspect;
1167
+ if (cssH > height) {
1168
+ cssH = height;
1169
+ cssW = cssH * aspect;
1170
+ }
1171
+ cssW = Math.round(cssW);
1172
+ cssH = Math.round(cssH);
1173
+ const MAX_BUF = 2048;
1174
+ let bufW, bufH;
1175
+ if (srcW && srcH && (srcW > cssW || srcH > cssH)) {
1176
+ const scale = Math.min(1, MAX_BUF / Math.max(srcW, srcH));
1177
+ bufW = Math.round(srcW * scale);
1178
+ bufH = Math.round(srcH * scale);
1179
+ } else {
1180
+ const dpr = typeof window !== "undefined" ? window.devicePixelRatio || 1 : 1;
1181
+ bufW = Math.round(cssW * dpr);
1182
+ bufH = Math.round(cssH * dpr);
1183
+ }
1184
+ canvas.width = bufW;
1185
+ canvas.height = bufH;
1176
1186
  canvas.style.width = cssW + "px";
1177
1187
  canvas.style.height = cssH + "px";
1178
- canvas.__cssW = cssW;
1179
- canvas.__cssH = cssH;
1180
1188
  }
1181
1189
  async function asciify(source, canvas, { fontSize = 10, artStyle = "classic", options = {} } = {}) {
1182
1190
  let el;
@@ -1253,22 +1261,16 @@ async function asciifyVideo(source, canvas, { fontSize = 10, artStyle = "classic
1253
1261
  } else {
1254
1262
  video2 = source;
1255
1263
  }
1256
- if (container) sizeCanvasToContainer(canvas, container, video2.videoWidth / video2.videoHeight);
1257
- const srcW = video2.videoWidth;
1258
- const srcH = video2.videoHeight;
1264
+ if (container) sizeCanvasToContainer(canvas, container, video2.videoWidth / video2.videoHeight, video2.videoWidth, video2.videoHeight);
1259
1265
  const maxDur = trimEnd !== void 0 ? trimEnd - trimStart : 10;
1260
- const { frames, fps } = await videoToAsciiFrames(video2, merged, srcW, srcH, void 0, maxDur, void 0, trimStart);
1266
+ const { frames, fps } = await videoToAsciiFrames(video2, merged, canvas.width, canvas.height, void 0, maxDur, void 0, trimStart);
1261
1267
  let cancelled2 = false, animId2, i = 0, last = performance.now();
1262
1268
  let firstFrame2 = true;
1263
1269
  const interval = 1e3 / fps;
1264
1270
  const tick2 = (now) => {
1265
1271
  if (cancelled2) return;
1266
- const cssW = canvas.__cssW || canvas.width;
1267
- const cssH = canvas.__cssH || canvas.height;
1268
1272
  if (now - last >= interval) {
1269
- const dpr = window.devicePixelRatio || 1;
1270
- ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
1271
- renderFrameToCanvas(ctx, frames[i], merged, cssW, cssH);
1273
+ renderFrameToCanvas(ctx, frames[i], merged, canvas.width, canvas.height);
1272
1274
  i = (i + 1) % frames.length;
1273
1275
  last = now;
1274
1276
  if (firstFrame2) {
@@ -1341,8 +1343,9 @@ async function asciifyVideo(source, canvas, { fontSize = 10, artStyle = "classic
1341
1343
  let ro = null;
1342
1344
  if (container) {
1343
1345
  const aspect = video.videoWidth / video.videoHeight;
1344
- sizeCanvasToContainer(canvas, container, aspect);
1345
- ro = new ResizeObserver(() => sizeCanvasToContainer(canvas, container, aspect));
1346
+ const vw = video.videoWidth, vh = video.videoHeight;
1347
+ sizeCanvasToContainer(canvas, container, aspect, vw, vh);
1348
+ ro = new ResizeObserver(() => sizeCanvasToContainer(canvas, container, aspect, vw, vh));
1346
1349
  ro.observe(container);
1347
1350
  }
1348
1351
  let cancelled = false;
@@ -1354,15 +1357,9 @@ async function asciifyVideo(source, canvas, { fontSize = 10, artStyle = "classic
1354
1357
  if (video.readyState < 2 || canvas.width === 0 || canvas.height === 0) return;
1355
1358
  if (trimStart > 0 && video.currentTime < trimStart) return;
1356
1359
  if (trimEnd !== void 0 && video.currentTime >= trimEnd) return;
1357
- const srcW = video.videoWidth;
1358
- const srcH = video.videoHeight;
1359
- const { frame } = imageToAsciiFrame(video, merged, srcW, srcH);
1360
+ const { frame } = imageToAsciiFrame(video, merged, canvas.width, canvas.height);
1360
1361
  if (frame.length > 0) {
1361
- const cssW = canvas.__cssW || canvas.width;
1362
- const cssH = canvas.__cssH || canvas.height;
1363
- const dpr = window.devicePixelRatio || 1;
1364
- ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
1365
- renderFrameToCanvas(ctx, frame, merged, cssW, cssH, 0, null);
1362
+ renderFrameToCanvas(ctx, frame, merged, canvas.width, canvas.height, 0, null);
1366
1363
  if (firstFrame) {
1367
1364
  firstFrame = false;
1368
1365
  onReady?.(video);