I was messing around with HTML5 / Javascript and found what I don't understand.
I tried to write some basic procedures for working with an HTML5 canvas and found that the drawImage function does not draw anything. Assuming this is my code, I used the code from an existing tutorial, namely the HTML5 Canvas Image Tutorial . When I included the Javascript code in it from the outside (i.e., as a separate file), I still did not see my image facing the canvas.
From there I copied the full source, found it here , literally, and it really worked. I started messing around to see why it worked, but mine didn’t.
I found that the difference was that the verbatim code determined the width and height of the line in the line. I prefer to separate the style of HTML and CSS. The code worked if I did:
<canvas id = "myCanvas" width = "600" height = "400"></canvas>
but it didn’t work if I determined the width and height of the canvas in the external CSS stylesheet.
I really decided that one of my problems was that I included a stylesheet after the Javascript file; Before doing this, my image did not appear at all, even after confirming with the alert () function that the code inside the image loading handler was lit. After changing the order in which the files were included, my image appeared, but it was strangely scaled, as if the CSS style was ignored.
Why is this? Am I missing something regarding the order in which canvas properties are defined using CSS? Why does Javascript ignore CSS style in this case? If at all relevant, I have tried this in the latest versions of Chrome and Firefox.
source share