CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload =...

48
CANVAS introduction to: Tuesday, September 3, 13

Transcript of CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload =...

Page 1: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

CANVASintroduction to:

Tuesday, September 3, 13

Page 2: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

Mark J. Morris @blurredbits

presented by:

September 3, 2013

Tuesday, September 3, 13

Page 3: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

“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: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

Chrome 25+

Firefox 20+

Safari 5+

IE 9.0+

Opera 9.0+

Tuesday, September 3, 13

Page 5: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

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: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

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: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

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: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

Tuesday, September 3, 13

Page 9: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

Tuesday, September 3, 13

Page 10: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

Tuesday, September 3, 13

Page 11: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

basic canvas method

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

Tuesday, September 3, 13

Page 12: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

Tuesday, September 3, 13

Page 13: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

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: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

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: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

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: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

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: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

Tuesday, September 3, 13

Page 18: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

Text Methods

Tuesday, September 3, 13

Page 19: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

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: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

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: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

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: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

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: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

Tuesday, September 3, 13

Page 24: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

Line Methods

Tuesday, September 3, 13

Page 25: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

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: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

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: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

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: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

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: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

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: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

Tuesday, September 3, 13

Page 31: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

(2,1)

(2,4)

Tuesday, September 3, 13

Page 32: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

(1.5,1)

(1.5,4)

Tuesday, September 3, 13

Page 33: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

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: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

Tuesday, September 3, 13

Page 35: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

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: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

Image Methods

Tuesday, September 3, 13

Page 37: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

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: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

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: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

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: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

Tuesday, September 3, 13

Page 41: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

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

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

Tuesday, September 3, 13

Page 42: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

GradientsAnimationsPatternsShadowsTransformationsCompositingVideoAudio

Tuesday, September 3, 13

Page 43: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

Additional Resources

Tuesday, September 3, 13

Page 44: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

Tuesday, September 3, 13

Page 45: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

Tuesday, September 3, 13

Page 46: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

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

Tuesday, September 3, 13

Page 47: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

Thursday 9/56:00pm

Crooked Cup

Tuesday, September 3, 13

Page 48: CANVAS - Meetupfiles.meetup.com/981826/intro_canvas.pdf · js/canvas.js window.onload = canvasApp(); function canvasApp {}!! theCanvas.fillStyle = "blue";!theCanvas.font = "30px Arial";!theCanvas.textBaseline

Mark J. Morris @blurredbits

Thanks!

Tuesday, September 3, 13