Skip to content

Commit

Permalink
Adopt a pull request #3 from qthru/master
Browse files Browse the repository at this point in the history
  • Loading branch information
David, Shim committed Jun 3, 2013
1 parent 41fc496 commit 73424ea
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 72 deletions.
139 changes: 68 additions & 71 deletions qrcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,9 @@ var QRCode;

// Drawing in DOM by using Table tag
var Drawing = !_isSupportCanvas() ? (function () {
var _el = null;
var _htOption = null;

var Drawing = function (el, htOption) {
_el = el;
_htOption = htOption;
this._el = el;
this._htOption = htOption;
};

/**
Expand All @@ -132,6 +129,8 @@ var QRCode;
* @param {QRCode} oQRCode
*/
Drawing.prototype.draw = function (oQRCode) {
var _htOption = this._htOption;
var _el = this._el;
var nCount = oQRCode.getModuleCount();
var nWidth = Math.floor(_htOption.width / nCount);
var nHeight = Math.floor(_htOption.height / nCount);
Expand Down Expand Up @@ -164,31 +163,20 @@ var QRCode;
* Clear the QRCode
*/
Drawing.prototype.clear = function () {
_el.innerHTML = '';
this._el.innerHTML = '';
};

return Drawing;
})() : (function () { // Drawing in Canvas
var _el = null;
var _elCanvas = null;
var _elImage = null;
var _fFail = null;
var _fSuccess = null;
var _htOption = null;
var _bSupportDataURI = null;
var _oContext = null;
var _bIsPainted = false;
var _android = _getAndroid();

function _onMakeImage() {
_elImage.src = _elCanvas.toDataURL("image/png");
_elImage.style.display = "block";
_elCanvas.style.display = "none";
this._elImage.src = this._elCanvas.toDataURL("image/png");
this._elImage.style.display = "block";
this._elCanvas.style.display = "none";
}

// Android 2.1 bug workaround
// http://code.google.com/p/android/issues/detail?id=5141
if (_android && _android <= 2.1) {
if (this._android && this._android <= 2.1) {
var factor = 1 / window.devicePixelRatio;
var drawImage = CanvasRenderingContext2D.prototype.drawImage;
CanvasRenderingContext2D.prototype.drawImage = function (image, sx, sy, sw, sh, dx, dy, dw, dh) {
Expand All @@ -215,37 +203,38 @@ var QRCode;
* @param {Function} fFail Occurs if it doesn't support Data URI
*/
function _safeSetDataURI(fSuccess, fFail) {
_fFail = fFail;
_fSuccess = fSuccess;

// Check it just once
if (_bSupportDataURI === null) {
var el = document.createElement("img");
var fOnError = function () {
_bSupportDataURI = false;

if (_fFail) {
_fFail();
}
};
var fOnSuccess = function () {
_bSupportDataURI = true;

if (_fSuccess) {
_fSuccess();
}
};

el.onabort = fOnError;
el.onerror = fOnError;
el.onload = fOnSuccess;
el.src = "data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="; // the Image contains 1px data.
return;
} else if (_bSupportDataURI === true && _fSuccess) {
_fSuccess();
} else if (_bSupportDataURI === false && _fFail) {
_fFail();
}
var self = this;
self._fFail = fFail;
self._fSuccess = fSuccess;

// Check it just once
if (self._bSupportDataURI === null) {
var el = document.createElement("img");
var fOnError = function() {
self._bSupportDataURI = false;

if (self._fFail) {
_fFail.call(self);
}
};
var fOnSuccess = function() {
self._bSupportDataURI = true;

if (self._fSuccess) {
self._fSuccess.call(self);
}
};

el.onabort = fOnError;
el.onerror = fOnError;
el.onload = fOnSuccess;
el.src = "data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="; // the Image contains 1px data.
return;
} else if (self._bSupportDataURI === true && self._fSuccess) {
self._fSuccess.call(self);
} else if (self._bSupportDataURI === false && self._fFail) {
self._fFail.call(self);
}
};

/**
Expand All @@ -256,18 +245,21 @@ var QRCode;
* @param {Object} htOption QRCode Options
*/
var Drawing = function (el, htOption) {
_htOption = htOption;
_elCanvas = document.createElement("canvas");
_elCanvas.width = htOption.width;
_elCanvas.height = htOption.height;
el.appendChild(_elCanvas);
_el = el;
_oContext = _elCanvas.getContext("2d");
_bIsPainted = false;
_elImage = document.createElement("img");
_elImage.style.display = "none";
_el.appendChild(_elImage);
_bSupportDataURI = null;
this._bIsPainted = false;
this._android = _getAndroid();

this._htOption = htOption;
this._elCanvas = document.createElement("canvas");
this._elCanvas.width = htOption.width;
this._elCanvas.height = htOption.height;
el.appendChild(this._elCanvas);
this._el = el;
this._oContext = this._elCanvas.getContext("2d");
this._bIsPainted = false;
this._elImage = document.createElement("img");
this._elImage.style.display = "none";
this._el.appendChild(this._elImage);
this._bSupportDataURI = null;
};

/**
Expand All @@ -276,12 +268,17 @@ var QRCode;
* @param {QRCode} oQRCode
*/
Drawing.prototype.draw = function (oQRCode) {
_elImage.style.display = "none";
var _elImage = this._elImage;
var _oContext = this._oContext;
var _htOption = this._htOption;

var nCount = oQRCode.getModuleCount();
var nWidth = _htOption.width / nCount;
var nHeight = _htOption.height / nCount;
var nRoundedWidth = Math.round(nWidth);
var nRoundedHeight = Math.round(nHeight);

_elImage.style.display = "none";
this.clear();

for (var row = 0; row < nCount; row++) {
Expand Down Expand Up @@ -311,15 +308,15 @@ var QRCode;
}
}

_bIsPainted = true;
this._bIsPainted = true;
};

/**
* Make the image from Canvas if the browser supports Data URI.
*/
Drawing.prototype.makeImage = function () {
if (_bIsPainted) {
_safeSetDataURI(_onMakeImage);
if (this._bIsPainted) {
_safeSetDataURI.call(this, _onMakeImage);
}
};

Expand All @@ -329,15 +326,15 @@ var QRCode;
* @return {Boolean}
*/
Drawing.prototype.isPainted = function () {
return _bIsPainted;
return this._bIsPainted;
};

/**
* Clear the QRCode
*/
Drawing.prototype.clear = function () {
_oContext.clearRect(0, 0, _elCanvas.width, _elCanvas.height);
_bIsPainted = false;
this._oContext.clearRect(0, 0, this._elCanvas.width, this._elCanvas.height);
this._bIsPainted = false;
};

/**
Expand Down
Loading

0 comments on commit 73424ea

Please sign in to comment.