+ 2

window.onload = function() { stk = document.getElementsByTagName("canvas")[0].getContext("2d"); stk.moveTo(Math.random()

plezz.....explain me this code !!

13th Feb 2017, 5:07 AM
yash
yash - avatar
4 Answers
+ 2
window.onload Is an event that is triggered when the widow has loaded including its content. So this function runs after the page loads. document.getElementsByTagName("canvas")[0] gets all the elements of the tag type <canvas> and puts them into an array and then gets the first element "[0]" of that array. Then getContext("2d"); gets the 2 dimensional context for that canvas which allows for 2d drawing to now take place on that canvas. This is all put into the variable "stk" (which i'm guessing is supposed to be short for stroke). When this is created I believe that the "pen" tip aka "stk" would be located at the 0, 0 coordinates (upper left of canvas). The stk.moveTo(Math.random() <-- Incomplete, this should take both an x and y coordinate. But, I think the Idea here is to draw to a random location on the canvas. Maybe like this instead -> stk.moveTo(Math.random(), Math.random());
13th Feb 2017, 5:35 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Oh yes... that's the start of your sentence and the breakin line wich confuses me, and so, I was too quickly, not you ^^
14th Feb 2017, 4:05 AM
visph
visph - avatar
0
@ChaoticDawg: document.getElementsByTagName("canvas") gets all the elements of the tag type <canvas>, but the '[0]' tagert the first element index ;) ( I think you've just write to quickly, but it seems to me needing to accuratly explain the difference for beginner leaners :P )... And probably these are the three first lines of a much longer function ^^
14th Feb 2017, 4:01 AM
visph
visph - avatar
0
@visph Nope that's in my answer. Read the sentence agian. ;-)
14th Feb 2017, 4:02 AM
ChaoticDawg
ChaoticDawg - avatar