Skip to content

Commit

Permalink
Merge pull request #5 from ysangkok/master
Browse files Browse the repository at this point in the history
Add SVG support
  • Loading branch information
davidshimjs committed Jul 12, 2013
2 parents c3db592 + 1b87ce8 commit 2b64de0
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
37 changes: 37 additions & 0 deletions index.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 50 additions & 2 deletions qrcode.js
Expand Up @@ -116,8 +116,56 @@ var QRCode;
return android;
}

var svgDrawer = (function() {

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

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);

this.clear();

function makeSVG(tag, attrs) {
var el = document.createElementNS('http://www.w3.org/2000/svg', tag);
for (var k in attrs)
if (attrs.hasOwnProperty(k)) el.setAttribute(k, attrs[k]);
return el;
}

var svg = makeSVG("svg" , {'viewBox': '0 0 ' + String(nCount) + " " + String(nCount), 'width': '100%', 'height': '100%', 'fill': _htOption.colorLight});
svg.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xlink", "http://www.w3.org/1999/xlink");
_el.appendChild(svg);

svg.appendChild(makeSVG("rect", {"fill": _htOption.colorDark, "width": "1", "height": "1", "id": "template"}));

for (var row = 0; row < nCount; row++) {
for (var col = 0; col < nCount; col++) {
if (oQRCode.isDark(row, col)) {
var child = makeSVG("use", {"x": String(row), "y": String(col)});
child.setAttributeNS("http://www.w3.org/1999/xlink", "href", "#template")
svg.appendChild(child);
}
}
}
};
Drawing.prototype.clear = function () {
while (this._el.hasChildNodes())
this._el.removeChild(this._el.lastChild);
};
return Drawing;
})();

var useSVG = document.documentElement.tagName.toLowerCase() === "svg";

// Drawing in DOM by using Table tag
var Drawing = !_isSupportCanvas() ? (function () {
var Drawing = useSVG ? svgDrawer : !_isSupportCanvas() ? (function () {
var Drawing = function (el, htOption) {
this._el = el;
this._htOption = htOption;
Expand Down Expand Up @@ -495,4 +543,4 @@ var QRCode;
* @name QRCode.CorrectLevel
*/
QRCode.CorrectLevel = QRErrorCorrectLevel;
})();
})();

0 comments on commit 2b64de0

Please sign in to comment.