-
Notifications
You must be signed in to change notification settings - Fork 8
/
render.js
71 lines (55 loc) · 1.63 KB
/
render.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* globals __DEV__ */
import { frame } from 'wilderness-core'
import { node } from 'wilderness-dom-node'
import { tick } from './timeline'
const appendNode = (container, shape) => {
if (shape.replace) {
shape.replace.parentNode.replaceChild(shape.node, shape.replace)
delete shape.replace
} else {
container.appendChild(shape.node)
}
}
const createNode = (shape, frameShape) => {
shape.node = node(frameShape)
}
const render = (container, ...shapesAndTimelines) => {
if (__DEV__) {
if (typeof container !== 'object' || !container.nodeName) {
throw new TypeError(`The first argument passed to the render function must be a dom node`)
}
}
const { shapes, timelines } = split(shapesAndTimelines)
shapes.map(shape => {
createNode(shape, shape.keyframes[ 0 ].frameShape)
})
timelines.map(timeline => {
const frameShapes = frame(timeline)
timeline.timelineShapes.map(({ shape }, i) => {
createNode(shape, frameShapes[ i ])
})
})
shapes.map(shape => appendNode(container, shape))
timelines.map(timeline => {
timeline.timelineShapes.map(({ shape }) => appendNode(container, shape))
timeline.state.rendered = true
})
tick()
}
const split = shapesAndTimelines => {
const shapes = []
const timelines = []
shapesAndTimelines.map(x => {
if (x.keyframes) {
if (__DEV__ && x.timeline) {
throw new Error(`You cannot render a shape that has been placed on a timeline, instead render the timeline`)
}
shapes.push(x)
} else {
// @todo validate timeline
timelines.push(x)
}
})
return { shapes, timelines }
}
export default render