1 var Utils = require('./utils')
3 function clearCanvas (ctx, canvas, size) {
4 ctx.clearRect(0, 0, canvas.width, canvas.height)
6 if (!canvas.style) canvas.style = {}
9 canvas.style.height = size + 'px'
10 canvas.style.width = size + 'px'
13 function getCanvasElement () {
15 return document.createElement('canvas')
17 throw new Error('You need to specify a canvas element')
21 exports.render = function render (qrData, canvas, options) {
25 if (typeof opts === 'undefined' && (!canvas || !canvas.getContext)) {
31 canvasEl = getCanvasElement()
34 opts = Utils.getOptions(opts)
35 var size = Utils.getImageWidth(qrData.modules.size, opts)
37 var ctx = canvasEl.getContext('2d')
38 var image = ctx.createImageData(size, size)
39 Utils.qrToImageData(image.data, qrData, opts)
41 clearCanvas(ctx, canvasEl, size)
42 ctx.putImageData(image, 0, 0)
47 exports.renderToDataURL = function renderToDataURL (qrData, canvas, options) {
50 if (typeof opts === 'undefined' && (!canvas || !canvas.getContext)) {
57 var canvasEl = exports.render(qrData, canvas, opts)
59 var type = opts.type || 'image/png'
60 var rendererOpts = opts.rendererOpts || {}
62 return canvasEl.toDataURL(type, rendererOpts.quality)