Skip to content

Commit

Permalink
Merge pull request #135 from apolyanov/fix/will-read-frequently
Browse files Browse the repository at this point in the history
fix: Adding willReadFrequently makeBufferFromCanvasImageData method to improve performance
  • Loading branch information
werthdavid authored May 22, 2024
2 parents d4c22f7 + e0208d8 commit c1de1e8
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/common/HTMLCanvasElementLuminanceSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ export class HTMLCanvasElementLuminanceSource extends LuminanceSource {
private static DEGREE_TO_RADIANS = Math.PI / 180;

private static makeBufferFromCanvasImageData(canvas: HTMLCanvasElement): Uint8ClampedArray {
const canvasCtx = canvas.getContext('2d');
let canvasCtx;

try {
canvasCtx = canvas.getContext('2d', { willReadFrequently: true });
} catch (e) {
canvasCtx = canvas.getContext('2d');
}

if (!canvasCtx) { throw new Error('Couldn\'t get canvas context.'); }
const imageData = canvasCtx.getImageData(0, 0, canvas.width, canvas.height);
return HTMLCanvasElementLuminanceSource.toGrayscaleBuffer(imageData.data, canvas.width, canvas.height);
Expand Down

0 comments on commit c1de1e8

Please sign in to comment.