0

I have the following code that gets input from users on a button click and draws on a canvas but when i click the button nothing works. Here's the code:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link href="mycss.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="js.js"></script>
        <title>CANVAS</title>

    </head>
    <body>
        <canvas id="myCanvas" width="800" height="600" style="border: 1px solid #000000;"></canvas>

        <form>
            <input id="rectangle" type="button" value="Draw a rectagle" class="button2"/>   
        </form>

    </body>
</html>

js.js

function drawRectangle(){
    var x  = prompt("Enter x co-ordinate", "0");
    var y  = prompt("Enter y co-ordinate", "0");
    var color = prompt("Enter color to fill rectangle","color");

    var canvas = document.getElementById("myCanvas");
    var context = canvas.getContext("2d");
    context.fillStyle = color;
    context.fillRect(x,y,100,50);


}
function registerEvents(){
    var rect = document.getElementById( "rectangle" );
   rect .addEventListener( "click", drawRectangle, false); 
}

window.addEventListener(load, registerEvents, false);

I don't know why the code doesn't work. What i'm i getting wrong? Thanks

2
Contributors
2
Replies
12
Views
4 Years
Discussion Span
Last Post by dhani09
1

there are issues with different browser event listener.Take a look.I have for now added script in same HTML you can modify it accordingly.
For IE and non/IE browser there are differnt way to load and call nethods.

window.addEventListener('load', registerEvents, false);

Moreover,you were also missing quotes before and after event in line 18

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link href="mycss.css" rel="stylesheet" type="text/css" />

        <title>CANVAS</title>
<script>
function drawRectangle(){
    var x  = prompt("Enter x co-ordinate", "0");
    var y  = prompt("Enter y co-ordinate", "0");
    var color = prompt("Enter color to fill rectangle","color");
    var canvas = document.getElementById("myCanvas");
    var context = canvas.getContext("2d");
    context.fillStyle = color;
    context.fillRect(x,y,100,50);
}
function registerEvents(){
    var rect = document.getElementById( "rectangle" );

if (window.addEventListener){

  window.addEventListener('click', drawRectangle, false);
  }
else if (window.attachEvent){

  window.attachEvent('onclick', drawRectangle);}



}

if (window.addEventListener){

  window.addEventListener('load', registerEvents, false);
  }
else if (window.attachEvent){

  window.attachEvent('onload', registerEvents);}

//window.addEventListener("onload", registerEvents, false);
</script>
    </head>
    <body>
        <canvas id="myCanvas" width="800" height="600" style="border: 1px solid #000000;"></canvas>

        <form>
            <input id="rectangle" type="button" value="Draw a rectagle" class="button2"/>   
        </form>

    </body>
</html>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.