Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set focus to the HTML5 canvas element?

I'm using the HTML5 <canvas> element in Firefox 2.0.0.16 and in Safari 3.1.2, both on my iMac. (I've tried this in Firefox 3.0 on Windows as well, also to no avail.) The tag looks like this:

<td>
   <canvas id="display"
           width="500px"
           height="500px">
   </canvas>
</td>

I have a button to "activate" some functionality that interacts with the canvas. That button's onclick() event calls a function. In that function I have the following line:

document.getElementById("display").focus();

This does not work. Firebug reports no error. But the focus still remains where it was. I can click on the canvas or tab towards the canvas and focus will be lost from the other elements, but apparently never be gained on by the canvas (The canvas's onfocus() event never fires).

I find this odd. Is it that the canvas simply cannot get focus, or am I missing something here? Any insight would be appreciated.

Thank you.

like image 260
Alan Avatar asked Sep 11 '08 14:09

Alan


People also ask

How do you set focus on canvas?

Now to do this task open the canvas app editor and select the screen having the create student form. Choose the screen and open OnVisible action property and mention the PowerFX “SetFocus(DataCardValue19)“.

How do you set focus on a specific element in HTML?

To set focus to an HTML form element, the focus() method of JavaScript can be used. To do so, call this method on an object of the element that is to be focused, as shown in the example. Example 1: The focus() method is set to the input tag when user clicks on Focus button.

What is the canvas element in HTML 5?

The HTML <canvas> element is used to draw graphics, on the fly, via JavaScript. The <canvas> element is only a container for graphics. You must use JavaScript to actually draw the graphics. Canvas has several methods for drawing paths, boxes, circles, text, and adding images.

What is focus in HTML?

The HTMLElement. focus() method sets focus on the specified element, if it can be focused. The focused element is the element that will receive keyboard and similar events by default.


1 Answers

Give the canvas a tab index:

   <canvas id="display"
           width="500px"
           height="500px"
           tabindex="1">
   </canvas>
like image 96
Shog9 Avatar answered Nov 05 '22 15:11

Shog9