Note that there are some explanatory texts on larger screens.

plurals
  1. POSeparate Javascript code from HTML Canvas
    primarykey
    data
    text
    <p>I am trying to learn HTML5 Canvas, which drawn from javascript codes.</p> <p>It seems that I could not separate Javascript code from the original html. had been looking some solutions:</p> <ul> <li><a href="https://stackoverflow.com/questions/11349613/html5-canvas-not-working-in-external-javascript-file">HTML5 Canvas not working in external JavaScript file</a></li> <li><a href="https://stackoverflow.com/questions/11032138/canvas-html-tag">canvas html tag</a></li> </ul> <p>These has not been a solution for me, as after I implement both solutions what appears to me is blank canvas.</p> <p>Here is my code.</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Christoper Hans' Paint HTML5 Project&lt;/title&gt; &lt;link rel="stylesheet" type="text/css" href="paint.css"&gt; &lt;script type="text/javascript"&gt;&lt;!-- window.addEventListener('load', function () { var ctx = document.getElementById("paint").getContext("2d"); ctx.canvas.width = window.innerWidth; ctx.canvas.height = window.innerHeight; function drawRect(ctx, x1, y1, x2, y2, fill) { ctx.fillStyle=fill; ctx.fillRect(x1, y1, x2, y2); } drawRect(ctx, 100, 100, 200, 250, "FF0000"); }, false); // --&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;canvas id="paint" width="500" height="500" style="border:1px solid #c3c3c3;"&gt; Your browser does not support the HTML5 canvas tag. &lt;/canvas&gt; &lt;/body&gt; </code></pre> <p></p> <p>I don't know why but only this kind of code <em>which I got from a post from this website</em> works and I could not separate the javascript to external file.</p> <p>TL;DR : Wanted to separate javascript from html to work with canvas.</p> <p>Any help guys?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload