diff --git a/.gitignore b/.gitignore index 4cbedb2..97b30e7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,23 +1,4 @@ -# Build files and folders to ignore build -release - -# Local -local.makefile -build.local.xml +local *.sublime-* -.ant-targets* - -# Numerous always-ignore extensions -*.ant-targets-build.xml -*.diff -*.err -*.orig -*.log -*.rej -*.swo -*.swp -*.vi -*~ -*.sass-cache diff --git a/LICENSE.txt b/LICENSE.txt index bab9d95..35363c3 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,4 +1,4 @@ -Copyright (c) 2012 Lars Jung, http://larsjung.de +Copyright (c) 2013 Lars Jung, http://larsjung.de Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/README.md b/README.md index 24c2038..bbd1f84 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,11 @@ Uses [QR Code Generator](http://www.d-project.com/qrcode/index.html) (MIT). Kudo ## Changelog +### v0.3 - *2012-06-01* + +* adds option to draw on existing canvas + + ### v0.2 - *2012-07-02* * now encodes up to ~2900 characters (8-bit) diff --git a/build.properties b/build.properties deleted file mode 100644 index a2a746a..0000000 --- a/build.properties +++ /dev/null @@ -1,16 +0,0 @@ - -# project -project.name = jquery.qrcode -project.version = 0.2 - -# src -src.dir = src - -# build -build.dir = build -release.dir = release - -# tools -tool.wepp = wepp -tool.docco = docco -tool.jshint = jshint diff --git a/build.xml b/build.xml deleted file mode 100644 index 92f324b..0000000 --- a/build.xml +++ /dev/null @@ -1,115 +0,0 @@ - - - - - - - - Build: ${build.label} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/jshint.json b/jshint.json deleted file mode 100644 index 8adb47f..0000000 --- a/jshint.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - // Enforcing Options - "bitwise": true, - "curly": true, - "eqeqeq": true, - "forin": true, - "latedef": true, - "newcap": true, - "noempty": true, - "plusplus": true, - "trailing": true, - "undef": true, - - // Environments - "browser": true, - - // Globals - "predef": [ - "jQuery", "qrcode" - ] -} \ No newline at end of file diff --git a/makefile.js b/makefile.js new file mode 100644 index 0000000..00d857e --- /dev/null +++ b/makefile.js @@ -0,0 +1,143 @@ +/*jshint node: true */ +'use strict'; + + +var path = require('path'), + child_process = require('child_process'); + + +var pkg = require('./package.json'), + + root = path.resolve(__dirname), + src = path.resolve(root, 'src'), + build = path.resolve(root, 'build'), + + jshint = { + // Enforcing Options + bitwise: true, + curly: true, + eqeqeq: true, + forin: true, + latedef: true, + newcap: true, + noempty: true, + plusplus: true, + trailing: true, + undef: true, + + // Environments + browser: true, + + // Globals + predef: [ + "jQuery", "qrcode" + ] + }, + + mapperRoot = function (blob) { + + return blob.source.replace(root, build); + }; + + +module.exports = function (make) { + + var Event = make.Event, + $ = make.fQuery, + moment = make.moment, + stamp, replacements; + + + make.version('>=0.8.1'); + make.defaults('release'); + + + make.before(function () { + + stamp = moment(); + + replacements = { + pkg: pkg, + stamp: stamp.format('YYYY-MM-DD HH:mm:ss') + }; + + Event.info({ method: 'before', message: pkg.version + ' ' + replacements.stamp }); + }); + + + make.target('check-version', [], 'add git info to dev builds').async(function (done, fail) { + + if (!/-dev$/.test(pkg.version)) { + done(); + return; + } + + $.git(root, function (err, result) { + + pkg.version += '-' + result.revListOriginMasterHead.length + '-' + result.revParseHead.slice(0, 7); + Event.info({ + method: 'check-version', + message: 'version set to ' + pkg.version + }); + done(); + }); + }); + + + make.target('clean', [], 'delete build folder') + .sync(function () { + + $.rmfr($.I_AM_SURE, build); + }); + + + make.target('lint', [], 'lint all JavaScript files with JSHint') + .sync(function () { + + $(src + ': jquery.qrcode.js') + .jshint(jshint); + }); + + + make.target('build', ['check-version'], 'build all updated files') + .sync(function () { + + var scriptName = pkg.name; + + $(src + ': ' + scriptName + '.js') + .includify() + .handlebars(replacements) + .write($.OVERWRITE, path.join(build, scriptName + '-' + pkg.version + '.js')) + .uglifyjs() + .write($.OVERWRITE, path.join(build, scriptName + '-' + pkg.version + '.min.js')); + + $(root + ': README*, LICENSE*') + .write($.OVERWRITE, mapperRoot); + }); + + + make.target('release', ['clean', 'build'], 'create a zipball') + .async(function (done, fail) { + + var target = path.join(build, pkg.name + '-' + pkg.version + '.zip'), + cmd = 'zip', + args = ['-ro', target, '.'], + options = { cwd: build }, + proc = child_process.spawn(cmd, args, options); + + Event.info({ method: 'exec', message: cmd + ' ' + args.join(' ') }); + + proc.stderr.on('data', function (data) { + process.stderr.write(data); + }); + proc.on('exit', function (code) { + if (code) { + Event.error({ method: 'exec', message: cmd + ' exit code ' + code }); + fail(); + } else { + Event.ok({ method: 'exec', message: 'created zipball ' + target }); + done(); + } + }); + }); +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..0e7d76b --- /dev/null +++ b/package.json @@ -0,0 +1,5 @@ +{ + "name": "jquery.qrcode", + "displayName": "jQuery.qrcode", + "version": "0.3" +} \ No newline at end of file diff --git a/src/jquery.qrcode.js b/src/jquery.qrcode.js index 764d820..3440aa9 100644 --- a/src/jquery.qrcode.js +++ b/src/jquery.qrcode.js @@ -1,4 +1,4 @@ -/*! jQuery.qrcode %BUILD_VERSION% - //larsjung.de/qrcode - MIT License */ +/*! {{pkg.displayName}} {{pkg.version}} - //larsjung.de/qrcode - MIT License */ // Uses [QR Code Generator](http://www.d-project.com/qrcode/index.html) (MIT), appended to the end of this file. // Kudos to [jquery.qrcode.js](http://github.com/jeromeetienne/jquery-qrcode) (MIT). @@ -6,10 +6,17 @@ (function ($) { 'use strict'; + // Check if canvas is available in the browser (as Modernizr does) + var canvasAvailable = (function () { + + var elem = document.createElement('canvas'); + return !!(elem.getContext && elem.getContext('2d')); + }()), + // Wrapper for the original QR code generator. - var createQr = function (typeNumber, correctLevel, text) { + createQr = function (typeNumber, correctLevel, text) { - // qrcode is the single public function that will be defined by the `QR Code Generator` + // `qrcode` is the single public function that will be defined by the `QR Code Generator` // at the end of the file. var qr = qrcode(typeNumber, correctLevel); qr.addData(text); @@ -31,63 +38,88 @@ return null; }, - // Returns a `canvas` element representing the QR code for the given settings. - createCanvas = function (settings) { + // Draws QR code to the given `canvas` and returns it. + drawOnCanvas = function (canvas, settings) { + + // some shortcuts to improve compression + var settings_text = settings.text, + settings_left = settings.left, + settings_top = settings.top, + settings_width = settings.width, + settings_height = settings.height, + settings_color = settings.color, + settings_bgColor = settings.bgColor, - var qr = createBestQr(settings.text), - $canvas = $('').attr('width', settings.width).attr('height', settings.height), + qr = createBestQr(settings_text), + $canvas = $(canvas), ctx = $canvas[0].getContext('2d'); - if (settings.bgColor) { - ctx.fillStyle = settings.bgColor; - ctx.fillRect(0, 0, settings.width, settings.height); + if (settings_bgColor) { + ctx.fillStyle = settings_bgColor; + ctx.fillRect(settings_left, settings_top, settings_width, settings_height); } if (qr) { var moduleCount = qr.getModuleCount(), - moduleWidth = settings.width / moduleCount, - moduleHeight = settings.height / moduleCount, + moduleWidth = settings_width / moduleCount, + moduleHeight = settings_height / moduleCount, row, col; ctx.beginPath(); for (row = 0; row < moduleCount; row += 1) { for (col = 0; col < moduleCount; col += 1) { if (qr.isDark(row, col)) { - ctx.rect(col * moduleWidth, row * moduleHeight, moduleWidth, moduleHeight); + ctx.rect(settings_left + col * moduleWidth, settings_top + row * moduleHeight, moduleWidth, moduleHeight); } } } - ctx.fillStyle = settings.color; + ctx.fillStyle = settings_color; ctx.fill(); } return $canvas; }, + // Returns a `canvas` element representing the QR code for the given settings. + createCanvas = function (settings) { + + var $canvas = $('').attr('width', settings.width).attr('height', settings.height); + + return drawOnCanvas($canvas, settings); + }, + // Returns a `div` element representing the QR code for the given settings. createDiv = function (settings) { - var qr = createBestQr(settings.text), + // some shortcuts to improve compression + var settings_text = settings.text, + settings_width = settings.width, + settings_height = settings.height, + settings_color = settings.color, + settings_bgColor = settings.bgColor, + math_floor = Math.floor, + + qr = createBestQr(settings_text), $div = $('
').css({ position: 'relative', left: 0, top: 0, padding: 0, margin: 0, - width: settings.width, - height: settings.height + width: settings_width, + height: settings_height }); - if (settings.bgColor) { - $div.css('background-color', settings.bgColor); + if (settings_bgColor) { + $div.css('background-color', settings_bgColor); } if (qr) { var moduleCount = qr.getModuleCount(), - moduleWidth = Math.floor(settings.width / moduleCount), - moduleHeight = Math.floor(settings.height / moduleCount), - offsetLeft = Math.floor(0.5 * (settings.width - moduleWidth * moduleCount)), - offsetTop = Math.floor(0.5 * (settings.height - moduleHeight * moduleCount)), + moduleWidth = math_floor(settings_width / moduleCount), + moduleHeight = math_floor(settings_height / moduleCount), + offsetLeft = math_floor(0.5 * (settings_width - moduleWidth * moduleCount)), + offsetTop = math_floor(0.5 * (settings_height - moduleHeight * moduleCount)), row, col; for (row = 0; row < moduleCount; row += 1) { @@ -110,13 +142,20 @@ margin: 0, width: moduleWidth, height: moduleHeight, - 'background-color': settings.color + 'background-color': settings_color }); } return $div; }, + createHTML = function (options) { + + var settings = $.extend({}, defaults, options); + + return canvasAvailable && settings.render === 'canvas' ? createCanvas(settings) : createDiv(settings); + }, + // Plugin // ====== @@ -127,6 +166,10 @@ // render method: `'canvas'` or `'div'` render: 'canvas', + // left and top in pixel if drawn onto existing canvas + left: 0, + top: 0, + // width and height in pixel width: 256, height: 256, @@ -145,11 +188,13 @@ // ------------------- $.fn.qrcode = function(options) { - var settings = $.extend({}, defaults, options); - return this.each(function () { - $(this).append(settings.render === 'canvas' ? createCanvas(settings) : createDiv(settings)); + if (this.nodeName.toLowerCase() === 'canvas') { + drawOnCanvas(this, options); + } else { + $(this).append(createHTML(options)); + } }); };