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.cjs CHANGED
@@ -1162,23 +1162,31 @@ 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
- canvas.__cssW = cssW;
1181
- canvas.__cssH = cssH;
1182
1190
  }
1183
1191
  async function asciify(source, canvas, { fontSize = 10, artStyle = "classic", options = {} } = {}) {
1184
1192
  let el;
@@ -1255,22 +1263,16 @@ async function asciifyVideo(source, canvas, { fontSize = 10, artStyle = "classic
1255
1263
  } else {
1256
1264
  video2 = source;
1257
1265
  }
1258
- if (container) sizeCanvasToContainer(canvas, container, video2.videoWidth / video2.videoHeight);
1259
- const srcW = video2.videoWidth;
1260
- const srcH = video2.videoHeight;
1266
+ if (container) sizeCanvasToContainer(canvas, container, video2.videoWidth / video2.videoHeight, video2.videoWidth, video2.videoHeight);
1261
1267
  const maxDur = trimEnd !== void 0 ? trimEnd - trimStart : 10;
1262
- const { frames, fps } = await videoToAsciiFrames(video2, merged, srcW, srcH, void 0, maxDur, void 0, trimStart);
1268
+ const { frames, fps } = await videoToAsciiFrames(video2, merged, canvas.width, canvas.height, void 0, maxDur, void 0, trimStart);
1263
1269
  let cancelled2 = false, animId2, i = 0, last = performance.now();
1264
1270
  let firstFrame2 = true;
1265
1271
  const interval = 1e3 / fps;
1266
1272
  const tick2 = (now) => {
1267
1273
  if (cancelled2) return;
1268
- const cssW = canvas.__cssW || canvas.width;
1269
- const cssH = canvas.__cssH || canvas.height;
1270
1274
  if (now - last >= interval) {
1271
- const dpr = window.devicePixelRatio || 1;
1272
- ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
1273
- renderFrameToCanvas(ctx, frames[i], merged, cssW, cssH);
1275
+ renderFrameToCanvas(ctx, frames[i], merged, canvas.width, canvas.height);
1274
1276
  i = (i + 1) % frames.length;
1275
1277
  last = now;
1276
1278
  if (firstFrame2) {
@@ -1343,8 +1345,9 @@ async function asciifyVideo(source, canvas, { fontSize = 10, artStyle = "classic
1343
1345
  let ro = null;
1344
1346
  if (container) {
1345
1347
  const aspect = video.videoWidth / video.videoHeight;
1346
- sizeCanvasToContainer(canvas, container, aspect);
1347
- ro = new ResizeObserver(() => sizeCanvasToContainer(canvas, container, aspect));
1348
+ const vw = video.videoWidth, vh = video.videoHeight;
1349
+ sizeCanvasToContainer(canvas, container, aspect, vw, vh);
1350
+ ro = new ResizeObserver(() => sizeCanvasToContainer(canvas, container, aspect, vw, vh));
1348
1351
  ro.observe(container);
1349
1352
  }
1350
1353
  let cancelled = false;
@@ -1356,15 +1359,9 @@ async function asciifyVideo(source, canvas, { fontSize = 10, artStyle = "classic
1356
1359
  if (video.readyState < 2 || canvas.width === 0 || canvas.height === 0) return;
1357
1360
  if (trimStart > 0 && video.currentTime < trimStart) return;
1358
1361
  if (trimEnd !== void 0 && video.currentTime >= trimEnd) return;
1359
- const srcW = video.videoWidth;
1360
- const srcH = video.videoHeight;
1361
- const { frame } = imageToAsciiFrame(video, merged, srcW, srcH);
1362
+ const { frame } = imageToAsciiFrame(video, merged, canvas.width, canvas.height);
1362
1363
  if (frame.length > 0) {
1363
- const cssW = canvas.__cssW || canvas.width;
1364
- const cssH = canvas.__cssH || canvas.height;
1365
- const dpr = window.devicePixelRatio || 1;
1366
- ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
1367
- renderFrameToCanvas(ctx, frame, merged, cssW, cssH, 0, null);
1364
+ renderFrameToCanvas(ctx, frame, merged, canvas.width, canvas.height, 0, null);
1368
1365
  if (firstFrame) {
1369
1366
  firstFrame = false;
1370
1367
  onReady?.(video);