These slides were largely inspired by "SVG for JavaScript developers" and "How to use SVG, Canvas, and WebGL without a Time Machine".
Raster | Vector | Motion | Interactive | 2D | 3D | |
Images (GIF, BMP, PNG, JPG) | ✔ | ✖ | ✔ | ✖ | ✔ | ✖ |
DOM + JS (Example) | ✔ | ✖ | ✔ | ✔ | ✔ | ✖ |
Plugins (Flash, Java, X3D, SVG, VRML) | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ |
Browser-specific technologies (IE's VML) | ✖ | ✔ | ✔ | ✔ | ✔ | ✖ |
Raster | Vector | Motion | Interactive | 2D | 3D | |
SVG | ✖ | ✔ | ✔ | ✔ | ✔ | ✖ |
Canvas | ✔ | ✖ | ✔ | ✔ | ✔ | ✖ |
WebGL | ✖ | ✔ | ✔ | ✔ | ✔ | ✔ |
CSS3 | ✔ | ✖ | ✔ | ✔ | ✔ | ✔ |
An XML-based vector image format for two-dimensional graphics that has support for interactivity and animation.
<circle cx="100" cy="50" r="25" fill="black" />
<rect x="0" y="10" width="100" height="75" fill="red" />
<ellipse cx="90" cy="50" rx="50" ry="45" fill="orange" />
<polygon points="10,5 100,45 100,5 100,100" fill="blue" />
An HTML tag that creates a blank canvas for JavaScript to draw onto.
<canvas id="myDrawing" width="500" height="500"></canvas>
var canvas = document.getElementById("myDrawing");
var context = canvas.getContext("2d");
fillRect(x, y, width, height);
strokeRect(x, y, width, height);
clearRect(x, y, width, height);
moveTo(x, y);
lineTo(x, y);
rect(x, y, width, height);
arc(x, y, radius, startAngle, endAngle, counterclockwise);
beginPath();
closePath();
fill();
stroke();
var FPS = 60; setInterval(function() { redrawCanvas(); // your function here }, 1000 / FPS);
window.requestAnimationFrame(function() { redrawCanvas(); // your function here });
<canvas id="myDrawing" width="500" height="500">
var canvas = document.getElementById("myDrawing"); context = canvas.getContext("experimental-webgl");
But most people use the ThreeJS library...
Create a WebGLRenderer
var renderer = new THREE.WebGLRenderer({antialias: true}); renderer.setSize(document.body.clientWidth, document.body.clientHeight);
Put it on the DOM
document.body.appendChild(renderer.domElement);
Set some color
renderer.setClearColorHex(0xFFFFFF, 1.0); renderer.clear();Demo
Create a Camera
var camera = new THREE.PerspectiveCamera(45, width/height, 1, 10000); camera.position.z = 300;
Make a Scene with a Cube
var scene = new THREE.Scene(); var cube = new THREE.Mesh(new THREE.CubeGeometry(50,50,50), new THREE.MeshBasicMaterial({color: 0x1fd230})); scene.add(cube);
And render the Scene from the Camera
renderer.render(scene, camera);Demo
function animate(t) { // spin the camera in a circle camera.position.x = Math.sin(t/1000)*300; camera.position.y = 150; camera.position.z = Math.cos(t/1000)*300; // you need to update lookAt every frame camera.lookAt(scene.position); // renderer automatically clears unless autoClear = false renderer.render(scene, camera); window.requestAnimationFrame(animate, renderer.domElement); }; animate(new Date().getTime());Demo
CSS lets you specify keyframes, animations, and transitions for styles.
#tardis { animation-name: disappear; animation-duration: 1s; animation-timing-function: linear; animation-iteration-count: 1; animation-delay: 2s; } @keyframes disappear { from { opacity: 1; } to { opacity: 0; } }
Raster | Vector | Motion | Interactive | 2D | 3D | |
SVG | ✖ | ✔ | ✔ | ✔ | ✔ | ✖ |
Canvas | ✔ | ✖ | ✔ | ✔ | ✔ | ✖ |
WebGL | ✖ | ✔ | ✔ | ✔ | ✔ | ✔ |
CSS3 | ✔ | ✖ | ✔ | ✔ | ✔ | ✔ |
When to use which? Depends on performance, features, interactivity, and target browsers.