Moved common code from dom.js and canvas.js to elements.js