+ 5
[SOLVED]Canvas problem
I saw this code online ad desired to make some modifications but couldn't cause getcontext is not working. https://code.sololearn.com/WDOZEmzY8501/?ref=app
6 Respuestas
+ 2
Because canvas === null
you forget to add canvas element with id "canvasRegn"
to avoid error
You can use Optional chaining (?.)
"Optional chaining (?.) - JavaScript | MDN" https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining
canvas?.getContext
+ 2
Check the code again
Please bro can you help me correct it directly from the code
edit : corrected thanks
+ 1
Please I don't really get it
Do you mean
canvas?.getContext...
But it won't work
0
ℂ𝕙𝕚𝕘𝕠𝕫𝕚𝕖 𝔸𝕟𝕪𝕒𝕖𝕛𝕚🇳🇬 i mean to say
1. You haven't created canvas elements so ,
document.getElementById("canvasRegn")
Will return null
2. Null don't have any property called getContext so it will throw an error
To avoid this error using can use
Optional chaining (?.)
how it works ?
you can view MDN link
0
As Mirielle writes, it works changing the image
https://art.pixilart.com/2b2f3aabe30daa5.png
is giving a directory path rather than an image valid?
another issue is that dimensions are hard-coded for a small canvas