var size = require('element-size') module.exports = fit var scratch = new Float32Array(2) function fit(canvas, parent, scale) { var isSVG = canvas.nodeName.toUpperCase() === 'SVG' canvas.style.position = canvas.style.position || 'absolute' canvas.style.top = 0 canvas.style.left = 0 resize.scale = parseFloat(scale || 1) resize.parent = parent return resize() function resize() { var p = resize.parent || canvas.parentNode if (typeof p === 'function') { var dims = p(scratch) || scratch var width = dims[0] var height = dims[1] } else if (p && p !== document.body) { var psize = size(p) var width = psize[0]|0 var height = psize[1]|0 } else { var width = window.innerWidth var height = window.innerHeight } if (isSVG) { canvas.setAttribute('width', width * resize.scale + 'px') canvas.setAttribute('height', height * resize.scale + 'px') } else { canvas.width = width * resize.scale canvas.height = height * resize.scale } canvas.style.width = width + 'px' canvas.style.height = height + 'px' return resize } }