Jump to content
Mackenzie17

Opera Does Not Get Canvas Handle

Recommended Posts

Hi I'm a little new to JavaScript and I am trying to make a small program using an HTML5 canvas. However, when the following code is executed:function Prog(){ var self = this; this.init = function() { var d = document; self.canvas = d.getElementById("can"); self.c = self.canvas.getContext("2d");...}var p = new Prog();p.init();The code works in the other major browsers, but in opera, it throws an error saying "cannot convert self.canvas to object" or sometimes just "self.canvas is null"Thanks in advance

Edited by Mackenzie17

Share this post


Link to post
Share on other sites

have you checked that d.getElementById("can") is referencing an element? It's possible that you're trying to access the element before it has loaded.

Share this post


Link to post
Share on other sites

hi Ingolmethanks for replying, I tried replacingself.c = self.canvas.getContext("2d"); withself.c = d.getElementById("can").getContext("2d"); with that code the error console says"Cannot convert 'd.getElementById("can")' to object"the script is deferred so the page should be loaded before the script is executed thanks

Edited by Mackenzie17

Share this post


Link to post
Share on other sites
It's possible that you're trying to access the element before it has loaded.
how/when are you calling this function in relation to the page loading? Can we see the full code? (JS/HTML) The best practice would be to have it run within the window.onload event handler. Edited by thescientist

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...