|
| 1 | +--- |
| 2 | + layout: post |
| 3 | + title: "Learning the Canvas: Part 1" |
| 4 | + tags: |
| 5 | + - javascript |
| 6 | + - canvas |
| 7 | + categories: |
| 8 | +--- |
| 9 | + |
| 10 | +First we start by creating the canvas element, with an ID, width and height |
| 11 | +attributes, and a border so it's easier to see. |
| 12 | + |
| 13 | +{% highlight html %} |
| 14 | +<div> |
| 15 | + <canvas id="canvas" width="200" height="200" style="border: 1px solid black"> |
| 16 | + </canvas> |
| 17 | +</div> |
| 18 | +{% endhighlight %} |
| 19 | + |
| 20 | +Then we add the JavaScript code to draw a simple shape onto the canvas. We'll |
| 21 | +choose (0,0) as the starting point and stretch it out half the width and half |
| 22 | +the height of the canvas. |
| 23 | + |
| 24 | +{% highlight html %} |
| 25 | +<script> |
| 26 | + var canvas = document.getElementById("canvas"); |
| 27 | + var context = canvas.getContext("2d"); |
| 28 | + context.fillStyle = "rgb(0, 0, 125)"; |
| 29 | + context.fillRect(0, 0, 100, 100); |
| 30 | +</script> |
| 31 | +{% endhighlight %} |
| 32 | + |
| 33 | +The result is a square that occupies one quarter of the total area of the |
| 34 | +canvas. |
| 35 | + |
| 36 | +<div> |
| 37 | + <canvas id="canvas" width="200" height="200" style="border: 1px solid black"> |
| 38 | + </canvas> |
| 39 | +</div> |
| 40 | + |
| 41 | +<script> |
| 42 | + var canvas = document.getElementById("canvas"); |
| 43 | + var context = canvas.getContext("2d"); |
| 44 | + context.fillStyle = "rgb(0, 0, 125)"; |
| 45 | + context.fillRect(0, 0, 100, 100); |
| 46 | +</script> |
| 47 | + |
| 48 | +<br/> |
| 49 | + |
| 50 | +Next we can extend the same idea by drawing more shapes onto the canvas to form |
| 51 | +a pattern. |
| 52 | + |
| 53 | +{% highlight html %} |
| 54 | +<script> |
| 55 | + var canvas = document.getElementById("canvas"); |
| 56 | + var context = canvas.getContext("2d"); |
| 57 | + context.fillStyle = "rgb(0, 0, 125)"; |
| 58 | + context.fillRect(0, 0, 100, 100); |
| 59 | + context.fillStyle = "rgb(125, 0, 0)"; |
| 60 | + context.fillRect(0, 100, 100, 200); |
| 61 | + context.fillStyle = "rgb(125, 0, 0)"; |
| 62 | + context.fillRect(100, 0, 200, 100); |
| 63 | + context.fillStyle = "rgb(0, 0, 125)"; |
| 64 | + context.fillRect(100, 100, 200, 200); |
| 65 | +</script> |
| 66 | +{% endhighlight %} |
| 67 | + |
| 68 | +<div> |
| 69 | + <canvas id="canvas-2" width="200" height="200" style="border: 1px solid black"> |
| 70 | + </canvas> |
| 71 | +</div> |
| 72 | + |
| 73 | +<script> |
| 74 | + var canvas = document.getElementById("canvas-2"); |
| 75 | + var context = canvas.getContext("2d"); |
| 76 | + context.fillStyle = "rgb(0, 0, 125)"; |
| 77 | + context.fillRect(0, 0, 100, 100); |
| 78 | + context.fillStyle = "rgb(125, 0, 0)"; |
| 79 | + context.fillRect(0, 100, 100, 200); |
| 80 | + context.fillStyle = "rgb(125, 0, 0)"; |
| 81 | + context.fillRect(100, 0, 200, 100); |
| 82 | + context.fillStyle = "rgb(0, 0, 125)"; |
| 83 | + context.fillRect(100, 100, 200, 200); |
| 84 | +</script> |
| 85 | + |
| 86 | +<br/> |
| 87 | + |
| 88 | +Now this is great, but it's a lot of repetition. It would be great if we could |
| 89 | +leverage a bit of JavaScript's dynamic nature to make this easier. |
| 90 | + |
| 91 | +{% highlight html %} |
| 92 | +<script> |
| 93 | + var canvas = document.getElementById("canvas-3"); |
| 94 | + var context = canvas.getContext("2d"); |
| 95 | + for (var index = 0; index < 8; index += 1) { |
| 96 | + if (index % 2 == 0) { |
| 97 | + context.fillStyle = "rgb(255, 255, 255)"; |
| 98 | + } else { |
| 99 | + context.fillStyle = "rgb(0, 0, 0)"; |
| 100 | + } |
| 101 | + context.fillRect(0+100*index, 0, 100+100*index, 100); |
| 102 | + } |
| 103 | +</script> |
| 104 | +{% endhighlight %} |
| 105 | + |
| 106 | +<div> |
| 107 | + <canvas id="canvas-3" width="800" height="100" style="border: 1px solid black"> |
| 108 | + </canvas> |
| 109 | +</div> |
| 110 | + |
| 111 | +<script> |
| 112 | + var canvas = document.getElementById("canvas-3"); |
| 113 | + var context = canvas.getContext("2d"); |
| 114 | + for (var index = 0; index < 8; index += 1) { |
| 115 | + if (index % 2 == 0) { |
| 116 | + context.fillStyle = "rgb(255, 255, 255)"; |
| 117 | + } else { |
| 118 | + context.fillStyle = "rgb(0, 0, 0)"; |
| 119 | + } |
| 120 | + context.fillRect(0+100*index, 0, 100+100*index, 100); |
| 121 | + } |
| 122 | +</script> |
| 123 | + |
| 124 | +<br/> |
| 125 | + |
| 126 | +You can see how powerful this is and how it's starting to resemble a game board |
| 127 | +that might look familiar. |
0 commit comments