2 var canPromise = require('./can-promise')
4 var QRCode = require('./core/qrcode')
5 var CanvasRenderer = require('./renderer/canvas')
6 var SvgRenderer = require('./renderer/svg-tag.js')
8 function renderCanvas (renderFunc, canvas, text, opts, cb) {
9 var args = [].slice.call(arguments, 1)
10 var argsNum = args.length
11 var isLastArgCb = typeof args[argsNum - 1] === 'function'
13 if (!isLastArgCb && !canPromise()) {
14 throw new Error('Callback required as last argument')
19 throw new Error('Too few arguments provided')
25 canvas = opts = undefined
26 } else if (argsNum === 3) {
27 if (canvas.getContext && typeof cb === 'undefined') {
39 throw new Error('Too few arguments provided')
44 canvas = opts = undefined
45 } else if (argsNum === 2 && !canvas.getContext) {
51 return new Promise(function (resolve, reject) {
53 var data = QRCode.create(text, opts)
54 resolve(renderFunc(data, canvas, opts))
62 var data = QRCode.create(text, opts)
63 cb(null, renderFunc(data, canvas, opts))
69 exports.create = QRCode.create
70 exports.toCanvas = renderCanvas.bind(null, CanvasRenderer.render)
71 exports.toDataURL = renderCanvas.bind(null, CanvasRenderer.renderToDataURL)
74 exports.toString = renderCanvas.bind(null, function (data, _, opts) {
75 return SvgRenderer.render(data, opts)