This article is translated from Steve Fulton & Jeff Fulton HTML5 Canvas, Chapter 1, “Testing to See Whether the Browser Supports Canvas”.
After getting the reference to the Canvas element on the HTML page, we need to test whether the element contains "context". The context of a Canvas refers to the plane defined by the browser for drawing. Simply put, if the context does not exist, Canvas exists in name only. There are several ways to test whether a browser supports Canvas. The first method is to check whether the getContext method of the Canvas element in the HTML page exists:
In the above code, if the test fails, then the return statement is executed and the program terminates.
Another method is to create a function specifically used to determine whether Canvas is supported, and in this function, a Canvas element is generated in real time to make this determination - this method is very popular, Mark Pilgrim in his HTML5 This solution is mentioned on the website http://diveintohtml5.org:
Introducing modernizr into the HTML page is very simple, download the code from http://www.modernizr.com, and then include this external js file in the HTML page: