Skip to content

Commit 8c9fdf2

Browse files
committed
Support dynamic canvas element allowing it be produced by DOM driver
1 parent ef0e67d commit 8c9fdf2

File tree

5 files changed

+293
-219
lines changed

5 files changed

+293
-219
lines changed

README.md

Lines changed: 148 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,42 @@ Looks like this:
6464

6565
![img](http://i.imgur.com/1LCZxrg.png)
6666

67+
<a id="makeDynamicHostCanvasDriverExample"></a>
68+
Or in case the canvas element appears later, for example, when it's created by DOM driver:
69+
70+
```jsx
71+
import {run} from '@cycle/rxjs-run';
72+
import {makeDOMDriver} from '@cycle/dom'
73+
import {makeDynamicHostCanvasDriver, rect} from 'cycle-canvas';
74+
import {Observable} from 'rxjs'
75+
76+
function main (sources) {
77+
const hostCanvas$ = sources.DOM.select('canvas').element()
78+
const rootElement$ = Observable.of(rect({
79+
x: 10,
80+
y: 10,
81+
width: 160,
82+
height: 100,
83+
draw: [
84+
{fill: 'purple'}
85+
]
86+
}))
87+
return {
88+
DOM: Observable.of(<canvas width="200" height="200"></canvas>),
89+
Canvas: Observable.combineLatest(hostCanvas$, rootElement$).map(([hostCanvas, rootElement]) => {
90+
return {hostCanvas, rootElement}
91+
})
92+
};
93+
}
94+
95+
const drivers = {
96+
DOM: makeDOMDriver('body'),
97+
Canvas: makeDynamicHostCanvasDriver()
98+
};
99+
100+
run(main, drivers);
101+
```
102+
67103
Also check out the [flappy bird example](https://cyclejs-community.github.io/cycle-canvas/).
68104

69105
You can find the source for flappy bird [here](https://github.com/cyclejs-community/cycle-canvas/tree/master/examples/flappy-bird).
@@ -73,10 +109,7 @@ You can find the source for flappy bird [here](https://github.com/cyclejs-commun
73109
#### Creating a canvas driver
74110

75111
- [`makeCanvasDriver`](#makeCanvasDriver)
76-
77-
#### Using event streams of the canvas element
78-
79-
- [`sources.Canvas.events`](#events)
112+
- [`makeDynamicHostCanvasDriver`](#makeDynamicHostCanvasDriver)
80113

81114
#### Drawing shapes and text
82115

@@ -109,13 +142,11 @@ The input to this driver is a stream of drawing instructions and transformations
109142
- `selector: string` a css selector to use in order to find a canvas to attach the driver to.
110143
- `canvasSize: {width: integer, height: integer}` an object that denotes the size to set for the attached canvas. If null, the driver attaches to its canvas without altering its size.
111144

112-
## Using event streams of the canvas element
113-
114-
### <a id="events"></a> `sources.Canvas.events(eventName)`
145+
#### Listening to event streams of the canvas element: `sources.Canvas.events(eventName)`
115146

116-
Canvas driver exposes a source object with an `events` method, which works similarly to the `events` method of the DOM driver.
147+
Canvas driver produced by `makeCanvasDriver` function exposes a source object with an `events` method, which works similarly to the `events` method of the DOM driver.
117148

118-
#### Example:
149+
##### Example:
119150
```js
120151
import {run} from '@cycle/rxjs-run';
121152
import {makeCanvasDriver, rect, text} from 'cycle-canvas';
@@ -150,6 +181,21 @@ const drivers = {
150181
run(main, drivers);
151182
```
152183

184+
### <a id="makeDynamicHostCanvasDriver"></a> `makeDynamicHostCanvasDriver()`
185+
186+
An alternative factory for the canvas driver function.
187+
188+
Does not take any arguments, but requires events in the sink to be of this format:
189+
```js
190+
{
191+
hostCanvas
192+
rootElement
193+
}
194+
```
195+
where `hostCanvas` is a `<canvas>` DOM element and `rootElement` is the element to draw on the `hostCanvas`.
196+
197+
You can find an [example](#makeDynamicHostCanvasDriverExample) at the top.
198+
153199
## Drawing shapes and text
154200

155201
### <a id="rect"></a> `rect(params = {})`
@@ -171,24 +217,24 @@ Draws a rectangle given an object containing drawing parameters.
171217
#### Example:
172218
```js
173219
rect({
174-
x: 10,
175-
y: 10,
176-
width: 100,
177-
height: 100,
178-
draw: [
179-
{fill: 'purple'}
180-
],
181-
children: [
182-
rect({
183-
x: 20,
184-
y: 20,
185-
width: 50,
186-
height: 50,
187-
draw: [
188-
{fill: 'blue'}
189-
]
190-
})
191-
]
220+
x: 10,
221+
y: 10,
222+
width: 100,
223+
height: 100,
224+
draw: [
225+
{fill: 'purple'}
226+
],
227+
children: [
228+
rect({
229+
x: 20,
230+
y: 20,
231+
width: 50,
232+
height: 50,
233+
draw: [
234+
{fill: 'blue'}
235+
]
236+
})
237+
]
192238
})
193239
```
194240

@@ -212,19 +258,19 @@ Draws line(s) given an object containing drawing parameters.
212258
#### Example:
213259
```js
214260
line({
215-
x: 10,
216-
y: 10,
217-
style: {
218-
lineWidth: 2,
219-
lineCap: 'square',
220-
strokeStyle: '#CCCCCC'
221-
},
222-
points: [
223-
{x: 10, y: 10},
224-
{x: 10, y: 20},
225-
{x: 20, y: 10},
226-
{x: 10, y: 10}
227-
]
261+
x: 10,
262+
y: 10,
263+
style: {
264+
lineWidth: 2,
265+
lineCap: 'square',
266+
strokeStyle: '#CCCCCC'
267+
},
268+
points: [
269+
{x: 10, y: 10},
270+
{x: 10, y: 20},
271+
{x: 20, y: 10},
272+
{x: 10, y: 10}
273+
]
228274
})
229275
```
230276

@@ -272,17 +318,17 @@ Draws line(s) given an object containing drawing parameters.
272318
#### Example:
273319
```js
274320
polygon({
275-
points: [
276-
{x: 10, y: 0},
277-
{x: 0, y: 10},
278-
{x: 0, y: 30},
279-
{x: 30, y: 30},
280-
{x: 30, y: 10} // a house shaped polygon
281-
],
282-
draw: {
283-
stroke: '#000',
284-
fill: '#ccc'
285-
},
321+
points: [
322+
{x: 10, y: 0},
323+
{x: 0, y: 10},
324+
{x: 0, y: 30},
325+
{x: 30, y: 30},
326+
{x: 30, y: 10} // a house shaped polygon
327+
],
328+
draw: {
329+
stroke: '#000',
330+
fill: '#ccc'
331+
},
286332
})
287333
```
288334

@@ -304,13 +350,13 @@ Draws text given an object containing drawing parameters.
304350
#### Example:
305351
```js
306352
text({
307-
x: 10,
308-
y: 10,
309-
value: 'Hello World!',
310-
font: '18pt Arial',
311-
draw: [
312-
{fill: 'white'}
313-
]
353+
x: 10,
354+
y: 10,
355+
value: 'Hello World!',
356+
font: '18pt Arial',
357+
draw: [
358+
{fill: 'white'}
359+
]
314360
})
315361
```
316362

@@ -333,8 +379,8 @@ Draws an image given an object containing drawing parameters.
333379
#### Example:
334380
```js
335381
image({
336-
x: 10,
337-
y: 10,
382+
x: 10,
383+
y: 10,
338384
src: document.querySelector('img')
339385
})
340386
```
@@ -348,18 +394,18 @@ Moves the canvas origin to a different point.
348394

349395
#### Example:
350396
```js
351-
rect({
352-
transformations: [
397+
rect({
398+
transformations: [
353399
{translate: {x: 10, y: 10}}
354400
],
355-
x: 100,
356-
y: 100,
357-
width: 150,
358-
height: 150,
359-
draw: [
360-
{fill: 'purple'}
361-
]
362-
})
401+
x: 100,
402+
y: 100,
403+
width: 150,
404+
height: 150,
405+
draw: [
406+
{fill: 'purple'}
407+
]
408+
})
363409
```
364410

365411
### <a id="rotate"></a> `rotate: number`
@@ -368,18 +414,18 @@ Rotate the canvas around the current origin.
368414

369415
#### Example:
370416
```js
371-
rect({
372-
transformations: [
373-
{rotate: (20*Math.PI/180)}
417+
rect({
418+
transformations: [
419+
{rotate: (20*Math.PI/180)}
374420
],
375-
x: 10,
376-
y: 10,
377-
width: 150,
378-
height: 150,
379-
draw: [
380-
{fill: 'purple'}
381-
]
382-
})
421+
x: 10,
422+
y: 10,
423+
width: 150,
424+
height: 150,
425+
draw: [
426+
{fill: 'purple'}
427+
]
428+
})
383429
```
384430

385431
### <a id="scale"></a> `scale: {x: number, y: number}`
@@ -388,18 +434,18 @@ Scales the drawing bigger or smaller.
388434

389435
#### Example:
390436
```js
391-
rect({
392-
transformations: [
393-
{scale: {x: 2, y: 2}},
437+
rect({
438+
transformations: [
439+
{scale: {x: 2, y: 2}},
394440
],
395-
x: 10,
396-
y: 10,
397-
width: 150,
398-
height: 150,
399-
draw: [
400-
{fill: 'purple'}
401-
]
402-
})
441+
x: 10,
442+
y: 10,
443+
width: 150,
444+
height: 150,
445+
draw: [
446+
{fill: 'purple'}
447+
]
448+
})
403449
```
404450

405451
### Combining transformations
@@ -408,17 +454,17 @@ Scales the drawing bigger or smaller.
408454

409455
Rotate around the point (100, 100) and draw a 50x50px box centered there:
410456
```js
411-
rect({
412-
transformations: [
457+
rect({
458+
transformations: [
413459
{translate: {x: 100, y: 100}},
414460
{rotate: (20*Math.PI/180)}
415461
],
416-
x: -25, // At this point, {x: 0, y: 0} is a point on position {x: 100, y: 100} of the canvas
417-
y: -25,
418-
width: 50,
419-
height: 50,
420-
draw: [
421-
{fill: 'purple'}
422-
]
423-
})
462+
x: -25, // At this point, {x: 0, y: 0} is a point on position {x: 100, y: 100} of the canvas
463+
y: -25,
464+
width: 50,
465+
height: 50,
466+
draw: [
467+
{fill: 'purple'}
468+
]
469+
})
424470
```

0 commit comments

Comments
 (0)