Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added callback function support on QR image onload event #231

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,17 @@ var qrcode = new QRCode(document.getElementById("qrcode"), {
height: 128,
colorDark : "#000000",
colorLight : "#ffffff",
correctLevel : QRCode.CorrectLevel.H
correctLevel : QRCode.CorrectLevel.H,
/**
* Fired when QR image element is loaded
* @param {HTMLElement} QR image DOM element
* @param {string} text to generate QR-code image
* @return {void}
*/
callback: function (image, text) {
console.log('text QR code was created from', text);
console.log('loaded image DOM element', image);
}
Comment on lines +25 to +34

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn´t work for me!

Copy link
Author

@IvankoRambo IvankoRambo Jul 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @apfjuliano .
What was the exact problem?)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The callback function, It's not been fired on my tests when the QRCode is done

});
</script>
```
Expand Down
6 changes: 5 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
<script type="text/javascript">
var qrcode = new QRCode(document.getElementById("qrcode"), {
width : 100,
height : 100
height : 100,
callback: function(image, text) {
console.log('Input text', text);
console.log('Loaded image element', image);
}
});

function makeCode () {
Expand Down
14 changes: 9 additions & 5 deletions qrcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,10 @@ var QRCode;
function _onMakeImage() {
this._elImage.src = this._elCanvas.toDataURL("image/png");
this._elImage.style.display = "block";
this._elCanvas.style.display = "none";
this._elCanvas.style.display = "none";
if (this._htOption.callback) {
this._elImage.addEventListener('load', this._htOption.callback.bind(null, this._elImage, this._htOption.text));
}
}

// Android 2.1 bug workaround
Expand Down Expand Up @@ -569,7 +572,7 @@ var QRCode;
this._oDrawing = new Drawing(this._el, this._htOption);

if (this._htOption.text) {
this.makeCode(this._htOption.text);
this.makeCode(this._htOption.text);
}
};

Expand All @@ -579,11 +582,12 @@ var QRCode;
* @param {String} sText link data
*/
QRCode.prototype.makeCode = function (sText) {
this._oQRCode = new QRCodeModel(_getTypeNumber(sText, this._htOption.correctLevel), this._htOption.correctLevel);
this._oQRCode.addData(sText);
this._htOption.text = sText;
this._oQRCode = new QRCodeModel(_getTypeNumber(this._htOption.text, this._htOption.correctLevel), this._htOption.correctLevel);
this._oQRCode.addData(this._htOption.text);
this._oQRCode.make();
this._el.title = sText;
this._oDrawing.draw(this._oQRCode);
this._oDrawing.draw(this._oQRCode);
this.makeImage();
};

Expand Down
Loading