[USE JQUERY TO DRAW ON CANVAS] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

[USE JQUERY TO DRAW ON CANVAS]

i wonder why Jquery isnt use to draw on canvas, html <canvas id="c"></canvas> Jquery $(function(){ var canvas = $("#c"); //nothing happen but //using javascript it successfully draw var pen = canvas.getContext("2d"); pen.strokeText("Sololearn", 20, 30); }); when i use jquery it always fails, only with jS it can pass, Can some one explain why?

9th Jul 2018, 1:42 PM
Nura Programmer
Nura Programmer - avatar
12 Answers
+ 1
You have to remember that JQuery return an WRAPPER object that is not SAME that nornal js DOM object. For get it from an JQuery wrapper object, use: wrapperObj[0] or wrapperObj.get(0)
9th Jul 2018, 2:36 PM
KrOW
KrOW - avatar
+ 1
Add [0] or .get(0) after $("#c") . Like... var canvas = $("#c")[0]; It's something to do with how jQuery selects elements -- it only saves a link not the element itself. I don't know exactly, I was just curious so I looked into your question.
9th Jul 2018, 2:03 PM
James
James - avatar
+ 1
I just saw this site. It doesn't really explain too well, but I think it gets quite complex when you start trying to explain the inner workings of a coding language/library. You should try Googling more things about how jQuery works. https://stackoverflow.com/questions/2925130/jquery-equivalent-of-getting-the-context-of-a-canvas
9th Jul 2018, 2:11 PM
James
James - avatar
+ 1
KrOW oh, that technically explains clear why. 👍
9th Jul 2018, 3:32 PM
Nura Programmer
Nura Programmer - avatar
0
What part you trying to use jQuery for exactly?
9th Jul 2018, 1:45 PM
James
James - avatar
0
when i select a canvas and try to get its CONTEXT
9th Jul 2018, 1:47 PM
Nura Programmer
Nura Programmer - avatar
9th Jul 2018, 1:56 PM
Nura Programmer
Nura Programmer - avatar
0
Ofcourse it worked, but the explanation, can u elaborate
9th Jul 2018, 2:07 PM
Nura Programmer
Nura Programmer - avatar
0
bcs i looked in so many codes here on sololearn i didnt see a single person uses jquery to draw on canvas, so i looked forward since i love using jquery
9th Jul 2018, 2:09 PM
Nura Programmer
Nura Programmer - avatar
0
James thanks 4 ur help but i think explaining from some one lively is more efficient.. Any why, once again thnk u i will really looked through ur sorce🙆
9th Jul 2018, 2:16 PM
Nura Programmer
Nura Programmer - avatar
0
🌟th3 c0d3r😎 👍👍👍👍
9th Jul 2018, 3:45 PM
KrOW
KrOW - avatar
0
🌟th3 c0d3r😎 👍👍👍👍
9th Jul 2018, 3:46 PM
KrOW
KrOW - avatar