Note that there are some explanatory texts on larger screens.

plurals
  1. POmouse event is not defined
    primarykey
    data
    text
    <p>I have an HTML5 canvas on a web page, with a JavaScript function that draws an image on the canvas. I'm trying to add a mouse event to the image, so that when it is clicked, another JavaScript function that is called, which will update what is displayed on the canvas.</p> <p>When viewing the page in Firefox, nothing happens when I click the image that's displayed on the canvas. I'm using Firebug to try and see what's wrong, and it's given me the following message:</p> <pre><code> mouse_event is not defined drawStartButton()index.html (line 107) startGame()index.html (line 64) (?)()index.html (line 1) event = load [Break On This Error] ...useX = (mouse_event.clientX-boundingBox.left) * (myGameCanvas.width/boundingBox.... index.html (line 107) </code></pre> <p>The function I've used to draw the start button on the canvas, and mouse event I've added are below:</p> <pre><code>function drawStartButton(){ image.onload = function(){ context.drawImage(image, 260.5, 60); }; image.src = "StartButton.png"; /** Now I need to add an event listener to the area of the canvas on on which the button image is displayed, in order to 'listen' for a click on the button */ var boundingBox = myGameCanvas.getBoundingClientRect(); var mouseX = (mouse_event.clientX-boundingBox.left) * (myGameCanvas.width/boundingBox.width); var mouseY = (mouse_event.clientY-boundingBox.top) * (myGameCanvas.height/boundingBox.height); var pixels = context.getImageData(mouseX, mouseY, 1, 1); } </code></pre> <p>Basically, all I want to do, is that when the user clicks the button, the function below will be called, and will update the contents of the canvas:</p> <pre><code>function drawLevelOneElements(){ var context = canvas.getContext("2d"); /* Draw the images for numbers 1-10.*/ var image1 = new Image(); /* Test that this code is being executed */ context.moveTo(300, 300); context.font = "11pt Calibri"; context.strokeStyle = "black"; context.strokeText("Testing",300, 300); /* End of test */ image1.onLoad = function(){ context.drawImage(image1, 50, 50); }; image1.src="1.png"; } </code></pre> <p>Thanks for your help. :)</p> <p>*<strong><em>Update</em>**</strong></p> <p>It seems I was able to solve this by putting the variables mouseX, mouseY and pixels in a function and setting the boundingBox.onmousemove property equal to this function:</p> <pre><code>boundingBox.onmousemove = function(e){ var mouseX = e.pageX - this.offsetLeft; var mouseY = e.pageY - this.offsetTop; var pixels = context.getImageData(mouseX, mouseY, 1, 1); } </code></pre>
    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.
    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