cheatsheets/canvas.md

141 lines
2.2 KiB
Markdown
Raw Permalink Normal View History

2013-10-14 02:36:58 +00:00
---
title: Canvas
2015-11-24 05:02:17 +00:00
category: JavaScript
2013-10-14 02:36:58 +00:00
---
2012-10-11 08:13:19 +00:00
### Getting the context
2017-09-21 08:05:49 +00:00
```js
var canvas = document.getElementById('c')
var c = canvas.getContext('2d')
```
2012-10-11 08:13:19 +00:00
### Basic drawing
2017-09-21 08:05:49 +00:00
```js
// x = 10, y = 20, width = 200, height = 100
c.fillStyle = '#ff0000'
c.strokeStyle = '#ff00ff'
```
2012-11-25 04:13:51 +00:00
2017-09-21 08:05:49 +00:00
```js
c.lineWidth = 5
c.lineCap = 'round'
```
2012-11-25 04:13:51 +00:00
2017-09-21 08:05:49 +00:00
```js
c.fillRect(10, 20, 200, 100)
```
2012-11-25 04:13:51 +00:00
2017-09-21 08:05:49 +00:00
```js
c.stroke()
c.fill()
```
2012-11-25 04:13:51 +00:00
### Saving and restoring
2017-09-21 08:05:49 +00:00
```js
c.save()
```
2012-11-25 04:13:51 +00:00
2017-09-21 08:05:49 +00:00
```js
c.restore()
```
Saves: `strokeStyle` `fillStyle` `globalAlpha` `lineWidth` `lineCap` `lineJoin` `miterLimit` `shadowOffsetX` `shadowOffsetY` `shadowBlur` `shadowColor`
`globalCompositeOperation`, Transformations (`translate` `rotate` `scale` `transform` `setTransform`), Clipping path
2012-11-25 04:13:51 +00:00
2012-12-15 12:56:59 +00:00
### Animation
2017-09-21 08:05:49 +00:00
```js
onframe: function() {
c.clearRect(0, 0, w, h)
}
```
2012-12-15 12:56:59 +00:00
2012-11-25 04:13:51 +00:00
### Transformations
2017-09-21 08:05:49 +00:00
```js
c.translate(0, 0)
c.rotate(Math.PI*2/5)
c.scale(1.0, 1.0)
```
2012-11-25 04:13:51 +00:00
2012-12-15 12:56:59 +00:00
To rotate along origin:
2017-09-21 08:05:49 +00:00
```js
c.translate(ox, oy)
c.rotate(theta)
c.translate(-ox, -oy)
```
2012-12-15 12:56:59 +00:00
To scale along origin:
2017-09-21 08:05:49 +00:00
```js
c.translate(-ox*x, -oy*y)
c.scale(x, y)
c.translate(ox/x, oy/y)
```
2012-12-15 12:56:59 +00:00
2012-11-25 04:13:51 +00:00
See [MDN: Transformations][xform].
2012-10-11 08:13:19 +00:00
### Image drawing
2017-09-21 08:05:49 +00:00
```js
c.drawImage(image, dx, dy, [dw, dh]);
/* `image` can be HTML Image/Canvas/Video */
```
2012-10-11 08:13:19 +00:00
2012-11-25 04:13:51 +00:00
See [MDN: Images][images].
2012-10-11 08:13:19 +00:00
### Colors, styles shadows
2017-09-21 08:05:49 +00:00
```js
c.strokeStyle = '#ff00ff';
c.fillStyle = '#ff00ff';
```
2012-10-11 08:13:19 +00:00
2017-09-21 08:05:49 +00:00
```js
c.shadowOffsetX = 0;
c.shadowOffsetY = 0;
c.shadowOffsetBlur = 3.0;
c.shadowColor = 'rgba(0,0,0,0.2)';
```
2012-10-11 08:13:19 +00:00
2017-09-21 08:05:49 +00:00
See [MDN: Styles][styles]
2012-11-25 04:13:51 +00:00
2012-10-11 08:13:19 +00:00
### Gradients
2017-09-21 08:05:49 +00:00
```js
gr = c.createLinearGradient(x0,y0,x1,y1)
gr = c.createRadialGradient(x0,y0,r0,x1,y1,r1)
pat = c.createPattern(image, 'repeat-x')
```
2012-10-11 08:13:19 +00:00
2017-09-21 08:05:49 +00:00
```js
c.fillStyle = gr
```
2012-10-11 08:13:19 +00:00
### Drawing
2017-09-21 08:05:49 +00:00
```js
c.beginPath()
c.moveTo(x,y)
c.lineTo(x,y)
c.quadraticCurveTo(cpx,cpy,x,y)
c.bezierCurveTo(cp1x,cp1y,cp2x,cp2y)
c.arcTo(...)
c.arc(...)
c.closePath()
```
2012-10-11 08:13:19 +00:00
2012-11-25 04:13:51 +00:00
### More resources
2012-10-11 08:13:19 +00:00
2012-11-25 04:13:51 +00:00
* [Canvas Cheatsheet PDF][pdf]
2012-10-11 08:13:19 +00:00
2012-11-25 04:13:51 +00:00
[pdf]: http://www.nihilogic.dk/labs/canvas_sheet/HTML5_Canvas_Cheat_Sheet.pdf
[xform]: https://developer.mozilla.org/en-US/docs/Canvas_tutorial/Transformations
[styles]: https://developer.mozilla.org/en-US/docs/Canvas_tutorial/Applying_styles_and_colors
[images]: https://developer.mozilla.org/en-US/docs/Canvas_tutorial/Using_images