2. WHAT HTML5 CANVAS IS USED FOR
■ dynamic graphics
■ online and offline games
■ animations
■ interactive video and audio
Easy to turn a plain web page into a dynamic web application and then convert that
application into a mobile app for use on smartphones and tablets.
3. Skeleton
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<script type="application/javascript">
function draw() {
var canvas = document.getElementById('canvas');
if (canvas.getContext) {
var ctx = canvas.getContext('2d');
//Draw Here }
}else{
//Fall bac kContent Goes Here
}
</script>
</head>
<body>
<canvas id="canvas" width="150" height="150"></canvas> //Create a Canvas
</body>
</html>
5. Curves
var x = canvas.width / 2;
var y = canvas.height / 2;
var radius = 75;
var startAngle = 0 * Math.PI;
var endAngle = 2 * Math.PI;
context.beginPath();
context.arc(x, y, radius, startAngle, endAngle);
context.lineWidth = 15;
// line color
context.strokeStyle = 'green';
context.stroke();
Arc
10. Fill Style
context.fillStyle = '#8ED6FF';
context.fill();
Color Fill
// add linear gradient
var grd = context.createLinearGradient(0, 0, canvas.width, canvas.height);
// light blue
grd.addColorStop(0, '#8ED6FF');
// dark blue
grd.addColorStop(1, '#004CB3');
context.fillStyle = grd;
context.fill();
Linear Gradient
11. Radial Gradient
// create radial gradient
var grd = context.createRadialGradient(238, 50, 10, 238,50, 300);
// light blue
grd.addColorStop(0, '#8ED6FF');
// dark blue
grd.addColorStop(1, '#004CB3');
context.fillStyle = grd;
context.fill();
12. Pattern
var imageObj = new Image();
imageObj.onload = function() {
var pattern = context.createPattern(imageObj, 'repeat');
context.rect(0, 0, canvas.width, canvas.height);
context.fillStyle = pattern;
context.fill();
};
imageObj.src =
'http://www.html5canvastutorials.com/demos/assets/wood-
pattern.png';
13. Text
context.textAlign = 'left';
ontext.strokeText('Hello World!', x, y);
Align
Stroke
context.fillStyle = 'blue';
Color
context.font = 'italic 40pt Calibri';
Font Size Style
context.measureText(text);
Metricies
wrapText(context, text, x, y, maxWidth, lineHeight);
WrapText