48
CANVAS introduction to: Tuesday, September 3, 13

Introduction to HTML5 Canvas

Embed Size (px)

DESCRIPTION

Delivered to Fort Collins Internet Pros Meetup Group - Sept 3, 2013.

Citation preview

Page 1: Introduction to HTML5 Canvas

CANVASintroduction to:

Tuesday, September 3, 13

Page 2: Introduction to HTML5 Canvas

Mark J. Morris @blurredbits

presented by:

September 3, 2013

Tuesday, September 3, 13

Page 3: Introduction to HTML5 Canvas

“Added in HTML5, the HTML <canvas> element is an element which can be used to draw graphics via scripting (usually Javascript).”

Tuesday, September 3, 13

Page 4: Introduction to HTML5 Canvas

Chrome 25+

Firefox 20+

Safari 5+

IE 9.0+

Opera 9.0+

Tuesday, September 3, 13

Page 5: Introduction to HTML5 Canvas

index.html

<!DOCTYPE html><html><head><title>Canvas Intro</title><link rel=”stylesheet” href=”css/style.css”>

</head><body>

<script src=”js/canvas.js”></script></body>

</html>

Tuesday, September 3, 13

Page 6: Introduction to HTML5 Canvas

index.html

<!DOCTYPE html><html><head><title>Canvas Intro</title><link rel=”stylesheet” href=”css/style.css”>

</head><body>

<script src=”js/canvas.js”></script></body>

</html>

<canvas id=”intro” width=300 height=150>

</canvas>

Tuesday, September 3, 13

Page 7: Introduction to HTML5 Canvas

index.html

<!DOCTYPE html><html><head><title>Canvas Intro</title><link rel=”stylesheet” href=”css/style.css”>

</head><body>

<script src=”js/canvas.js”></script></body>

</html>

<canvas id=”intro” width=300 height=150>

</canvas><p>Oh noes! No canvas support!</p>

Tuesday, September 3, 13

Page 8: Introduction to HTML5 Canvas

Tuesday, September 3, 13

Page 9: Introduction to HTML5 Canvas

Tuesday, September 3, 13

Page 10: Introduction to HTML5 Canvas

Tuesday, September 3, 13

Page 11: Introduction to HTML5 Canvas

basic canvas method

fillRect(float x, float y, float width, float height)

Tuesday, September 3, 13

Page 12: Introduction to HTML5 Canvas

Tuesday, September 3, 13

Page 13: Introduction to HTML5 Canvas

js/canvas.js

window.onload = canvasApp();

function canvasApp () {

}

var theCanvas = document.getElementById(‘intro’).getContext(‘2d’);

theCanvas.fillStyle = "rgb(160, 160, 160)";theCanvas.fillRect(0, 0, 50, 50);

Tuesday, September 3, 13

Page 14: Introduction to HTML5 Canvas

js/canvas.js

window.onload = canvasApp();

function canvasApp () {

}

var theCanvas = document.getElementById(‘intro’).getContext(‘2d’);

theCanvas.fillStyle = "rgb(160, 160, 160)";theCanvas.fillRect(0, 0, 50, 50);

Tuesday, September 3, 13

Page 15: Introduction to HTML5 Canvas

theCanvas.fillStyle = “orange”;

theCanvas.fillStyle = “#FFA500”;

theCanvas.fillStyle = “rgb(255,165,0)”;

theCanvas.fillStyle = “rgba(255,165,0,1)”;

Tuesday, September 3, 13

Page 16: Introduction to HTML5 Canvas

js/canvas.js

window.onload = canvasApp();

function canvasApp () {

}

var theCanvas = document.getElementById(‘intro’).getContext(‘2d’);

theCanvas.fillStyle = "rgb(160, 160, 160)";theCanvas.fillRect(0, 0, 50, 50);

Tuesday, September 3, 13

Page 17: Introduction to HTML5 Canvas

Tuesday, September 3, 13

Page 18: Introduction to HTML5 Canvas

Text Methods

Tuesday, September 3, 13

Page 19: Introduction to HTML5 Canvas

js/canvas.js

window.onload = canvasApp();

function canvasApp () {

}

!

! theCanvas.fillStyle = "blue";! theCanvas.font = "30px Arial";! theCanvas.textBaseline = "top";! theCanvas.fillText("Fort Collins", 0, 0);! theCanvas.fillStyle = "red";! theCanvas.fillText("Internet", 0, 50);! theCanvas.fillStyle = "blue";! theCanvas.fillText("Pros",0,100);

var theCanvas = document.getElementById(‘intro’).getContext(‘2d’);

Tuesday, September 3, 13

Page 20: Introduction to HTML5 Canvas

js/canvas.js

window.onload = canvasApp();

function canvasApp () {

}

!

! theCanvas.fillStyle = "blue";! theCanvas.font = "30px Arial";! theCanvas.textBaseline = "top";! theCanvas.fillText("Fort Collins", 0, 0);! theCanvas.fillStyle = "red";! theCanvas.fillText("Internet", 0, 50);! theCanvas.fillStyle = "blue";! theCanvas.fillText("Pros",0,100);

var theCanvas = document.getElementById(‘intro’).getContext(‘2d’);

Tuesday, September 3, 13

Page 21: Introduction to HTML5 Canvas

js/canvas.js

window.onload = canvasApp();

function canvasApp () {

}

!

! theCanvas.fillStyle = "blue";! theCanvas.font = "30px Arial";! theCanvas.textBaseline = "top";! theCanvas.fillText("Fort Collins", 0, 0);! theCanvas.fillStyle = "red";! theCanvas.fillText("Internet", 0, 50);! theCanvas.fillStyle = "blue";! theCanvas.fillText("Pros",0,100);

var theCanvas = document.getElementById(‘intro’).getContext(‘2d’);

Tuesday, September 3, 13

Page 22: Introduction to HTML5 Canvas

js/canvas.js

window.onload = canvasApp();

function canvasApp () {

}

!

! theCanvas.fillStyle = "blue";! theCanvas.font = "30px Arial";! theCanvas.textBaseline = "top";! theCanvas.fillText("Fort Collins", 0, 0);! theCanvas.fillStyle = "red";! theCanvas.fillText("Internet", 0, 50);! theCanvas.fillStyle = "blue";! theCanvas.fillText("Pros",0,100);

var theCanvas = document.getElementById(‘intro’).getContext(‘2d’);

Tuesday, September 3, 13

Page 23: Introduction to HTML5 Canvas

Tuesday, September 3, 13

Page 24: Introduction to HTML5 Canvas

Line Methods

Tuesday, September 3, 13

Page 25: Introduction to HTML5 Canvas

js/canvas.js

window.onload = canvasApp();

function canvasApp () {

}

!! theCanvas.beginPath();! theCanvas.moveTo(50, 25);! theCanvas.lineTo(50, 125);! theCanvas.lineTo(150, 125);! theCanvas.closePath();! theCanvas.stroke();

var theCanvas = document.getElementById(‘intro’).getContext(‘2d’);

Tuesday, September 3, 13

Page 26: Introduction to HTML5 Canvas

js/canvas.js

window.onload = canvasApp();

function canvasApp () {

}

!! theCanvas.beginPath();! theCanvas.moveTo(50, 25);! theCanvas.lineTo(50, 125);! theCanvas.lineTo(150, 125);! theCanvas.closePath();! theCanvas.stroke();

var theCanvas = document.getElementById(‘intro’).getContext(‘2d’);

Tuesday, September 3, 13

Page 27: Introduction to HTML5 Canvas

js/canvas.js

window.onload = canvasApp();

function canvasApp () {

}

!! theCanvas.beginPath();! theCanvas.moveTo(50, 25);! theCanvas.lineTo(50, 125);! theCanvas.lineTo(150, 125);! theCanvas.closePath();! theCanvas.stroke();

var theCanvas = document.getElementById(‘intro’).getContext(‘2d’);

Tuesday, September 3, 13

Page 28: Introduction to HTML5 Canvas

js/canvas.js

window.onload = canvasApp();

function canvasApp () {

}

!! theCanvas.beginPath();! theCanvas.moveTo(50, 25);! theCanvas.lineTo(50, 125);! theCanvas.lineTo(150, 125);! theCanvas.closePath();! theCanvas.stroke();

var theCanvas = document.getElementById(‘intro’).getContext(‘2d’);

Tuesday, September 3, 13

Page 29: Introduction to HTML5 Canvas

js/canvas.js

window.onload = canvasApp();

function canvasApp () {

}

!! theCanvas.beginPath();! theCanvas.moveTo(50, 25);! theCanvas.lineTo(50, 125);! theCanvas.lineTo(150, 125);! theCanvas.closePath();! theCanvas.stroke();

var theCanvas = document.getElementById(‘intro’).getContext(‘2d’);

Tuesday, September 3, 13

Page 30: Introduction to HTML5 Canvas

Tuesday, September 3, 13

Page 31: Introduction to HTML5 Canvas

(2,1)

(2,4)

Tuesday, September 3, 13

Page 32: Introduction to HTML5 Canvas

(1.5,1)

(1.5,4)

Tuesday, September 3, 13

Page 33: Introduction to HTML5 Canvas

js/canvas.js

window.onload = canvasApp();

function canvasApp () {

}

!! theCanvas.beginPath();! theCanvas.moveTo(50.5, 25.5);! theCanvas.lineTo(50.5, 125.5);! theCanvas.lineTo(150.5, 125.5);! theCanvas.closePath();! theCanvas.stroke();

var theCanvas = document.getElementById(‘intro’).getContext(‘2d’);

Tuesday, September 3, 13

Page 34: Introduction to HTML5 Canvas

Tuesday, September 3, 13

Page 35: Introduction to HTML5 Canvas

arcsarc(x, y, radius, startAngle, endAngle, anticlockwise)

bezierbezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y)

quadraticquadraticCurveTo(cp1x, cp1y, x, y)

Tuesday, September 3, 13

Page 36: Introduction to HTML5 Canvas

Image Methods

Tuesday, September 3, 13

Page 37: Introduction to HTML5 Canvas

js/canvas.js

window.onload = canvasApp();

function canvasApp () {

}

! var img = new Image(); img.src = 'https://mdn.mozillademos.org/files/5397/rhino.jpg';! img.onload = function(){! ! theCanvas.drawImage(img,0,0);!! };

var theCanvas = document.getElementById(‘intro’).getContext(‘2d’);

Tuesday, September 3, 13

Page 38: Introduction to HTML5 Canvas

js/canvas.js

window.onload = canvasApp();

function canvasApp () {

}

! var img = new Image(); img.src = 'https://mdn.mozillademos.org/files/5397/rhino.jpg';! img.onload = function(){! ! theCanvas.drawImage(img,0,0);!! };

var theCanvas = document.getElementById(‘intro’).getContext(‘2d’);

Tuesday, September 3, 13

Page 39: Introduction to HTML5 Canvas

js/canvas.js

window.onload = canvasApp();

function canvasApp () {

}

! var img = new Image(); img.src = 'https://mdn.mozillademos.org/files/5397/rhino.jpg';! img.onload = function(){! ! theCanvas.drawImage(img,0,0);!! };

var theCanvas = document.getElementById(‘intro’).getContext(‘2d’);

Tuesday, September 3, 13

Page 40: Introduction to HTML5 Canvas

Tuesday, September 3, 13

Page 41: Introduction to HTML5 Canvas

scalingdrawImage(image, x, y, width, height)

slicingdrawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight)

Tuesday, September 3, 13

Page 42: Introduction to HTML5 Canvas

GradientsAnimationsPatternsShadowsTransformationsCompositingVideoAudio

Tuesday, September 3, 13

Page 43: Introduction to HTML5 Canvas

Additional Resources

Tuesday, September 3, 13

Page 44: Introduction to HTML5 Canvas

Tuesday, September 3, 13

Page 45: Introduction to HTML5 Canvas

Tuesday, September 3, 13

Page 46: Introduction to HTML5 Canvas

https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Canvas_tutorial

Tuesday, September 3, 13

Page 47: Introduction to HTML5 Canvas

Thursday 9/56:00pm

Crooked Cup

Tuesday, September 3, 13

Page 48: Introduction to HTML5 Canvas

Mark J. Morris @blurredbits

Thanks!

Tuesday, September 3, 13