Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple Canvas Rectangle Interaction with Javascript
    primarykey
    data
    text
    <p>I am trying to teach myself the basics of the Canvas tag and Javascript interaction. In the following code, I can make the rectangle expand when I hover with "onmouseover", but it will not contract when "onmouseout".</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;script&gt; window.requestAnimationFrame = (function(){ return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function( callback ){ window.setTimeout(callback, 1000 / 60); }; })(); var rectWidth = 100; function clear() { var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); ctx.clearRect(0, 0, ctx.width, ctx.height); } function widenRect(){ var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); clear(); ctx.fillStyle="#92B901"; ctx.fillRect(0,0,rectWidth,100); if(rectWidth &lt; 200){ rectWidth+=10; } requestAnimationFrame(widenRect); } function narrowRect(){ var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); clear(); ctx.fillStyle="#92B901"; ctx.fillRect(0,0,rectWidth,100); if(rectWidth &gt; 100){ rectWidth-=10; } requestAnimationFrame(narrowRect); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;canvas id="myCanvas" width="200" height="100" onmouseover="widenRect()" onmouseout="narrowRect()"&gt; Your browser does not support the HTML5 canvas tag. &lt;script&gt; var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); ctx.fillStyle="#92B901"; ctx.fillRect(0,0,rectWidth,100); &lt;/script&gt; &lt;/canvas&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Any help will be appreciated!</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.
    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