This repository has been archived by the owner on Nov 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (53 loc) · 1.9 KB
/
index.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
const Pixi = require('pixi.js')
const PixiDom = require('pixi.dom')(Pixi)
const Mute = require('muuute')
const TreeForEachIdentical = require('./lib/treeForEachIdentical').treeForEachIdentical
const properties = ['x','y','alpha','scale.x','scale.y']
const transfert = (a,b)=>Mute.switch(a,properties,b,a.__style__)
const isSame = (a,b)=>a&&b&&a.name&&b.name&&a.name==b.name
// App :: {model:Object, Reducer reducer:Function, View view:Function, Effet effect:Function, el:DomElement}
// Model :: data:Object => model:Object
// Reducer :: model:Object => action:Object => model:Object
// View :: model:Object => prev:Object => send:Function => tree:Pixi.DisplaObject
// Effect :: model:Object => prev:Object => action:Object => send:Function => undefined
// app :: IO App app:Object
const app = ({model,view,reducer,effect=()=>{},el=document.body,pixiOpts={}})=>{
const width = pixiOpts.width || 1000
const heigth = pixiOpts.heigth || 1000
const renderer = Pixi.autoDetectRenderer(width, heigth, pixiOpts)
let tree
let rerender
const dispatch = action =>{
const newModel = reducer(model, action)
effect(newModel, model, action, send, renderer, tree)
rerender = newModel !== model
if(rerender){
destroyDomElements(Pixi.DOM.getAllElements())
var newTree = view(newModel,model,send)
TreeForEachIdentical(transfert,isSame,newTree,tree)
tree = newTree
model = newModel
}
}
const send = action=>setTimeout(()=>dispatch(action))
const frame = time=>{
requestAnimationFrame(frame)
if(Mute.muted() || rerender){
Mute.update()
renderer.render(tree)
rerender = false
}
}
send({type:'init'})
tree = view(model,{},send)
renderer.render(tree)
requestAnimationFrame(frame)
el.appendChild(renderer.view)
return el
}
const destroyDomElements = els=>{
for(var i = els.length-1; i!=-1; i--){
els[i].destroy()
}
}
module.exports = app