Answer the question
In order to leave comments, you need to log in
Why is there an error when calling drawImage?
Why is there an error when calling drawImage?
<body></body>
<script>
const canvas = document.createElement('canvas')
canvas.width = 400
canvas.height = 300
document.body.appendChild(canvas)
var ctx = canvas.getContext('2d')
const setBg = () => ctx.drawImage("https://mdn.mozillademos.org/files/225/Canvas_drawimage.jpg", 0, 0)
const background = document.createElement('img')
background.setAttribute('crossorigin', 'anonymous')
background.onload = setBg
const downloader = document.createElement('a')
downloader.style.display = 'none'
document.body.appendChild(downloader)
downloader.download = 'myimage.png'
setTimeout(() => {
ctx.drawImage("https://mdn.mozillademos.org/files/225/Canvas_drawimage.jpg", 0, 0)
save();
}, 2000)
function save() {
downloader.href = canvas.toDataURL()
downloader.click()
}
</script>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question