Skip to main content
Engineering LibreTexts

12.2: HTML Canvas 2D Examples

  • Page ID
    14101
  • The 2D canvas API is discussed in Section 2.6, and several examples are mentioned in that section. Canvas examples are written as web pages, and you can look at the web page in a browser to see what it does. However, you are also meant to read the source code. And the first four examples are meant to be used as a basis for your own experimentation. (Note that all of the live demos in Chapter 2 use the canvas API, but you are not necessarily meant to understand the source code of the demos.) In the web site download of this book, you can find these examples in the canvas2d folder inside the source folder.

    • GraphicsStarter.html is a minimal framework for drawing on an HTML canvas. It includes examples of drawing text and various shapes.
    • GraphicsPlusStarter.html adds a few convenience functions to the previous example, in- cluding a function for setting up a coordinate system on the canvas and functions to draw shapes such as lines and ovals that are not included in the basic API. It includes examples of using transforms
    • AnimationStarter.html adds animation to the previous example and includes a simple example using hierarchical modeling.
    • EventsStarter.html is a minimal framework for using keyboard and mouse events with a canvas, with some examples of basic event handling.
    • SimplePaintProgram.html lets the user draw using simple shapes. There is also a “smudge” tool that lets the user smudge the drawing as if it is drawn in wet paint; this is an example of pixel manipulation in the HTML canvas API. The program shows how to use an “off-screen canvas.” The demo program c2/SimplePaintDemo.html is a version of this program that uses an “overlay canvas” instead of an off-screen canvas.
    • Was this article helpful?