+ 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

23rd Feb 2022, 5:39 PM
Chigozie Anyaeji 🇳🇬
Chigozie Anyaeji 🇳🇬 - avatar
6 Answers
+ 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
23rd Feb 2022, 5:47 PM
Pariket Thakur
Pariket Thakur - avatar
+ 2
Check the code again Please bro can you help me correct it directly from the code edit : corrected thanks
23rd Feb 2022, 6:18 PM
Chigozie Anyaeji 🇳🇬
Chigozie Anyaeji 🇳🇬 - avatar
+ 1
Please I don't really get it Do you mean canvas?.getContext... But it won't work
23rd Feb 2022, 6:09 PM
Chigozie Anyaeji 🇳🇬
Chigozie Anyaeji 🇳🇬 - avatar
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
23rd Feb 2022, 6:14 PM
Pariket Thakur
Pariket Thakur - avatar
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
24th Feb 2022, 6:33 PM
bell
bell - avatar