Channels ▼

Web Development

The Arrival of HTML 5

Not By Text Alone

Although HTML was initially a text markup language, its abilities evolved from its 'textual' bounds long ago. New native elements in HTML 5 allow authors to operate non-textual information such as video and sound. HTML 5 has not made major developments in image support.

The new <video> element provides the ability to embed videos inside web documents without using third-party plug-ins like Adobe Flash or Microsoft Silverlight. Below is a simple example of a video element with explicit width and height specifications for a poster image (e.g. representative frame from the video):

<video src="good-dog.flv" width=200 height=120 controls 
       poster=preview.jpg />

The next example illustrates a more complex example where a <video> element may include several sources of a video encoded using different codecs and alternative content to be shown if a browser does not support the video:

<video width=200 height=120 controls poster=preview.jpg>
  <source src=video.ogv type=video/ogg>
  <source src=video.mp4 type=video/mp4>	
  Your browser does not support video. 
  Please <a href=video.ogv>download it</a>.

Note: An even more complex example may include an <object> element inside the <video> tag to play the video using Flash if the browser does not support the <video> tag out-of-the-box. The <audio> element is very similar to the <video> element. Both DOM elements extend the same HTMLMediaElement for those have like attributes (the vast majority of them) and methods. Here is an example of that tag:

<audio src="audio.oga" controls>
  Your browser does not support audio. 

Note that there are many additional features of both elements not shown in the examples above such as buffering, auto-playing, and the ability to associate external tracks containing subtitles, captions or descriptions with media content. The bottom line is that, by using video and audio elements with a corresponding DOM interface, web developers can embed and manage multimedia resources without the headache related to embedding external plug-ins for different browsers.

One of the chief innovations related to data representation is the <canvas> element which lets JavaScript developers paint dynamic bitmap content and 2D shapes on the screen. The <canvas> element, in combination with modern speedy JavaScript engines, opens incredible possibilities in the creation of online games and advertisements. HTML 5.0 specification defines the HTMLCanvasElement.getContext(contextId) method that returns canvas context for drawing. Context APIs are not defined in HTML 5.0 specification itself. Context API for 2D rendering is defined in a separate HTML Canvas 2D Context W3C Recommendation that is also currently in a draft state. This 2D drawing API is rich enough to render complex 2D scenes. Dynamism can be achieved by clearing and re-rendering the whole scene or only parts of it. Below is a simple example of defining a canvas element and rendering a red rectangle using 2D context API:

<canvas id="canvas" width="200" height="200">
  Your browser does not support HTML5 Canvas.

<script type="text/javascript">
  var canvas = document.getElementById('canvas');
  var context = canvas.getContext('2d');
  context.fillStyle   = '#f00'; // red
  context.fillRect(0, 0, 100, 100); 


HTML 5.0 also allows embedding SVG (Scalable Vector Graphics) scenes inside a document by using the <SVG> element. SVG is a separate specification of another technology allowing us to define 2D scenes. Compared with <canvas>, SVG provides means for high-level operations of drawing static and dynamic scenes. Basically, SVG is an XML format for defining shapes, text and other visual objects in a scene. As a result, a developer using SVG defines objects to be rendered (i.e. declarative approach and vector graphics) instead of programmatically drawing these objects (i.e. imperative approach and bitmap graphics) when using <canvas>. A SVG scene can be rendered by a browser, a third-party application or a plug-in.

SVG is also much more convenient and powerful for developing dynamic scenes. All defined objects are remembered in a tree similar to DOM and can be manipulated by using languages like ECMAScript (JavaScript) embedded in SVG to change their attributes. This greatly simplifies implementation of dynamic scenes: a developer needs only to change the attributes of a defined object (e.g. position, size, color) and need not implement any code for clearing or re-rendering the scene. This is in sharp contrast with <canvas> where a developer must programmatically clear and re-render the scene. Note that SVG 1.1 supports animation (gradual timed change of position or other attributes) out-of-the-box. Here is an example of the same red rectangle defined in SVG:

<svg width="200" height="200" version="1.1"
 <rect width="100" height="100" style="fill:rgb(255,0,0)"/>

A quality critique that helps one decide between using SVG and Canvas by exploring the advantages and disadvantages of each technology for different tasks can be found here. Note that SVG is an older, but less supported technology compared to HTML 5 Canvas.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.