Canvas created dynamically after using excanvas.js in IE does not support getcontext resolution (GO)

Source: Internet
Author: User

Canvas created dynamically after using excanvas.js in IE does not support getcontext resolution (GO)

After using Excanvas.js in IE, the dynamically created canvas does not support GetContext's workaround
After introducing Excanvas.js, in IE, the canvas in the document can be used, but if it is created by the CreateElement method is not
var canvas=document.createelement ("Canvas");
if (Canvas.getcontext) {
Alert ("Support GetContext ()");
}
This code does not work under IE, so the Google made this let IE support canvas code probably read it again, know what is going on. Put the code in the Aptana, 10 minutes less, 785 lines of code, but also good, I think, thanks to the previous carefully read the Rhino book in front of the JS Core section 7 times and the canvas and VML understanding it.

Originally, when loading and executing excanvas.js This script code, G_vmlcanvasmanager_.init (); This statement iterates through all the canvas elements in the page and initializes the elements. That is, the standard canvas of those methods assigned to the canvas under IE:
var els = doc.getelementsbytagname ("canvas");
for (var i = 0; i < els.length; i++) {
if (!els[i].getcontext) {
This.initelement (els[i]);
}
}
If this is the original canvas label for the document, then there is no problem, and this script passes the found canvas element as a parameter to the G_vmlcanvasmanager_.initelement () method. However, the canvas created by the CreateElement method is not available because the new canvas element was not found by the time the script was called.
Then the workaround is to invoke the G_vmlcanvasmanager_.initelement () method to initialize the newly created canvas element.

However, G_vmlcanvasmanager_ is defined in an anonymous function () {}, and I have no way to refer to it in the global scope, fortunately, after the Excanvas.js file, there is a sentence,
G_vmlcanvasmanager = G_vmlcanvasmanager_;
Because of the JS function, if a variable is not defined by Var, then the variable is treated as a global variable, so now there is a global variable to reference it.
So, here's the code to fix the problem:
$ (function () {
var canvas=document.createelement ("Canvas");
Document.body.appendChild (canvas);
if ($.browser.msie) {
Canvas=window. G_vmlcanvasmanager.initelement (canvas);
}
if (canvas.getcontext) {alert ("Support");}
});
I'm using jquery here, and I'm going to execute this script after Dom ready.
In addition, remember Canvas=window. G_vmlcanvasmanager.initelement (canvas), before appending the newly created canvas to the document flow: Document.body.appendChild (canvas);

Canvas created dynamically after using excanvas.js in IE does not support getcontext resolution (GO)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.