asciify-engine 1.0.47 → 1.0.49

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
@@ -648,11 +648,14 @@ function imageToAsciiFrame(source, options, targetWidth, targetHeight) {
648
648
  normRange = hi > lo ? hi - lo : 255;
649
649
  }
650
650
  const frame = [];
651
+ const ck = options.chromaKey;
652
+ const ckEnabled = ck != null && ck !== false;
653
+ const ckHeuristicGreen = ck === true;
654
+ const ckHeuristicBlue = ck === "blue-screen";
651
655
  let ckRGB = null;
652
656
  let ckTolSq = 0;
653
- const ck = options.chromaKey;
654
- if (ck != null && ck !== false) {
655
- ckRGB = ck === true ? { r: 0, g: 177, b: 64 } : parseChromaKeyColor(ck);
657
+ if (ckEnabled && !ckHeuristicGreen && !ckHeuristicBlue) {
658
+ ckRGB = parseChromaKeyColor(ck);
656
659
  ckTolSq = (options.chromaKeyTolerance ?? 60) ** 2;
657
660
  }
658
661
  for (let y = 0; y < rows; y++) {
@@ -663,11 +666,19 @@ function imageToAsciiFrame(source, options, targetWidth, targetHeight) {
663
666
  const g = pixels[i + 1];
664
667
  const b = pixels[i + 2];
665
668
  const a = pixels[i + 3];
666
- if (ckRGB !== null) {
667
- const dr = r - ckRGB.r;
668
- const dg = g - ckRGB.g;
669
- const db = b - ckRGB.b;
670
- if (dr * dr + dg * dg + db * db <= ckTolSq) {
669
+ if (ckEnabled) {
670
+ let keyed = false;
671
+ if (ckHeuristicGreen) {
672
+ keyed = g > r * 1.4 && g > b * 1.4 && g > 80;
673
+ } else if (ckHeuristicBlue) {
674
+ keyed = b > r * 1.4 && b > g * 1.4 && b > 80;
675
+ } else if (ckRGB !== null) {
676
+ const dr = r - ckRGB.r;
677
+ const dg = g - ckRGB.g;
678
+ const db = b - ckRGB.b;
679
+ keyed = dr * dr + dg * dg + db * db <= ckTolSq;
680
+ }
681
+ if (keyed) {
671
682
  row.push({ char: " ", r: 0, g: 0, b: 0, a: 0 });
672
683
  continue;
673
684
  }
@@ -1102,6 +1113,7 @@ async function asciifyVideo(source, canvas, { fontSize = 10, artStyle = "classic
1102
1113
  onReady?.(video2);
1103
1114
  const { frames, fps } = await videoToAsciiFrames(video2, merged, canvas.width, canvas.height);
1104
1115
  let cancelled2 = false, animId2, i = 0, last = performance.now();
1116
+ let firstFrame2 = true;
1105
1117
  const interval = 1e3 / fps;
1106
1118
  const tick2 = (now) => {
1107
1119
  if (cancelled2) return;
@@ -1109,6 +1121,10 @@ async function asciifyVideo(source, canvas, { fontSize = 10, artStyle = "classic
1109
1121
  renderFrameToCanvas(ctx, frames[i], merged, canvas.width, canvas.height);
1110
1122
  i = (i + 1) % frames.length;
1111
1123
  last = now;
1124
+ if (firstFrame2) {
1125
+ firstFrame2 = false;
1126
+ onReady?.(video2);
1127
+ }
1112
1128
  onFrame?.();
1113
1129
  }
1114
1130
  animId2 = requestAnimationFrame(tick2);
@@ -1158,9 +1174,9 @@ async function asciifyVideo(source, canvas, { fontSize = 10, artStyle = "classic
1158
1174
  ro = new ResizeObserver(() => sizeCanvasToContainer(canvas, container, aspect));
1159
1175
  ro.observe(container);
1160
1176
  }
1161
- onReady?.(video);
1162
1177
  let cancelled = false;
1163
1178
  let animId;
1179
+ let firstFrame = true;
1164
1180
  const tick = () => {
1165
1181
  if (cancelled) return;
1166
1182
  animId = requestAnimationFrame(tick);
@@ -1168,6 +1184,10 @@ async function asciifyVideo(source, canvas, { fontSize = 10, artStyle = "classic
1168
1184
  const { frame } = imageToAsciiFrame(video, merged, canvas.width, canvas.height);
1169
1185
  if (frame.length > 0) {
1170
1186
  renderFrameToCanvas(ctx, frame, merged, canvas.width, canvas.height, 0, null);
1187
+ if (firstFrame) {
1188
+ firstFrame = false;
1189
+ onReady?.(video);
1190
+ }
1171
1191
  onFrame?.();
1172
1192
  }
1173
1193
  };