Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Add frame capture support to canvas controls

Richie McIlroyd59678352caf
diff --git a/apps/desktop/src/routes/editor/Editor.tsx b/apps/desktop/src/routes/editor/Editor.tsx
index a83f558fb..e9be53c2e 100644
--- a/apps/desktop/src/routes/editor/Editor.tsx
+++ b/apps/desktop/src/routes/editor/Editor.tsx
@@ -129,6 +129,11 @@ function Inner() {
return "type" in d && d.type === "export" && d.open;
};
+ const isCropMode = () => {
+ const d = dialog();
+ return "type" in d && d.type === "crop" && d.open;
+ };
+
const [layoutRef, setLayoutRef] = createSignal<HTMLDivElement>();
const layoutBounds = createElementBounds(layoutRef);
const [storedTimelineHeight, setStoredTimelineHeight] = makePersisted(
@@ -231,6 +236,14 @@ function Inner() {
}),
);
+ createEffect(
+ on(isCropMode, (cropMode, prevCropMode) => {
+ if (prevCropMode === true && cropMode === false) {
+ emitRenderFrame(frameNumberToRender());
+ }
+ }),
+ );
+
const doConfigUpdate = async (time: number) => {
const config = serializeProjectConfiguration(project);
const frameNumber = Math.max(Math.floor(time * FPS), 0);
@@ -481,8 +494,11 @@ function Dialogs() {
})()}
>
{(dialog) => {
- const { setProject: setState, editorInstance } =
- useEditorContext();
+ const {
+ setProject: setState,
+ editorInstance,
+ canvasControls,
+ } = useEditorContext();
const display = editorInstance.recordings.segments[0].display;
let cropperRef: CropperRef | undefined;
@@ -493,16 +509,14 @@ function Dialogs() {
string | null
>(null);
- const playerCanvas = document.getElementById(
- "canvas",
- ) as HTMLCanvasElement | null;
- if (playerCanvas) {
- playerCanvas.toBlob((blob) => {
+ const controls = canvasControls();
+ if (controls) {
+ controls.captureFrame().then((blob) => {
if (blob) {
const url = URL.createObjectURL(blob);
setFrameBlobUrl(url);
}
- }, "image/png");
+ });
}
onCleanup(() => {
diff --git a/apps/desktop/src/utils/socket.ts b/apps/desktop/src/utils/socket.ts
index 3b603025d..a4a58cc76 100644
--- a/apps/desktop/src/utils/socket.ts
+++ b/apps/desktop/src/utils/socket.ts
@@ -92,6 +92,7 @@ export type CanvasControls = {
hasRenderedFrame: () => boolean;
initDirectCanvas: (canvas: HTMLCanvasElement) => void;
resetFrameState: () => void;
+ captureFrame: () => Promise<Blob | null>;
};
interface ReadyMessage {
@@ -190,6 +191,8 @@ export function createImageDataWS(
let mainThreadWebGPUInitializing = false;
let pendingNv12Frame: ArrayBuffer | null = null;
+ let lastRenderedImageData: ImageData | null = null;
+
function cleanup() {
if (isCleanedUp) return;
isCleanedUp = true;
@@ -225,6 +228,8 @@ export function createImageDataWS(
cachedStrideWidth = 0;
cachedStrideHeight = 0;
+ lastRenderedImageData = null;
+
setIsConnected(false);
}
@@ -266,6 +271,18 @@ export function createImageDataWS(
yStride,
);
+ const rgba = convertNv12ToRgbaMainThread(
+ frameData,
+ width,
+ height,
+ yStride,
+ );
+ lastRenderedImageData = new ImageData(
+ new Uint8ClampedArray(rgba),
+ width,
+ height,
+ );
+
if (!hasRenderedFrame()) {
setHasRenderedFrame(true);
}
@@ -309,8 +326,13 @@ export function createImageDataWS(
height,
yStride,
);
- const imageData = new ImageData(rgba, width, height);
+ const imageData = new ImageData(
+ new Uint8ClampedArray(rgba),
+ width,
+ height,
+ );
directCtx.putImageData(imageData, 0, 0);
+ lastRenderedImageData = imageData;
if (!hasRenderedFrame()) {
setHasRenderedFrame(true);
@@ -398,6 +420,11 @@ export function createImageDataWS(
}
cachedStrideImageData.data.set(frameData);
directCtx.putImageData(cachedStrideImageData, 0, 0);
+ lastRenderedImageData = new ImageData(
+ new Uint8ClampedArray(cachedStrideImageData.data),
+ width,
+ height,
+ );
if (!hasRenderedFrame()) {
setHasRenderedFrame(true);
@@ -408,6 +435,22 @@ export function createImageDataWS(
resetFrameState: () => {
worker.postMessage({ type: "reset-frame-state" });
},
+ captureFrame: async () => {
+ if (!lastRenderedImageData) {
+ return null;
+ }
+ const canvas = document.createElement("canvas");
+ canvas.width = lastRenderedImageData.width;
+ canvas.height = lastRenderedImageData.height;
+ const ctx = canvas.getContext("2d");
+ if (!ctx) {
+ return null;
+ }
+ ctx.putImageData(lastRenderedImageData, 0, 0);
+ return new Promise<Blob | null>((resolve) => {
+ canvas.toBlob((blob) => resolve(blob), "image/png");
+ });
+ },
};
worker.onmessage = (e: MessageEvent<WorkerMessage>) => {
@@ -600,6 +643,18 @@ export function createImageDataWS(
actualRendersCount++;
renderFrameCount++;
+ const rgba = convertNv12ToRgbaMainThread(
+ frameData,
+ width,
+ height,
+ yStride,
+ );
+ lastRenderedImageData = new ImageData(
+ new Uint8ClampedArray(rgba),
+ width,
+ height,
+ );
+
if (!hasRenderedFrame()) {
setHasRenderedFrame(true);
}
@@ -675,6 +730,11 @@ export function createImageDataWS(
}
cachedDirectImageData.data.set(rgbaData);
directCtx.putImageData(cachedDirectImageData, 0, 0);
+ lastRenderedImageData = new ImageData(
+ new Uint8ClampedArray(cachedDirectImageData.data),
+ width,
+ height,
+ );
actualRendersCount++;
renderFrameCount++;
@@ -761,6 +821,11 @@ export function createImageDataWS(
}
cachedDirectImageData.data.set(frameData);
directCtx.putImageData(cachedDirectImageData, 0, 0);
+ lastRenderedImageData = new ImageData(
+ new Uint8ClampedArray(cachedDirectImageData.data),
+ width,
+ height,
+ );
renderFrameCount++;
if (!hasRenderedFrame()) {
diff --git a/apps/desktop/src/utils/tauri.ts b/apps/desktop/src/utils/tauri.ts
index b14f88498..40623119e 100644
--- a/apps/desktop/src/utils/tauri.ts
+++ b/apps/desktop/src/utils/tauri.ts
@@ -372,6 +372,7 @@ uploadProgressEvent: "upload-progress-event"
/** user-defined types **/
+export type AllGpusInfo = { gpus: GpuInfoDiag[]; primaryGpuIndex: number | null; isMultiGpuSystem: boolean; hasDiscreteGpu: boolean }
export type Annotation = { id: string; type: AnnotationType; x: number; y: number; width: number; height: number; strokeColor: string; strokeWidth: number; fillColor: string; opacity: number; rotation: number; text: string | null; maskType?: MaskType | null; maskLevel?: number | null }
export type AnnotationType = "arrow" | "circle" | "rectangle" | "text" | "mask"
export type AppTheme = "system" | "light" | "dark"
@@ -440,6 +441,7 @@ quality: number | null;
* Whether to prioritize speed over quality (default: false)
*/
fast: boolean | null }
+export type GpuInfoDiag = { vendor: string; description: string; dedicatedVideoMemoryMb: number; adapterIndex: number; isSoftwareAdapter: boolean; isBasicRenderDriver: boolean; supportsHardwareEncoding: boolean }
export type HapticPattern = "alignment" | "levelChange" | "generic"
export type HapticPerformanceTime = "default" | "now" | "drawCompleted"
export type Hotkey = { code: string; meta: boolean; ctrl: boolean; alt: boolean; shift: boolean }
@@ -452,7 +454,6 @@ export type JsonValue<T> = [T]
export type LogicalBounds = { position: LogicalPosition; size: LogicalSize }
export type LogicalPosition = { x: number; y: number }
export type LogicalSize = { width: number; height: number }
-export type MacOSVersionInfo = { displayName: string }
export type MainWindowRecordingStartBehaviour = "close" | "minimise"
export type MaskKeyframes = { position?: MaskVectorKeyframe[]; size?: MaskVectorKeyframe[]; intensity?: MaskScalarKeyframe[] }
export type MaskKind = "sensitive" | "highlight"
@@ -495,6 +496,7 @@ export type RecordingStatus = "pending" | "recording"
export type RecordingStopped = null
export type RecordingTargetMode = "display" | "window" | "area"
export type RenderFrameEvent = { frame_number: number; fps: number; resolution_base: XY<number> }
+export type RenderingStatus = { isUsingSoftwareRendering: boolean; isUsingBasicRenderDriver: boolean; hardwareEncodingAvailable: boolean; warningMessage: string | null }
export type RequestOpenRecordingPicker = { target_mode: RecordingTargetMode | null }
export type RequestOpenSettings = { page: string }
export type RequestScreenCapturePrewarm = { force?: boolean }
@@ -515,7 +517,7 @@ export type StartRecordingInputs = { capture_target: ScreenCaptureTarget; captur
export type StereoMode = "stereo" | "monoL" | "monoR"
export type StudioRecordingMeta = { segment: SingleSegment } | { inner: MultipleSegments }
export type StudioRecordingStatus = { status: "InProgress" } | { status: "NeedsRemux" } | { status: "Failed"; error: string } | { status: "Complete" }
-export type SystemDiagnostics = { macosVersion: MacOSVersionInfo | null; availableEncoders: string[]; screenCaptureSupported: boolean }
+export type SystemDiagnostics = { windowsVersion: WindowsVersionInfo | null; gpuInfo: GpuInfoDiag | null; allGpus: AllGpusInfo | null; renderingStatus: RenderingStatus; availableEncoders: string[]; graphicsCaptureSupported: boolean; d3D11VideoProcessorAvailable: boolean }
export type TargetUnderCursor = { display_id: DisplayId | null; window: WindowUnderCursor | null }
export type TextSegment = { start: number; end: number; enabled?: boolean; content?: string; center?: XY<number>; size?: XY<number>; fontFamily?: string; fontSize?: number; fontWeight?: number; italic?: boolean; color?: string; fadeDuration?: number }
export type TimelineConfiguration = { segments: TimelineSegment[]; zoomSegments: ZoomSegment[]; sceneSegments?: SceneSegment[]; maskSegments?: MaskSegment[]; textSegments?: TextSegment[] }
@@ -532,6 +534,7 @@ export type VideoUploadInfo = { id: string; link: string; config: S3UploadMeta }
export type WindowExclusion = { bundleIdentifier?: string | null; ownerName?: string | null; windowTitle?: string | null }
export type WindowId = string
export type WindowUnderCursor = { id: WindowId; app_name: string; bounds: LogicalBounds }
+export type WindowsVersionInfo = { major: number; minor: number; build: number; displayName: string; meetsRequirements: boolean; isWindows11: boolean }
export type XY<T> = { x: T; y: T }
export type ZoomMode = "auto" | { manual: { x: number; y: number } }
export type ZoomSegment = { start: number; end: number; amount: number; mode: ZoomMode }