|
| 1 | +# Canvas ShaderTypes |
| 2 | + |
| 3 | +The [Canvas API](https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API) does not really have a concept called shaders, however for it to fit better into our architecture we are calling it shaders anyway. You can use the following properties to define your `CanvasShaderType`. |
| 4 | + |
| 5 | +## render |
| 6 | + |
| 7 | +The render property is the only `required` property for the CanvasShaderType. This property requires a function that draws the shader/effect. |
| 8 | + |
| 9 | +```js |
| 10 | +export const Border = { |
| 11 | + render(ctx, quad, renderContext) { |
| 12 | + //renders node context(if not called the context won't be drawn) |
| 13 | + renderContext(); |
| 14 | + |
| 15 | + ctx.strokeStyle = 'green'; |
| 16 | + ctx.lineWidth = 10; |
| 17 | + ctx.beginPath(); |
| 18 | + //draw an innerBorder |
| 19 | + ctx.strokeRect(quad.tx + 5, quad.ty + 5, quad.width - 10, quad.height - 10); |
| 20 | + }, |
| 21 | +}; |
| 22 | +``` |
| 23 | + |
| 24 | +## update |
| 25 | + |
| 26 | +The update property is an `optional` property. We can use to computed and store some values we don't want to calculate / mutate everytime a quad is drawn with canvas. |
| 27 | + |
| 28 | +```js |
| 29 | +export const Border = { |
| 30 | + props: { |
| 31 | + color: 0x00ff00ff, |
| 32 | + width: 20, |
| 33 | + }, |
| 34 | + update() { |
| 35 | + this.computed.borderColor = formatRGBAtoString(this.props.color); |
| 36 | + }, |
| 37 | + render(ctx, quad, renderContext) { |
| 38 | + //renders node context(if not called the context won't be drawn) |
| 39 | + renderContext(); |
| 40 | + const borderWidth = this.props.width; |
| 41 | + ctx.strokeStyle = this.computed.borderColor; |
| 42 | + ctx.lineWidth = borderWidth; |
| 43 | + const halfW = borderWidth * 0.5; |
| 44 | + ctx.beginPath(); |
| 45 | + //draw an innerBorder |
| 46 | + ctx.strokeRect( |
| 47 | + quad.tx + halfW, |
| 48 | + quad.ty + halfW, |
| 49 | + quad.width - borderWidth, |
| 50 | + quad.height - borderWidth, |
| 51 | + ); |
| 52 | + }, |
| 53 | +}; |
| 54 | +``` |
| 55 | + |
| 56 | +## saveAndRestore |
| 57 | + |
| 58 | +Generally when you are about to transform shape, rotation or clip with canvas methods you use `ctx.save` > apply methods > `ctx.restore`. Saving and restoring is very costly if used a lot. To reduce save and restore calls there is a property called `saveAndRestore` you can use to let the renderer know you need it to save and restore before and after this shader is executed. |
| 59 | + |
| 60 | +```js |
| 61 | +export const Rounded = { |
| 62 | + render(ctx, quad, renderContext) { |
| 63 | + const path = new Path2D(); |
| 64 | + roundRect( |
| 65 | + path, |
| 66 | + quad.tx, |
| 67 | + quad.ty, |
| 68 | + quad.width, |
| 69 | + quad.height, |
| 70 | + this.computed.radius!, |
| 71 | + ); |
| 72 | + ctx.clip(path); |
| 73 | + //renders node context(if not called the context won't be drawn) |
| 74 | + renderContext() |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +See following source to learn more about the Rounded canvas shader. |
| 80 | + |
| 81 | +You might still want to use `ctx.save` and `ctx.restore` in your render function. But this is only done when you want to more effects on top of a node. Just be careful not to overuse it. |
0 commit comments