|
| 1 | +# svg |
| 2 | + |
| 3 | +svg is an image format that browsers understand |
| 4 | + |
| 5 | +--- |
| 6 | +# `<svg>` |
| 7 | + |
| 8 | +svg looks a lot like html: |
| 9 | + |
| 10 | +``` html |
| 11 | +<svg viewbox="0 0 100 100" width="800" height="500" xmlns="http://www.w3.org/2000/svg"> |
| 12 | + <circle cx="300" cy="200" r="5" fill="purple"> |
| 13 | +</svg> |
| 14 | +``` |
| 15 | + |
| 16 | +--- |
| 17 | +# viewbox |
| 18 | + |
| 19 | +All coordinates are in terms of the viewbox: |
| 20 | + |
| 21 | +"0 0 100 100" |
| 22 | + |
| 23 | +"left top right bottom" |
| 24 | + |
| 25 | +--- |
| 26 | +# viewbox |
| 27 | + |
| 28 | +If you wanted to use a coordinate system with x and y |
| 29 | +between -1 and 1, you could do: |
| 30 | + |
| 31 | +"-1 -1 1 1" |
| 32 | + |
| 33 | +--- |
| 34 | + |
| 35 | +Most visible elements have these attributes: |
| 36 | + |
| 37 | +* `fill` |
| 38 | +* `stroke` |
| 39 | +* `stroke-width` |
| 40 | + |
| 41 | +--- |
| 42 | +# `<circle>` |
| 43 | + |
| 44 | +``` html |
| 45 | +<circle cx="300" cy="200" r="5" fill="purple"> |
| 46 | +``` |
| 47 | + |
| 48 | +* `cx` - center x coordinate |
| 49 | +* `cy` - center y coordinate |
| 50 | +* `r` - radius |
| 51 | + |
| 52 | +--- |
| 53 | +# `<rect>` |
| 54 | + |
| 55 | +``` html |
| 56 | +<rect x="1" y="1" width="998" height="298" |
| 57 | + fill="none" stroke-width="2"> |
| 58 | +``` |
| 59 | + |
| 60 | +--- |
| 61 | +# `<text>` |
| 62 | + |
| 63 | +``` html |
| 64 | +<text x="250" y="150" font-family="Verdana" font-size="55"> |
| 65 | + whatever |
| 66 | +</text> |
| 67 | +``` |
| 68 | + |
| 69 | +--- |
| 70 | +# transforms |
| 71 | + |
| 72 | +* transform |
| 73 | +* scale |
| 74 | +* matrix |
| 75 | + |
| 76 | +--- |
| 77 | +# `<g>` |
| 78 | + |
| 79 | +Create a group of elements. |
| 80 | + |
| 81 | +You can transform all the child nodes of a g by setting a |
| 82 | +transform on a `<g>` element. |
| 83 | + |
| 84 | +--- |
| 85 | +# `<path>` |
| 86 | + |
| 87 | +create a shape from a path string |
| 88 | + |
| 89 | +``` html |
| 90 | +``` |
| 91 | + |
| 92 | +--- |
| 93 | +# path string syntax |
| 94 | + |
| 95 | +* `M x,y` - move to (absolute) |
| 96 | +* `m dx,dy` - move to (relative) |
| 97 | + |
| 98 | +* `L x,y` - line to (absolute) |
| 99 | +* `l dx,dy` - line to (relative) |
| 100 | + |
| 101 | +* `C c1x,c1y c2x,c2y x,y` - curve to (absolute) |
| 102 | +* `c dc1x,dc1y dc2x,dc2y dx,dy` - curve to (relative) |
| 103 | + |
| 104 | +* `z` / `Z` - close path |
| 105 | + |
| 106 | +Some others: |
| 107 | + |
| 108 | +* A/a - elliptical arc |
| 109 | +* T/t - quadratic bezier curve |
| 110 | +* S/s - cubic bezier curve |
| 111 | + |
| 112 | +--- |
| 113 | +# `<polygon>` |
| 114 | + |
| 115 | +create a closed shape from line segments |
| 116 | + |
| 117 | +``` html |
| 118 | +``` |
| 119 | + |
| 120 | +--- |
| 121 | +# `<polyline>` |
| 122 | + |
| 123 | +create an open shape from line segments |
| 124 | + |
| 125 | +``` html |
| 126 | +<polygon points="60,20 100,40 100,80 60,100 20,80 20,40"> |
| 127 | +``` |
| 128 | + |
| 129 | +--- |
| 130 | + |
| 131 | +You can also dynamically construct elements with javascript! |
| 132 | + |
| 133 | +--- |
| 134 | +# document.createElementNS() |
| 135 | + |
| 136 | +Constructing elements is a bit weird: |
| 137 | + |
| 138 | +``` js |
| 139 | +var svg = document.createElementNS('svg', 'http://www.w3.org/2000/svg'); |
| 140 | +var circle = document.createElementNS('circle', 'http://www.w3.org/2000/svg'); |
| 141 | +``` |
| 142 | + |
| 143 | +--- |
| 144 | +# svg dom methods |
| 145 | + |
| 146 | +All of the usual dom methods work on svg: |
| 147 | + |
| 148 | +``` js |
| 149 | +var circle = document.createElementNS('circle', 'http://www.w3.org/2000/svg'); |
| 150 | +circle.setAttribute('fill', 'cyan'); |
| 151 | + |
| 152 | +var circle2 = elem.cloneNode(true); |
| 153 | +circle2.style.backgroundColor = 'orange'; |
| 154 | +``` |
| 155 | + |
| 156 | +--- |
| 157 | +# svg dom methods |
| 158 | + |
| 159 | +some useful methods: |
| 160 | + |
| 161 | +* `elem.cloneNode(true)` - copy a node and its children |
| 162 | +* `elem.setAttribute(name, value)` - set an attribute |
| 163 | + |
| 164 | +--- |
| 165 | +# svg-create-element |
| 166 | + |
| 167 | +with the `svg-create-element` module, we can create svg |
| 168 | +attributes more tersely with less remembering w3 urls: |
| 169 | + |
| 170 | +``` js |
| 171 | +var createElement = require('svg-create-element'); |
| 172 | +var svg = createElement('svg'); |
| 173 | +var circle = createElement('circle'); |
| 174 | +``` |
| 175 | + |
| 176 | +--- |
| 177 | +# svg-create-element |
| 178 | + |
| 179 | +You can also create attributes on created elements in one |
| 180 | +stroke with svg-create-element: |
| 181 | + |
| 182 | +``` js |
| 183 | +var createElement = require('svg-create-element'); |
| 184 | +var svg = createElement('svg', { |
| 185 | + viewbox: '0 0 100 100', width: 500, height: 300 |
| 186 | +}); |
| 187 | +var circle = createElement('circle', { |
| 188 | + cx: 250, cy: 150, r: 80, fill: 'purple' |
| 189 | +}); |
| 190 | +svg.appendChild(circle); |
| 191 | +document.body.appendChild(svg); |
| 192 | +``` |
| 193 | + |
| 194 | +--- |
| 195 | +# |
| 196 | + |
| 197 | +--- |
| 198 | +# inserting svgs into the page |
| 199 | + |
| 200 | +Inlining svg into your html is annoying if you create the |
| 201 | +svgs in an image program. |
| 202 | + |
| 203 | +This works to display an image: |
| 204 | + |
| 205 | +``` html |
| 206 | +<img src="cats.svg"> |
| 207 | +``` |
| 208 | + |
| 209 | +but you won't be able to manipulate the svg with javascript. |
| 210 | + |
| 211 | +--- |
| 212 | +# loading an svg |
| 213 | + |
| 214 | +Instead, we can use xhr to load an svg image, putting the |
| 215 | +contents into a constructed `<div>` to pull out the `<svg>` |
| 216 | +element: |
| 217 | + |
| 218 | +``` js |
| 219 | +var xhr = require('xhr'); |
| 220 | + |
| 221 | +xhr('cats.svg', function (err, res, body) { |
| 222 | + if (err) return console.error(err); |
| 223 | + if (/^2/.test(res.statusCode)) { |
| 224 | + return console.error(res.statusCode + ': ' + body); |
| 225 | + } |
| 226 | + var div = document.createElement('div'); |
| 227 | + div.innerHTML = body; |
| 228 | + var svg = div.querySelector('svg'); |
| 229 | + if (!svg) return console.error('no svg found'); |
| 230 | + document.body.appendChild(svg); |
| 231 | +}); |
| 232 | +``` |
| 233 | + |
| 234 | +--- |
| 235 | +# load-svg |
| 236 | + |
| 237 | +or just use the `load-svg` package instead: |
| 238 | + |
| 239 | +``` js |
| 240 | +var loadsvg = require('load-svg'); |
| 241 | + |
| 242 | +loadsvg('cool.svg', function (err, svg) { |
| 243 | + document.body.appendChild(svg); |
| 244 | +}); |
| 245 | +``` |
| 246 | + |
| 247 | +--- |
| 248 | +# window.requestAnimationFrame() |
| 249 | + |
| 250 | +We can make animations using |
| 251 | +`window.requestAnimationFrame()`: |
| 252 | + |
| 253 | +``` js |
| 254 | +window.requestAnimationFrame(loop); |
| 255 | + |
| 256 | +function loop () { |
| 257 | + // update the scene here |
| 258 | +} |
| 259 | +``` |
| 260 | + |
| 261 | +--- |
| 262 | +# time delta |
| 263 | + |
| 264 | +Not all computers are capable of running a simulation at 60 |
| 265 | +fps. By computing a time delta, you can make your simulation |
| 266 | +work even on systems that can't keep up at 60 fps. |
| 267 | + |
| 268 | +--- |
| 269 | +# time delta |
| 270 | + |
| 271 | +``` js |
| 272 | +var last = Date.now(); |
| 273 | +window.requestAnimationFrame(loop); |
| 274 | + |
| 275 | +function loop () { |
| 276 | + var now = Date.now(); |
| 277 | + var dt = last - now; |
| 278 | + last = now; |
| 279 | + |
| 280 | + // update the scene here based on dt |
| 281 | +} |
| 282 | +``` |
| 283 | + |
| 284 | +--- |
| 285 | +# frame-loop |
| 286 | + |
| 287 | +``` |
| 288 | +``` |
| 289 | + |
| 290 | +--- |
| 291 | +# box-sprite-svg |
| 292 | + |
| 293 | +--- |
| 294 | +# box-collide |
| 295 | + |
0 commit comments