Skip to content

Commit

Permalink
fix cropping, probably, in new code path
Browse files Browse the repository at this point in the history
  • Loading branch information
Fauntleroy committed Sep 22, 2024
1 parent 92808e2 commit 3676c7f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/components/new-message.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@
currentFrameNumber = frameNumber;
}
const filmstripData = await generateFilmstripWithCallback(videoElement, handleFrame);
const filmstripData = await generateFilmstripWithCallback(videoElement, handleFrame, {
width: 200,
height: 150
});
const optimizedImageArrayBuffer = uPNG.encode(
[filmstripData.data.buffer],
filmstripData.width,
Expand Down
5 changes: 4 additions & 1 deletion src/routes/(app)/camera/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@
currentFrameNumber = frameNumber;
}
const filmstripData = await generateFilmstripWithCallback(videoElement, handleFrame);
const filmstripData = await generateFilmstripWithCallback(videoElement, handleFrame, {
width,
height
});
const gifBlob = await generateGIF(
filmstripData,
$colorPalette as [string, string],
Expand Down
28 changes: 25 additions & 3 deletions src/utils/frames.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
import Dither from 'canvas-dither';

import { getCrop } from './webcam';

interface FilmstripOptions {
frameCount: number;
frameRate: number;
width: number;
height: number;
}

const defaultFilmstripOptions: FilmstripOptions = {
frameCount: 20,
frameRate: 10
frameRate: 10,
width: 200,
height: 150
};

export function generateFilmstripWithCallback(
video: HTMLVideoElement,
frameCallback: (frameNumber: number) => void,
filmstripOptions?: Partial<FilmstripOptions> | undefined
): Promise<ImageData> {
const { frameCount, frameRate } = { ...defaultFilmstripOptions, ...filmstripOptions };
const { frameCount, frameRate, width, height } = {
...defaultFilmstripOptions,
...filmstripOptions
};

return new Promise((resolve, reject) => {
let canvas: HTMLCanvasElement = document.createElement('canvas');
Expand All @@ -36,7 +45,20 @@ export function generateFilmstripWithCallback(

// iterate through frames
if (capturedFrameCount < frameCount) {
context.drawImage(video, 0, capturedFrameCount * video.height, video.width, video.height);
const { x, y, width: cropWidth, height: cropHeight } = getCrop(video, width / height);
context.drawImage(
video,
x,
y,
cropWidth,
cropHeight, // source (webcam)
0,
capturedFrameCount * height,
width,
height // destination (canvas)
);

// context.drawImage(video, 0, capturedFrameCount * video.height, video.width, video.height);
capturedFrameCount++;
frameCallback(capturedFrameCount);

Expand Down

0 comments on commit 3676c7f

Please sign in to comment.