This is what my code looks like:
document.addEventListener('DOMContentLoaded', function () { var canvas = document.querySelector('canvas'); var ctx = canvas.getContext("2d"); var imageData = ctx.getImageData(0, 0, 300, 300); var tile = { 'id': 1, 'data': imageData, 'dataUrl': imageData.toDataUrl() }; var div = document.createElement('div'); div.classList.add('tile'); grid.appendChild(div); div.style.backgroundImage = ('url(' + tile.dataUrl + ')'); });
I am trying to extract a portion of an image on a canvas starting with (0,0) with a height and width of 300 pixels and convert this imageData object to dataUrl, which will be used as the background div.
I get an error: imageData.toDataUrl () is not a function. How can I achieve this?
Thanks in advance!
Ramya
source share