Skip to content

Commit c43b0d9

Browse files
committed
add snapshot function to replay canvas
1 parent 0940301 commit c43b0d9

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

packages/replay/src/canvas.ts

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
import { getCanvasManager } from '@sentry-internal/rrweb';
1+
import { getCanvasManager, _getCanvasManager } from '@sentry-internal/rrweb';
22
import type { Integration } from '@sentry/types';
3-
import type { ReplayConfiguration } from './types';
3+
import type { CanvasManagerInterface, GetCanvasManagerOptions, ReplayConfiguration } from './types';
4+
import { CANVAS_QUALITY } from './constants';
45

56
interface ReplayCanvasOptions {
6-
fps: number;
7-
quality: number;
7+
canvasQuality: {
8+
sampling: {
9+
canvas: number;
10+
};
11+
dataURLOptions: {
12+
type: string;
13+
quality: number;
14+
};
15+
};
16+
manualSnapshot?: boolean;
817
}
918

1019
/** An integration to add canvas recording to replay. */
@@ -19,16 +28,20 @@ export class ReplayCanvas implements Integration {
1928
*/
2029
public name: string;
2130

31+
public canvasManager: (options: GetCanvasManagerOptions) => CanvasManagerInterface;
32+
2233
private _canvasOptions: ReplayCanvasOptions;
2334

2435
public constructor() {
2536
this.name = ReplayCanvas.id;
2637
// TODO FN: Allow to configure this
2738
// But since we haven't finalized how to configure this, this is predefined for now
2839
// to avoid breaking changes
40+
this.canvasManager = getCanvasManager;
2941
this._canvasOptions = {
30-
fps: 4,
31-
quality: 0.6,
42+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
43+
canvasQuality: CANVAS_QUALITY.medium,
44+
manualSnapshot: true,
3245
};
3346
}
3447

@@ -46,9 +59,32 @@ export class ReplayCanvas implements Integration {
4659
_experiments: {
4760
canvas: {
4861
...this._canvasOptions,
49-
manager: getCanvasManager,
62+
manager: opts =>
63+
this.canvasManager({
64+
...opts,
65+
manualSnapshot: this._canvasOptions.manualSnapshot,
66+
}),
5067
},
5168
},
5269
};
5370
}
71+
72+
/**
73+
* Take manual snapshot of canvas
74+
*/
75+
public snapshotCanvas(canvasElement?: HTMLCanvasElement): void {
76+
const canvasManager: CanvasManagerInterface = _getCanvasManager(this.canvasManager, {
77+
recordCanvas: true,
78+
blockClass: '',
79+
blockSelector: null,
80+
unblockSelector: null,
81+
dataURLOptions: this._canvasOptions.canvasQuality.dataURLOptions,
82+
});
83+
84+
return canvasManager.snapshot(
85+
this._canvasOptions.canvasQuality.dataURLOptions,
86+
this._canvasOptions.canvasQuality.sampling.canvas,
87+
canvasElement,
88+
);
89+
}
5490
}

packages/replay/src/types/replay.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ export interface ReplayPluginOptions extends ReplayNetworkOptions {
234234
traceInternals: boolean;
235235
canvas: {
236236
quality?: 'low' | 'medium' | 'high';
237+
manualSnapshot?: boolean;
237238
manager: (options: GetCanvasManagerOptions) => CanvasManagerInterface;
238239
};
239240
}>;

packages/replay/src/types/rrweb.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,18 @@ export interface CanvasManagerInterface {
5151
unfreeze(): void;
5252
lock(): void;
5353
unlock(): void;
54+
snapshot(
55+
dataURLOptions: Partial<{
56+
type: string;
57+
quality: number;
58+
}>,
59+
sampling: number,
60+
canvasElement?: HTMLCanvasElement,
61+
): void;
5462
}
5563

5664
export interface GetCanvasManagerOptions {
65+
manualSnapshot?: boolean;
5766
recordCanvas: boolean;
5867
blockClass: string | RegExp;
5968
blockSelector: string | null;

0 commit comments

Comments
 (0)