Top Banner
1 Drawing Using HTML5 Canvas Go to http://o.ooli.ca/canvasjsbin Presentation copy http:// ?
295

Drawing Using HTML5 Canvas - Programming · PDF file1 Drawing Using HTML5 Canvas Go to Presentation copy http:// ?

Feb 23, 2018

Download

Documents

hakiet
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

1

Drawing Using HTML5 Canvas

● Go to http://o.ooli.ca/canvasjsbin● Presentation copy http://

?

Page 2: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

2

ADVANCED

Overview

● Main Topics– Lines

– Color

– Shapes

– Images

● Optional– Curves

– Bezier curves

– Text

Page 3: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

3

Computer screens = dots

Page 4: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

4

Change colors of the dots to make pictures

Page 5: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

5

Change colors of the dots to make pictures

Page 6: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

6

Dots = pixels

8 megapixels

800 x 480

720p HD

Page 7: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

7

Where is the dot?

Page 8: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

8

Where is the dot?

Page 9: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

9

Where is the dot?

Page 10: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

10

Where is the dot?

Page 11: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

11

Where is the dot?

3

Page 12: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

12

Where is the dot?

3

Page 13: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

13

Where is the dot?

3

Page 14: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

14

Where is the dot?

3, 13

Page 15: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

15

Math it up!

3, 1x y

Page 16: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

16

Let's Start Programming: jsbinhttp://o.ooli.ca/canvasjsbin

Page 17: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

17

jsbinhttp://o.ooli.ca/canvasjsbin

<!DOCTYPE html><canvas id="canvas" width="500" height="500"></canvas><script> canvas = document.getElementById('canvas'); c = canvas.getContext('2d'); function wait(fn) {window.setTimeout(fn, 250); }</script>

Page 18: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

18

Let's Draw Somethinghttp://o.ooli.ca/canvasjsbin

Page 19: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

19

Let's Draw Somethinghttp://o.ooli.ca/canvasjsbin

Page 20: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

20

Let's Draw Somethinghttp://o.ooli.ca/canvasjsbin

Checking for Errors

cmd - op⌘ tion alt - C⌥ctrl - shift - J⇧F12

Page 21: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

21

What's Going On?

c.moveTo(20,90);c.lineTo(90,90);c.stroke();

Page 22: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

22

Canvas

c.moveTo(20,90);c.lineTo(90,90);c.stroke();

Page 23: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

23

Move Pen

c.moveTo(20,90);c.lineTo(90,90);c.stroke(); 20

90

Page 24: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

24

Tell Computer Where You Want a Line

c.moveTo(20,90);c.lineTo(90,90);c.stroke(); 90

90

Page 25: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

25

Tell Computer to Draw the Line

c.moveTo(20,90);c.lineTo(90,90);c.stroke();

Page 26: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

26

Tell Computer to Draw the Line

c.moveTo(20,90);c.lineTo(90,90);c.stroke();

Page 27: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

27

Tell Computer to Draw the Line

c.moveTo(20,90);c.lineTo(90,90);c.stroke();

Page 28: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

28

Make Your Own Picture

c.moveTo(20,90);c.lineTo(90,90);c.lineTo(90,140);c.lineTo(20,140);c.lineTo(20,90);c.lineTo(55,60);c.lineTo(90,90);

c.moveTo(45,140);c.lineTo(45,115);c.lineTo(65,115);c.lineTo(65,140);c.stroke();

Page 29: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

29

Make Your Own Picture

Page 30: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

30

Make Your Own Picture

c.moveTo(20,90);

Page 31: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

31

Make Your Own Picture

c.moveTo(20,90);c.lineTo(90,90);

Page 32: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

32

Make Your Own Picture

c.moveTo(20,90);c.lineTo(90,90);c.lineTo(90,140);

Page 33: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

33

Make Your Own Picture

c.moveTo(20,90);c.lineTo(90,90);

c.lineTo(20,140);c.lineTo(90,140);

Page 34: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

34

Make Your Own Picture

c.moveTo(20,90);c.lineTo(90,90);

c.lineTo(20,90);c.lineTo(20,140);c.lineTo(90,140);

Page 35: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

35

Make Your Own Picture

c.moveTo(20,90);c.lineTo(90,90);

c.lineTo(55,60);c.lineTo(20,90);c.lineTo(20,140);c.lineTo(90,140);

Page 36: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

36

Make Your Own Picture

c.moveTo(20,90);c.lineTo(90,90);

c.lineTo(90,90);c.lineTo(55,60);c.lineTo(20,90);c.lineTo(20,140);c.lineTo(90,140);

Page 37: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

37

Make Your Own Picture

c.moveTo(20,90);c.lineTo(90,90);

c.moveTo(45,140);

c.lineTo(90,90);c.lineTo(55,60);c.lineTo(20,90);c.lineTo(20,140);c.lineTo(90,140);

Page 38: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

38

Make Your Own Picture

c.moveTo(20,90);c.lineTo(90,90);

c.lineTo(45,115);c.moveTo(45,140);

c.lineTo(90,90);c.lineTo(55,60);c.lineTo(20,90);c.lineTo(20,140);c.lineTo(90,140);

Page 39: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

39

Make Your Own Picture

c.moveTo(20,90);c.lineTo(90,90);

c.lineTo(65,115);c.lineTo(45,115);c.moveTo(45,140);

c.lineTo(90,90);c.lineTo(55,60);c.lineTo(20,90);c.lineTo(20,140);c.lineTo(90,140);

Page 40: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

40

Make Your Own Picture

c.moveTo(20,90);c.lineTo(90,90);

c.lineTo(65,140);c.lineTo(65,115);c.lineTo(45,115);c.moveTo(45,140);

c.lineTo(90,90);c.lineTo(55,60);c.lineTo(20,90);c.lineTo(20,140);c.lineTo(90,140);

Page 41: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

41

Make Your Own Picture

c.moveTo(20,90);c.lineTo(90,90);

c.stroke();c.lineTo(65,140);c.lineTo(65,115);c.lineTo(45,115);c.moveTo(45,140);

c.lineTo(90,90);c.lineTo(55,60);c.lineTo(20,90);c.lineTo(20,140);c.lineTo(90,140);

Page 42: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

42

IdeasDrawing a Line

Checking for Errors

c.moveTo(20,90);c.lineTo(90,90);c.stroke();

cmd - op⌘ tion alt - C⌥ctrl - shift - J⇧

F12

Page 43: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

43

Curves

Page 44: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

44

Curves

Page 45: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

45

Curves

Page 46: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

46

Curves

Page 47: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

47

Which Curve?

Page 48: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

48

Which Curve?

Page 49: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

49

Which Curve?

Page 50: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

50

Curves

Page 51: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

51

Curves

Page 52: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

52

Curves

Page 53: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

53

Curves

Page 54: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

54

Curves

Page 55: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

55

Curves

Page 56: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

56

Curves

Page 57: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

57

Curves

Page 58: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

58

Curves

Page 59: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

59

c.moveTo(20,20);c.quadraticCurveTo(90,20,100,90);c.stroke();

(20,20) (90,20)

(100,90)

Page 60: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

60

c.moveTo(20,20);c.quadraticCurveTo(90,20,100,90);c.stroke();

(20,20) (90,20)

(100,90)

Page 61: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

61

c.moveTo(20,20);c.quadraticCurveTo(90,20,100,90);c.stroke();

(20,20) (90,20)

(100,90)

Page 62: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

62

c.moveTo(20,20);c.quadraticCurveTo(90,20,100,90);c.stroke();

(20,20) (90,20)

(100,90)

Page 63: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

63

c.moveTo(20,20);c.quadraticCurveTo(90,20,100,90);c.stroke();

(20,20) (90,20)

(100,90)

Page 64: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

64

c.moveTo(20,20);c.quadraticCurveTo(90,20,100,90);c.stroke();

(20,20) (90,20)

(100,90)

Page 65: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

65

c.moveTo(20,20);c.quadraticCurveTo(90,20,100,90);c.stroke();

(20,20) (90,20)

(100,90)

Page 66: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

66

c.moveTo(20,20);c.quadraticCurveTo(90,20,100,90);c.stroke();

(20,20) (90,20)

(100,90)

Page 67: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

67ADVANCED

Drawing a Curve

Ideas

c.moveTo(20,20);c.quadraticCurveTo(90,20,100,90);c.stroke();

cmd - op⌘ tion alt - C⌥ctrl - shift - J⇧F12

Page 68: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

68

Colored Lines

Page 69: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

69

c.moveTo(20,90);c.lineTo(90,90);c.stroke();

Review

Page 70: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

70

c.moveTo(20,90);c.lineTo(90,90);c.stroke();

Review

Page 71: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

71

c.moveTo(20,90);c.lineTo(90,90);c.stroke();

Review

Page 72: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

72

c.moveTo(20,90);c.lineTo(90,90);c.stroke();

Review

Page 73: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

73

c.moveTo(20,90);c.lineTo(90,90);c.strokeStyle = 'red';c.stroke();

Colored Lines

Page 74: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

74

c.moveTo(20,90);c.lineTo(90,90);c.strokeStyle = 'red';c.stroke();

Colored Lines

Page 75: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

75

c.moveTo(20,90);c.lineTo(90,90);c.strokeStyle = 'red';c.stroke();

Colors

blackbluebrown

greenorangepurple

redwhiteyellowyellow

pinkgray

Page 76: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

76

Drawing Two Lines

Page 77: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

77

c.moveTo(5,5);c.lineTo(90,5);c.strokeStyle='green';c.stroke();

Drawing Two Lines

Page 78: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

78

c.moveTo(5,5);c.lineTo(90,5);c.strokeStyle='green';c.stroke();

Drawing Two Lines

c.moveTo(20,20);c.lineTo(90,20);c.strokeStyle='red';c.stroke();

Page 79: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

79

c.moveTo(5,5);c.lineTo(90,5);c.strokeStyle='green';c.stroke();

Drawing Two Lines

c.moveTo(20,20);c.lineTo(90,20);c.strokeStyle='red';c.stroke();

Page 80: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

80

c.moveTo(5,5);c.lineTo(90,5);c.strokeStyle='green';c.stroke();

c.beginPath();c.moveTo(20,20);c.lineTo(90,20);c.strokeStyle='red';c.stroke();

Different Lines, Different Colors

Page 81: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

81

Colored Lines

Ideas

c.beginPath();c.moveTo(20,20);c.lineTo(90,20);c.lineTo(90,90);c.strokeStyle='red';c.stroke();

cmd - op⌘ tion alt - C⌥ctrl - shift - J⇧F12

blackbluebrowngreenorangepurpleredwhiteyellowyellowpinkgray

Page 82: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

82

Crazy Curves

Page 83: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

83

Crazy Curves

Page 84: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

84

Crazy Curves

Page 85: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

85

Crazy Curves

Page 86: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

86

Crazy Curves

Page 87: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

87

4 Point Bezier Curve

● Invented by French mathematicians/engineers around 1960

● Adobe used them in computer graphics in the 1980s

Page 88: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

88

4 Point Bezier Curve

● Invented by French mathematicians/engineers around 1960

● Adobe used them in computer graphics in the 1980s

Page 89: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

89

4 Point Bezier Curve

● Invented by French mathematicians/engineers around 1960

● Adobe used them in computer graphics in the 1980s

Page 90: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

90

4 Point Bezier Curve

● Invented by French mathematicians/engineers around 1960

● Adobe used them in computer graphics in the 1980s

Page 91: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

91

4 Point Bezier Curve

● Invented by French mathematicians/engineers around 1960

● Adobe used them in computer graphics in the 1980s

Page 92: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

92

4 Point Bezier Curve

● Invented by French mathematicians/engineers around 1960

● Adobe used them in computer graphics in the 1980s

Page 93: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

93

4 Point Bezier Curve

● Invented by French mathematicians/engineers around 1960

● Adobe used them in computer graphics in the 1980s

Page 94: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

94

4 Point Bezier Curve

● Invented by French mathematicians/engineers around 1960

● Adobe used them in computer graphics in the 1980s

Page 95: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

95

4 Point Bezier Curve

● Invented by French mathematicians/engineers around 1960

● Adobe used them in computer graphics in the 1980s

Page 96: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

96

4 Point Bezier Curve

● Invented by French mathematicians/engineers around 1960

● Adobe used them in computer graphics in the 1980s

Page 97: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

97

4 Point Bezier Curve

● Invented by French mathematicians/engineers around 1960

● Adobe used them in computer graphics in the 1980s

Page 98: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

98

4 Point Bezier Curve

● Invented by French mathematicians/engineers around 1960

● Adobe used them in computer graphics in the 1980s

Page 99: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

99

4 Point Bezier Curve

● Invented by French mathematicians/engineers around 1960

● Adobe used them in computer graphics in the 1980s

Page 100: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

100

4 Point Bezier Curve

● Invented by French mathematicians/engineers around 1960

● Adobe used them in computer graphics in the 1980s

Page 101: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

101

4 Point Bezier Curve

● Invented by French mathematicians/engineers around 1960

● Adobe used them in computer graphics in the 1980s

Page 102: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

102

4 Point Bezier Curve

● Invented by French mathematicians/engineers around 1960

● Adobe used them in computer graphics in the 1980s

Page 103: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

103

4 Point Bezier Curve

● Invented by French mathematicians/engineers around 1960

● Adobe used them in computer graphics in the 1980s

Page 104: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

104

4 Point Bezier Curve

● Invented by French mathematicians/engineers around 1960

● Adobe used them in computer graphics in the 1980s

Page 105: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

105

4 Point Bezier Curve

● Invented by French mathematicians/engineers around 1960

● Adobe used them in computer graphics in the 1980s

Page 106: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

106

c.moveTo(10,90);c.bezierCurveTo(20,20,90,20,100,90);c.stroke();

(20,20) (90,20)

(100,90)(10,90)

Page 107: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

107

c.moveTo(10,90);c.bezierCurveTo(20,20,90,20,100,90);c.stroke();

(20,20) (90,20)

(100,90)(10,90)

Page 108: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

108

c.moveTo(10,90);c.bezierCurveTo(20,20,90,20,100,90);c.stroke();

(20,20) (90,20)

(100,90)(10,90)

Page 109: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

109

c.moveTo(10,90);c.bezierCurveTo(20,20,90,20,100,90);c.stroke();

(20,20) (90,20)

(100,90)(10,90)

Page 110: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

110

c.moveTo(10,90);c.bezierCurveTo(20,20,90,20,100,90);c.stroke();

(20,20) (90,20)

(100,90)(10,90)

Page 111: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

111

c.moveTo(10,90);c.bezierCurveTo(20,20,90,20,100,90);c.stroke();

(20,20) (90,20)

(100,90)(10,90)

Page 112: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

112

c.moveTo(10,90);c.bezierCurveTo(20,20,90,20,100,90);c.stroke();

(20,20) (90,20)

(100,90)(10,90)

Page 113: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

113ADVANCED

Bezier Curves

Ideas

c.beginPath();c.moveTo(10,10);c.bezierCurveTo(10,50,90,50,90,90);c.stroke();

cmd - op⌘ tion alt - C⌥ctrl - shift - J⇧F12

Page 114: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

114

Filled Shapes

Page 115: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

115

c.beginPath();c.moveTo(5,5);c.lineTo(90,5);c.lineTo(90,90);c.lineTo(5,90);c.lineTo(5,5);

c.stroke();

Drawing Shapes

Page 116: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

116

c.beginPath();c.moveTo(5,5);c.lineTo(90,5);c.lineTo(90,90);c.lineTo(5,90);c.lineTo(5,5);

c.stroke();

Drawing Shapes

Page 117: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

117

c.beginPath();c.moveTo(5,5);c.lineTo(90,5);c.lineTo(90,90);c.lineTo(5,90);c.lineTo(5,5);

c.stroke();

Drawing Shapes

Page 118: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

118

c.beginPath();c.moveTo(5,5);c.lineTo(90,5);c.lineTo(90,90);c.lineTo(5,90);c.lineTo(5,5);

c.stroke();

Drawing Shapes

Page 119: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

119

c.beginPath();c.moveTo(5,5);c.lineTo(90,5);c.lineTo(90,90);c.lineTo(5,90);c.lineTo(5,5);

c.stroke();

Drawing Shapes

Page 120: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

120

c.beginPath();c.moveTo(5,5);c.lineTo(90,5);c.lineTo(90,90);c.lineTo(5,90);c.lineTo(5,5);

c.stroke();

Drawing Shapes

Page 121: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

121

c.beginPath();c.moveTo(5,5);c.lineTo(90,5);c.lineTo(90,90);c.lineTo(5,90);c.lineTo(5,5);

c.stroke();

Drawing Shapes

Page 122: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

122

c.beginPath();c.moveTo(5,5);c.lineTo(90,5);c.lineTo(90,90);c.lineTo(5,90);c.lineTo(5,5);

c.stroke();

Fills

Page 123: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

123

c.beginPath();c.moveTo(5,5);c.lineTo(90,5);c.lineTo(90,90);c.lineTo(5,90);c.lineTo(5,5);

c.stroke();

Fills

c.fill();

Page 124: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

124

c.beginPath();c.moveTo(5,5);c.lineTo(90,5);c.lineTo(90,90);c.lineTo(5,90);c.lineTo(5,5);

c.stroke();

Fills

c.fill();

Page 125: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

125

c.beginPath();c.moveTo(5,5);c.lineTo(90,5);c.lineTo(90,90);c.lineTo(5,90);c.lineTo(5,5);

c.fill();

Colored Fills

Page 126: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

126

c.beginPath();c.moveTo(5,5);c.lineTo(90,5);c.lineTo(90,90);c.lineTo(5,90);c.lineTo(5,5);

c.fill();

Colored Fills

c.fillStyle = 'red';

Page 127: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

127

c.beginPath();c.moveTo(5,5);c.lineTo(90,5);c.lineTo(90,90);c.lineTo(5,90);c.lineTo(5,5);

c.fill();

Colored Fills

c.fillStyle = 'red';

Page 128: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

128

Filled Shapes

Ideas

c.beginPath();c.moveTo(20,20);c.lineTo(90,20);c.lineTo(90,90);c.fillStyle='red';c.fill();

cmd - op⌘ tion alt - C⌥ctrl - shift - J⇧F12

blackbluebrowngreenorangepurpleredwhiteyellowyellowpinkgray

Page 129: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

129

Text

Will you go to thedance with me?

Page 130: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

130

Writing Things

c.fillText('Hello', 20, 100);

Hello

Page 131: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

131

Writing Things

c.fillText('Hello', 20, 100);

Hello

Page 132: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

132

Writing Things

c.fillText('Hello', 20, 100);

Hello

Page 133: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

133

Writing Things

c.fillText('Hello', 20, 100);

Hello

Page 134: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

134

Writing Things

c.font = '20px';c.fillText('Hello', 20, 100);

Hello

Page 135: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

135

Writing Things

c.font = '20px';c.fillText('Hello', 20, 100);

Hello

Page 136: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

136

Writing Things

c.font = '20px';c.fillText('Hello', 20, 100);

Hello

Page 137: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

137

Writing Things

c.font = '20px serif';c.fillText('Hello', 20, 100);

Hello

Page 138: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

138

Writing Things

c.font = '20px serif';c.fillText('Hello', 20, 100);

Hello

Page 139: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

139ADVANCED

Moon

Text

Ideas

c.fillStyle='blue';c.font = '20px sans-serif';c.fillText('Hello', 20, 100);

cmd - op⌘ tion alt - C⌥ctrl - shift - J⇧F12

Hello

Red Moon

Page 140: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

140

Drawing is Hard

Page 141: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

141

Images

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,100,50);

});

Page 142: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

142

Images

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,100,50);

});

Page 143: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

143

Images

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,100,50);

});

Page 144: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

144

Images

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,100,50);

});

Page 145: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

145

Images

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,100,50);

});

Page 146: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

146

Images

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,100,50);

});

Page 147: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

147

Images

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,100,50);

});

Page 148: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

148

Images

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,100,50);

});

Page 149: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

149

Images

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,100,50);

});

Page 150: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

150

Images

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,100,50);

});

Page 151: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

151

Changing Images

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,100,50);

});

Page 152: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

152

Changing Images

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,100,50);

});

Page 153: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

153

Changing Images

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,100,50);

});

http://o.ooli.ca/ele.png

Page 154: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

154

Changing Images

a = new Image();a.src = 'http://o.ooli.ca/ele.png';

wait(function() {

c.drawImage(a,100,50);

});

Page 155: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

155

Placing Images

a = new Image();a.src = 'http://o.ooli.ca/ele.png';

wait(function() {

c.drawImage(a,100,50);

});

Page 156: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

156

Placing Images

a = new Image();a.src = 'http://o.ooli.ca/ele.png';

wait(function() {

c.drawImage(a,100,50);

});

Page 157: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

157

Placing Images

a = new Image();a.src = 'http://o.ooli.ca/ele.png';

wait(function() {

c.drawImage(a,100,50);

});

100

Page 158: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

158

Placing Images

a = new Image();a.src = 'http://o.ooli.ca/ele.png';

wait(function() {

c.drawImage(a,100,50);

});

100

50

Page 159: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

159

Placing Images

a = new Image();a.src = 'http://o.ooli.ca/ele.png';

wait(function() {

c.drawImage(a,100,90);

});

100

50

Page 160: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

160

Placing Images

a = new Image();a.src = 'http://o.ooli.ca/ele.png';

wait(function() {

c.drawImage(a,100,90);

});

100

90

Page 161: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

161

Two Images

a = new Image();a.src = 'http://o.ooli.ca/ele.png';

wait(function() {

c.drawImage(a,100,90);

});

Page 162: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

162

Two Images

a = new Image();a.src = 'http://o.ooli.ca/ele.png';

wait(function() {

c.drawImage(a,100,90);

});

Page 163: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

163

Two Images

a = new Image();a.src = 'http://o.ooli.ca/ele.png';b = new Image();b.src = 'http://o.ooli.ca/mona.jpg';wait(function() {

c.drawImage(a,100,90);

});

Page 164: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

164

Two Images

a = new Image();a.src = 'http://o.ooli.ca/ele.png';b = new Image();b.src = 'http://o.ooli.ca/mona.jpg';wait(function() {

c.drawImage(a,100,90);

});

Page 165: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

165

Two Images

a = new Image();a.src = 'http://o.ooli.ca/ele.png';b = new Image();b.src = 'http://o.ooli.ca/mona.jpg';wait(function() {

c.drawImage(a,100,90);c.drawImage(b,10,10);

});

Page 166: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

166

Two Images

a = new Image();a.src = 'http://o.ooli.ca/ele.png';b = new Image();b.src = 'http://o.ooli.ca/mona.jpg';wait(function() {

c.drawImage(a,100,90);c.drawImage(b,10,10);

});

Page 167: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

167

Click on Run If It Gets Stuck

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,100,50);

});

Page 168: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

168cmd - op⌘ tion alt - C⌥ctrl - shift - J⇧F12

Program

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,100,50);

});

ele.png

Images

person.png

car_top.pngdog.png

mona.jpg

http://o.ooli.ca/

http://o.ooli.ca/

tree.png

leaves.png

grass.png

brick.png

stone.png

Page 169: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

169

Where to Get Images

● Find an image● Right-click (ctrl-click)● Copy

– Copy Image URL

– Copy Image Location

– Copy Image Address

– Properties...Address

● Paste it

● Or upload your own images!

Page 170: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

170cmd - op⌘ tion alt - C⌥ctrl - shift - J⇧F12

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,100,50);

});

Ideas http://o.ooli.ca/ele.png http://o.ooli.ca/dog.png http://o.ooli.ca/person.png http://o.ooli.ca/car_top.png http://o.ooli.ca/mona.jpg http://o.ooli.ca/tree.png http://o.ooli.ca/leaves.png http://o.ooli.ca/stone.png http://o.ooli.ca/brick.png http://o.ooli.ca/grass.png

● Find an image● Right-click (ctrl-click)● Copy

● Copy Image URL● Copy Image Location● Copy Image Address● Properties...Address

● Paste it

Page 171: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

171

Animation Using HTML5 Canvas

● Go to http://o.ooli.ca/canvasjsbin● Presentation copy http://

?

Page 172: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

172

ADVANCED

Overview

● Main Topics– Images

– Basic Animation

– Keyboard Control

● Optional– Affine Transforms

– Advanced Animation

→←

Page 173: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

173

jsbinhttp://o.ooli.ca/canvasjsbin

Page 174: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

174

jsbinhttp://o.ooli.ca/canvasjsbin

<!DOCTYPE html><canvas id="canvas" width="500" height="500"></canvas><script> canvas = document.getElementById('canvas'); c = canvas.getContext('2d'); c.clear = function() {this.clearRect(0,0,500,500);}; function wait(fn) {window.setTimeout(fn, 250); } function repeat(fn) { if (window.requestAnimationFrame) { var advance = function() {fn(); requestAnimationFrame(advance);}; requestAnimationFrame(advance); } else window.setInterval(fn, 50); } var dx = 0, dy = 0, mousex = 0, mousey=0, mouseclicks = 0; document.onkeydown = function(e) { var key = e.keyCode; if (key == 37) dx=-1; else if (key == 38) dy=-1; else if (key == 39) dx=1; else if (key == 40) dy=1; else return true; return false; }; document.onkeyup = function(e) { var key = e.keyCode; if (key == 37 || key == 39) dx=0; else if (key == 38 || key == 40) dy=0;else return true; return false; }; canvas.onmousemove = function(e) { var rect = canvas.getBoundingClientRect(); mousex = e.clientX - rect.left; mousey = e.clientY - rect.top; }; canvas.onmousedown = function(e) {mouseclicks++;};</script>

Page 175: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

175

Images

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,100,50);

});

Page 176: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

176

Images

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,100,50);

});

Page 177: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

177

Images

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,100,50);

});

Page 178: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

178

Images

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,100,50);

});

Page 179: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

179

Images

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,100,50);

});

Page 180: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

180

Images

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,100,50);

});

Page 181: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

181

Images

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,100,50);

});

Page 182: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

182

Images

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,100,50);

});

Page 183: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

183

Images

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,100,50);

});

Page 184: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

184

Images

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,100,50);

});

Page 185: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

185

Changing Images

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,100,50);

});

Page 186: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

186

Changing Images

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,100,50);

});

Page 187: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

187

Changing Images

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,100,50);

});

http://o.ooli.ca/ele.png

Page 188: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

188

Changing Images

a = new Image();a.src = 'http://o.ooli.ca/ele.png';

wait(function() {

c.drawImage(a,100,50);

});

Page 189: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

189

Where to Get Images

● Find an image● Right-click (ctrl-click)● Copy

– Copy Image URL

– Copy Image Location

– Copy Image Address

– Properties...Address

● Paste it

● Or upload your own images!

Page 190: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

190cmd - op⌘ tion alt - C⌥ctrl - shift - J⇧F12

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,100,50);

});

Ideas http://o.ooli.ca/ele.png http://o.ooli.ca/dog.png http://o.ooli.ca/person.png http://o.ooli.ca/car_top.png http://o.ooli.ca/mona.jpg http://o.ooli.ca/tree.png http://o.ooli.ca/leaves.png http://o.ooli.ca/stone.png http://o.ooli.ca/brick.png http://o.ooli.ca/grass.png

● Find an image● Right-click (ctrl-click)● Copy

● Copy Image URL● Copy Image Location● Copy Image Address● Properties...Address

● Paste it

Page 191: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

191

Affine Transformations

● What are they?– Graphics effects for distorting images

– Mostly useful for 3d animation● Due to useful mathematical properties

Page 192: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

192

Affine Transformation Example

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.drawImage(a,10,10);

});

Page 193: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

193

Affine Transformation Example

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.scale(3, 1);c.drawImage(a,10,10);

});

Page 194: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

194

scale

translate

rotate

shear (not available in canvas)

Four Distortions

Page 195: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

195

Scale

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.scale(1, 1);c.drawImage(a,10,10);

});

Page 196: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

196

Scale

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.scale(1, 2);c.drawImage(a,10,10);

});

Page 197: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

197

Scale

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.scale(2, 1);c.drawImage(a,10,10);

});

Page 198: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

198

Scale

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.scale(3, 1);c.drawImage(a,10,10);

});

Page 199: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

199

Scale

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.scale(0.5, 1);c.drawImage(a,10,10);

});

Page 200: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

200

Scale

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.scale(0.1, 1);c.drawImage(a,10,10);

});

Page 201: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

201

Scale

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.scale(-1, 1);c.drawImage(a,10,10);

});

Page 202: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

202

Scale

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.scale(-1, 1);c.drawImage(a,100,10);

});

Page 203: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

203

Translate

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.translate(0, 0);c.drawImage(a,10,10);

});

Page 204: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

204

Translate

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.translate(300, 0);c.drawImage(a,10,10);

});

Page 205: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

205

Translate

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.translate(300, 50);c.drawImage(a,10,10);

});

Page 206: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

206

Rotate

a = new Image();a.src = 'http://o.ooli.ca/mona.jpg';

wait(function() {

c.rotate(30 * Math.PI / 180);c.drawImage(a,10,10);

});

Page 207: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

207

Combining Affine Transforms

● More than one transformation?– What happens?

c.translate(100, 0);c.scale(1, 2);c.rotate(20 * Math.PI / 180);c.translate(-5, 0);c.drawImage(a,10,10);

Page 208: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

208

Combining Affine Transforms

● Do the steps backwards!

c.translate(100, 0);c.scale(1, 2);c.rotate(20 * Math.PI / 180);c.translate(-5, 0);c.drawImage(a,10,10);

Page 209: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

209

Combining Affine Transformsc.translate(100, 0);c.scale(1, 2);c.rotate(20 * Math.PI / 180);c.translate(-5, 0);c.drawImage(a,10,10);

Page 210: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

210

Combining Affine Transformsc.translate(100, 0);c.scale(1, 2);c.rotate(20 * Math.PI / 180);c.translate(-5, 0);c.drawImage(a,10,10);

Page 211: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

211

Combining Affine Transformsc.translate(100, 0);c.scale(1, 2);c.rotate(20 * Math.PI / 180);c.translate(-5, 0);c.drawImage(a,10,10);

Page 212: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

212

Combining Affine Transformsc.translate(100, 0);c.scale(1, 2);c.rotate(20 * Math.PI / 180);c.translate(-5, 0);c.drawImage(a,10,10);

Page 213: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

213

Combining Affine Transformsc.translate(100, 0);c.scale(1, 2);c.rotate(20 * Math.PI / 180);c.translate(-5, 0);c.drawImage(a,10,10);

Page 214: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

214

Overhead Car

a = new Image();a.src = 'http://o.ooli.ca/car_top.png';

wait(function() {

c.drawImage(a, 0, 0);

});

Page 215: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

215

Overhead Car

a = new Image();a.src = 'http://o.ooli.ca/car_top.png';

wait(function() {

c.drawImage(a, 0, 0);

});Move the car

over here,then rotate it

Page 216: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

216

Overhead Car

a = new Image();a.src = 'http://o.ooli.ca/car_top.png';

wait(function() {

c.translate(200, 200);c.rotate(30 * Math.PI / 180);c.translate(-37, -19);c.drawImage(a, 0, 0);

// Resetc.setTransform(1,0,0,1,0,0);

});

Page 217: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

217

Overhead Car

a = new Image();a.src = 'http://o.ooli.ca/car_top.png';

wait(function() {

c.translate(200, 200);c.rotate(30 * Math.PI / 180);c.translate(-37, -19);c.drawImage(a, 0, 0);

// Resetc.setTransform(1,0,0,1,0,0);

});

Page 218: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

218

Overhead Car

c.translate(200, 200);c.rotate(30 * Math.PI / 180);c.translate(-37, -19);c.drawImage(a, 0, 0);

Page 219: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

219

Overhead Car

c.translate(200, 200);c.rotate(30 * Math.PI / 180);c.translate(-37, -19);c.drawImage(a, 0, 0);

Page 220: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

220

Overhead Car

c.translate(200, 200);c.rotate(30 * Math.PI / 180);c.translate(-37, -19);c.drawImage(a, 0, 0);

Page 221: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

221

Overhead Car

c.translate(200, 200);c.rotate(30 * Math.PI / 180);c.translate(-37, -19);c.drawImage(a, 0, 0);

Page 222: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

222

Overhead Car

c.translate(200, 200);c.rotate(30 * Math.PI / 180);c.translate(-37, -19);c.drawImage(a, 0, 0);

Move the carover here,

then rotate it

Page 223: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

223ADVANCED

cmd - op⌘ tion alt - C⌥ctrl - shift - J⇧F12

a = new Image();a.src = 'http://o.ooli.ca/car_top.png';

wait(function() {

c.translate(200, 200);c.rotate(30 * Math.PI / 180);c.translate(-37, -19);c.scale(1, 1);c.drawImage(a, 0, 0);

// Resetc.setTransform(1,0,0,1,0,0);

});

http://o.ooli.ca/ … ele.png dog.png person.png car_top.png mona.jpg tree.png leaves.png stone.png brick.png grass.png

● Find an image● Right-click (ctrl-click)● Copy

● Copy Image URL● Copy Image Location● Copy Image Address● Properties...Address

● Paste it

● Stretch an image● Mirror or flip an image● Rotate an image its lower-right point

Page 224: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

224

Animation: Moving Things

Page 225: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

225

Animation: Moving Things

Page 226: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

226

Images Review

a = new Image();a.src = 'http://o.ooli.ca/person.png';

wait(function() {

c.drawImage(a,50,50);

});

Page 227: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

227

Images Review

a = new Image();a.src = 'http://o.ooli.ca/person.png';

wait(function() {

c.drawImage(a,50,50);

});

Page 228: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

228

Images Review

a = new Image();a.src = 'http://o.ooli.ca/person.png';

wait(function() {

c.drawImage(a,50,50);

});

Page 229: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

229

Animation

a = new Image();a.src = 'http://o.ooli.ca/person.png';

wait(function() {

c.drawImage(a,50,50);

});

Page 230: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

230

Animation

a = new Image();a.src = 'http://o.ooli.ca/person.png';

wait(function() {

c.drawImage(a,50,50);

});

Page 231: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

231

Animation

a = new Image();a.src = 'http://o.ooli.ca/person.png';

repeat(function() {

c.drawImage(a,50,50);

});

Page 232: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

232

Animation

a = new Image();a.src = 'http://o.ooli.ca/person.png';

repeat(function() {

c.drawImage(a,50,50);

});

Page 233: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

233

Animation: Variables

a = new Image();a.src = 'http://o.ooli.ca/person.png';

repeat(function() {

c.drawImage(a,50,50);

});

Page 234: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

234

Animation: Variables

a = new Image();a.src = 'http://o.ooli.ca/person.png';

repeat(function() {

c.drawImage(a,50,50);

});

Page 235: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

235

Animation: Variables

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.drawImage(a, x,50);

});

Page 236: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

236

Animation: Changing Things

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.drawImage(a, x,50);x = x + 1;

});

Page 237: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

237

Animation: Changing Things

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.drawImage(a, x,50);x = x + 1;

});

Page 238: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

238

Animation: Changing Things

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.clear();c.drawImage(a, x,50);x = x + 1;

});

Page 239: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

239

Animation: Changing Things

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.clear();c.drawImage(a, x,50);x = x + 1;

});

Page 240: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

240

Animation: Changing Things

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.clear();c.drawImage(a, x,50);x = x + 1;

});

Page 241: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

241

Animation: Changing Things

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.clear();c.drawImage(a, x,50);x = x + 1;

});

Page 242: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

242

Animation: Changing Things

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.clear();c.drawImage(a, x,50);x = x + 1;

});

Page 243: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

243

Animation: Changing Things

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.clear();c.drawImage(a, x,50);x = x + 1;

});

Page 244: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

244

Animation: Changing Things

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.clear();c.drawImage(a, x,50);x = x + 1;

});

Page 245: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

245cmd - op⌘ tion alt - C⌥ctrl - shift - J⇧F12

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.clear();c.drawImage(a, x, 50);x = x + 1;

});

Ideashttp://o.ooli.ca/ … ele.png dog.png person.png car_top.png mona.jpg tree.png leaves.png stone.png brick.png grass.png

● Find an image● Right-click (ctrl-click)● Copy

● Copy Image URL● Copy Image Location● Copy Image Address● Properties...Address

● Paste it

Page 246: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

246

Advanced Animation

● So far ● More flexibility

Page 247: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

247

Time

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;y = 50;

repeat(function() {

c.clear();c.drawImage(a, x, y);x = x + 1;

});

Page 248: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

248

Time

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;y = 50;

repeat(function() {

c.clear();c.drawImage(a, x, y);x = x + 1;

});

Page 249: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

249

Time

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;y = 50;time = 1;repeat(function() {

c.clear();c.drawImage(a, x, y);x = x + 1;

});

Page 250: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

250

Time

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;y = 50;time = 1;repeat(function() {

c.clear();c.drawImage(a, x, y);time = time + 1;x = x + 1;

});

Page 251: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

251

What Time Is It?

...repeat(function() {

c.clear();c.drawImage(a, x, y);time = time + 1;x = x + 1;

});

Move right

Page 252: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

252

What Time Is It?

...repeat(function() {

c.clear();c.drawImage(a, x, y);time = time + 1;if (time > 1 && time < 50) { x = x + 1;}

});

time=50

Page 253: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

253

What Time Is It?

...repeat(function() {

c.clear();c.drawImage(a, x, y);time = time + 1;if (time > 1 && time < 50) { x = x + 1;}

});

When time is between 1 and 50,

move right

time=50

Page 254: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

254

What Time Is It?

...repeat(function() {

c.clear();c.drawImage(a, x, y);time = time + 1;if (time > 1 && time < 50) { x = x + 1;}if (time > 50 && time < 100) { x = x + 1; y = y + 1;}});

time=0 time=50

time=100

Page 255: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

255

What Time Is It?

...repeat(function() {

c.clear();c.drawImage(a, x, y);time = time + 1;if (time > 1 && time < 50) { x = x + 1;}if (time > 50 && time < 100) { x = x + 1; y = y + 1;}}); When time is

between 50 and 100,

move right and down

time=0 time=50

time=100

Page 256: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

256ADVANCED

cmd - op⌘ tion alt - C⌥ctrl - shift - J⇧F12

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;y = 50;time = 1;repeat(function() {

c.clear();c.drawImage(a, x, y);time = time + 1;if (time > 1 && time < 50) { x = x + 1;}if (time > 50 && time < 100) { x = x + 1; y = y + 1;}});

http://o.ooli.ca/ … ele.png dog.png person.png car_top.png mona.jpg tree.png leaves.png stone.png brick.png grass.png

● Find an image● Right-click (ctrl-click)● Copy

● Copy Image URL● Copy Image Location● Copy Image Address● Properties...Address

● Paste it

Page 257: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

257

Using the Keyboard

→←

Page 258: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

258

Animation Review

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.clear();c.drawImage(a, x,50);x = x + 1;

});

Page 259: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

259

Animation Review

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.clear();c.drawImage(a, x,50);x = x + 1;

});

Page 260: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

260

Animation Review

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.clear();c.drawImage(a, x,50);x = x + 1;

});

Page 261: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

261

Animation Review

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.clear();c.drawImage(a, x,50);x = x + 1;

});

Page 262: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

262

Animation Review

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.clear();c.drawImage(a, x,50);x = x + 1;

});

Page 263: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

263

Animation Review

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.clear();c.drawImage(a, x,50);x = x + 1;

});

Page 264: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

264

Animation Review

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.clear();c.drawImage(a, x,50);x = x + 1;

});

Page 265: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

265

Animation Review

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.clear();c.drawImage(a, x,50);x = x + 1;

});

Page 266: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

266

Animation Review

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.clear();c.drawImage(a, x,50);x = x + 1;

});

Page 267: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

267

Keyboard: dx

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.clear();c.drawImage(a, x,50);x = x + 1;

});

Page 268: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

268

Keyboard: dx

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.clear();c.drawImage(a, x,50);x = x + 1;

});

Page 269: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

269

Keyboard: dx

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.clear();c.drawImage(a, x,50);x = x + dx;

});

Page 270: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

270

Keyboard: dx

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.clear();c.drawImage(a, x,50);x = x + dx;

});

Page 271: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

271

Keyboard: dx

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.clear();c.drawImage(a, x,50);x = x + dx;

});

→←

Page 272: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

272

Keyboard: dx

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.clear();c.drawImage(a, x,50);x = x + dx;

});

→←Done!

Page 273: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

273

Keyboard: dx

● Special variable dx

→←

dx = 0 dx = +1dx = -1

Page 274: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

274

Keyboard: Why Does This Work?

● Remember, use (x, y) for positions90

90

(90,90)

Page 275: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

275

Keyboard: Change in Positions

● To go right, x should get bigger

● To go left, x should get smaller

(90,90)

Page 276: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

276

Keyboard: Change in Positions

● To go right, x should get bigger

● To go left, x should get smaller

(90,90) (150,90)

Page 277: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

277

Keyboard: Change in Positions

● To go right, x should get bigger

● To go left, x should get smaller

(90,90) (150,90)(30,90)

Page 278: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

278

Change in x

● To go right, x should get bigger

● To go left, x should get smaller

(90,90) (150,90)(30,90)

Page 279: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

279

Change in x

● To go right, x should get bigger

● To go left, x should get smaller

(90,90) (150,90)(30,90)

x = x + 1

Page 280: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

280

Change in x

● To go right, x should get bigger

● To go left, x should get smaller

(90,90) (150,90)(30,90)

x = x + 1

x = x - 1

Page 281: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

281

Keyboard

● To go right, x should get bigger

● To go left, x should get smaller

(90,90) (150,90)(30,90)

x = x + 1

x = x - 1

→←

dx = 0 dx = +1dx = -1

Page 282: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

282

Keyboard

● To go right, x should get bigger

● To go left, x should get smaller

(90,90) (150,90)(30,90)

→ x = x + 1

x = x - 1

→←

dx = 0 dx = +1dx = -1

Page 283: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

283

Keyboard

● To go right, x should get bigger

● To go left, x should get smaller

(90,90) (150,90)(30,90)

→ x = x + 1

x = x - 1

→←

dx = 0 dx = +1dx = -1

dx

Page 284: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

284

Keyboard

● To go right, x should get bigger

● To go left, x should get smaller

(90,90) (150,90)(30,90)

x = x + 1

x = x - 1

→←

dx = 0 dx = +1dx = -1

dx

Page 285: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

285

Keyboard

● To go right, x should get bigger

● To go left, x should get smaller

(90,90) (150,90)(30,90)

x = x + 1

x = x - 1

→←

dx = 0 dx = +1dx = -1

+ dx

dx

Page 286: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

286

Keyboard

● To go right, x should get bigger

● To go left, x should get smaller

(90,90) (150,90)(30,90)

x = x + 1

x = x - 1

→←

dx = 0 dx = +1dx = -1

+ dx

dx

x = x + dx

Page 287: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

287

Keyboard: dx

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.clear();c.drawImage(a, x,50);x = x + 1;

});

Page 288: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

288

Keyboard: dx

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.clear();c.drawImage(a, x,50);x = x + 1;

});

Page 289: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

289

Keyboard: dx

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.clear();c.drawImage(a, x,50);x = x + dx;

});

Page 290: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

290

Keyboard: dx

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.clear();c.drawImage(a, x,50);x = x + dx;

});

dx= 1

Page 291: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

291

Keyboard: dx

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.clear();c.drawImage(a, x,50);x = x + dx;

});

dx= 1

dx= -1

Page 292: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

292

Click on Canvas to Use Keyboard

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.clear();c.drawImage(a, x,50);x = x + dx;

});

Page 293: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

293cmd - op⌘ tion alt - C⌥ctrl - shift - J⇧F12

a = new Image();a.src = 'http://o.ooli.ca/person.png';x = 50;repeat(function() {

c.clear();c.drawImage(a, x,50);x = x + dx;

});

Ideashttp://o.ooli.ca/ … ele.png dog.png person.png car_top.png mona.jpg tree.png leaves.png stone.png brick.png grass.png

● Find an image● Right-click (ctrl-click)● Copy

● Copy Image URL● Copy Image Location● Copy Image Address● Properties...Address

● Paste it

Page 294: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

ADVANCED

294

Driving Car

a = new Image();a.src = 'http://o.ooli.ca/car_top.png';x = 50;y = 50;angle = 0;repeat(function() {

angle = angle + dx;x = x - dy * Math.cos(angle * Math.PI / 180);y = y - dy * Math.sin(angle * Math.PI / 180);

c.clear();c.translate(x, y);c.rotate(angle * Math.PI / 180);c.translate(-37, -19);c.drawImage(a, 0, 0);

c.setTransform(1,0,0,1,0,0); // reset

});

Page 295: Drawing Using HTML5 Canvas - Programming  · PDF file1 Drawing Using HTML5 Canvas Go to   Presentation copy http:// ?

295

Art CreditsMage City ArcanosHyptosisopengameart.org

BaurnRachel J. MorrisArtSader

Justin BieberKevin Aranibar of Kerosene PhotographyCC Attribution 2.0 Generic

Chad KroegerJames AndersonCC Attribution-Share Alike 3.0 Unported

Celine DionGeorges BiardCC Atribution-Share Alike 3.0 Unported

neo1973ryanlerchopenclipart.org

TV iconjhnri4openclipart.org

Architetto -- macchina fotograficaAnonymous (francesco rollandin)openclipart.org

Edit Hand Holding Pencildarrenbeckopenclipart.org

tango style pen(remix of tango pen)warszawianka openclipart.org

Brunette Female DriverMerlin2525openclipart.org

horse machovkaopenclipart.org

RPG man symbols: Round Towernicubunuopenclipart.org