Skip to content
Snippets Groups Projects
Commit 8fcc4b0d authored by Reiter, Christoph's avatar Reiter, Christoph :snake:
Browse files

Draw into a temporary canvas first and then copy it to the DOM one

This allows the detection to be async and pasue execution between capturing
the image, analyzing it and then finally drawing it.
parent 435beae0
No related branches found
No related tags found
No related merge requests found
...@@ -201,7 +201,8 @@ export class QrCodeScanner extends ScopedElementsMixin(DBPLitElement) { ...@@ -201,7 +201,8 @@ export class QrCodeScanner extends ScopedElementsMixin(DBPLitElement) {
console.assert(this._lock.isLocked()); console.assert(this._lock.isLocked());
await this.updateComplete; await this.updateComplete;
let canvasElement = this._("#canvas"); let targetCanvas = this._("#canvas");
let canvasElement = document.createElement("canvas");
let firstDrawDone = false; let firstDrawDone = false;
this._askPermission = true; this._askPermission = true;
...@@ -218,14 +219,11 @@ export class QrCodeScanner extends ScopedElementsMixin(DBPLitElement) { ...@@ -218,14 +219,11 @@ export class QrCodeScanner extends ScopedElementsMixin(DBPLitElement) {
if (video.readyState === video.HAVE_ENOUGH_DATA) { if (video.readyState === video.HAVE_ENOUGH_DATA) {
this._loading = false; this._loading = false;
// draw into a temporary canvas first
canvasElement.height = video.videoHeight; canvasElement.height = video.videoHeight;
canvasElement.width = video.videoWidth; canvasElement.width = video.videoWidth;
let canvas = canvasElement.getContext("2d"); let canvas = canvasElement.getContext("2d");
canvas.drawImage(video, 0, 0, canvasElement.width, canvasElement.height); canvas.drawImage(video, 0, 0, canvasElement.width, canvasElement.height);
if (!firstDrawDone) {
this.dispatchEvent(new CustomEvent("scan-started", {bubbles: true, composed: true}));
firstDrawDone = true;
}
let maskWidth = canvasElement.width; let maskWidth = canvasElement.width;
let maskHeight = canvasElement.height; let maskHeight = canvasElement.height;
...@@ -286,6 +284,15 @@ export class QrCodeScanner extends ScopedElementsMixin(DBPLitElement) { ...@@ -286,6 +284,15 @@ export class QrCodeScanner extends ScopedElementsMixin(DBPLitElement) {
canvas.rect(maskStartX + maskWidth - borderWidth, maskStartY + maskHeight/3*2, borderWidth, maskHeight/3); canvas.rect(maskStartX + maskWidth - borderWidth, maskStartY + maskHeight/3*2, borderWidth, maskHeight/3);
canvas.fill(); canvas.fill();
targetCanvas.height = canvasElement.height;
targetCanvas.width = canvasElement.width;
targetCanvas.getContext("2d").drawImage(canvasElement, 0, 0);
if (!firstDrawDone) {
this.dispatchEvent(new CustomEvent("scan-started", {bubbles: true, composed: true}));
firstDrawDone = true;
}
if (code) { if (code) {
if (lastSentData !== code.data) { if (lastSentData !== code.data) {
this._outputData = code.data; this._outputData = code.data;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment