background preloader

HTML5

Facebook Twitter

HTML5 Canvas Tutorial: An Introduction. One of the most important instruments in a painter’s toolkit is their canvas.

HTML5 Canvas Tutorial: An Introduction

It gives them the freedom to express all kinds of feelings, impressions, ideas, and so forth, in almost unlimited variations and combinations. And that freedom can be restricted only by two things — their skill level and their imagination. The situation in the web development world is similar. With the ongoing progress of HTML5 and its powerful specifications, web developers have gained the ability to do things that were impossible in the past. Drawing graphics and creating animations directly in the browser is now completely possible thanks to a technology called HTML5 Canvas. What is HTML5 Canvas? The canvas element is an element defined in HTML code using width and height attributes. What’s so Great About HTML5 Canvas? Here are some reasons you might want to consider learning to use HTML5’s canvas feature: Interactivity.

What Applications Can You Create with HTML5 Canvas? HTML Canvas 2D Context. This Version: Latest Published Version: Previous Version: Editors: Rik Cabanier, Adobe Systems, Inc.

HTML Canvas 2D Context

Jatinder Mann, Microsoft Corporation Jay Munro, Microsoft Corporation Tom Wiltzius, Google, Inc. Ian Hickson, Google, Inc. Please check the errata for any errors or issues reported since publication. See also translations. Copyright © 2015 W3C® (MIT, ERCIM, Keio, Beihang). Abstract This specification defines the 2D Context for the HTML canvas element. Status of This Document This section describes the status of this document at the time of its publication.

This is the specification for the 2D Context for the HTML canvas element, published by the HTML Working Group. If you wish to make comments regarding this document in a manner that is tracked by the W3C, please submit them via using our public issues list. Work on extending this specification typically proceeds through extension specifications which should be consulted to see what new features are being reviewed. Table of Contents. Canvas. You are here: Home Dive Into HTML5 Diving In HTML 5 defines the <canvas> element as “a resolution-dependent bitmap canvas which can be used for rendering graphs, game graphics, or other visual images on the fly.”

Canvas

A canvas is a rectangle in your page where you can use JavaScript to draw anything you want. So what does a canvas look like? Invisible canvas The markup looks like this: Let’s add a dotted border so we can see what we’re dealing with. Canvas with border You can have more than one <canvas> element on the same page. Let’s expand that markup to include an id attribute: Now you can easily find that <canvas> element in the DOM. var a_canvas = document.getElementById("a"); Simple Shapes Every canvas starts out blank.

Click to draw on this canvas The onclick handler called this function: function draw_b() { var b_canvas = document.getElementById("b"); var b_context = b_canvas.getContext("2d"); b_context.fillRect(50, 25, 150, 100); } And then there’s this Every canvas has a drawing context Paths path. HTML5 Canvas Tutorials. Canvas tutorial - Web developer guide. <canvas> is a HTML element which can be used to draw graphics using scripting (usually JavaScript).

Canvas tutorial - Web developer guide

This can, for instance, be used to draw graphs, make photo composition or simple (and not so simple) animations. The images on the right show examples of implementations <canvas> which they will be in the future in this tutorial. <canvas> was first introduced by Apple for the Mac OS X Dashboard and later implemented in Safari and Google Chrome. Gecko 1.8-based browsers, such as Firefox 1.5, also support this element. The <canvas> element is part of the WhatWG Web applications 1.0 specification also known as HTML5. This tutorial describes how to use the <canvas> element to draw 2D graphics, starting with the basics.

Before you start Using the <canvas> element isn't very difficult but you do need a basic understanding of HTML and JavaScript. In this tutorial See also A note to contributors Document Tags and Contributors.