-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
WebGL.js
86 lines (70 loc) · 1.74 KB
/
WebGL.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import React from 'react'
import PropTypes from 'prop-types'
import _ from 'lodash'
import Icon from 'react-fontawesome'
import { app, StoreWrap } from 'xadmin'
import elementResizeEvent from 'element-resize-event'
class WebGL extends React.Component {
init = false
initing = false
// first add
componentDidMount() {
let onEvents = this.props.onEvents || {}
this.renderGLDom(glObj => glObj.resize())
//this.bindEvents(glObj, onEvents)
// on resize
elementResizeEvent(this.refs.webglDom, () => {
this.renderGLDom(glObj => glObj.resize())
})
}
// update
componentDidUpdate() {
this.renderGLDom(glObj => glObj.resize())
}
// remove
componentWillUnmount() {
}
//bind the events
bindEvents(instance, events) {
let _loop = (eventName) => {
// ignore the event config which not satisfy
if (typeof eventName === 'string' && typeof events[eventName] === 'function') {
// binding event
instance.off(eventName)
instance.on(eventName, (param) => {
events[eventName](param, instance)
})
}
}
for (let eventName in events) {
_loop(eventName)
}
}
// render the dom
renderGLDom(cb) {
if(this.initing) return
this.getGLInstance(glObj => {
glObj.setOption(this.props)
cb(glObj)
})
}
getGLInstance(cb) {
if(this.init) {
cb(this.props.modal)
} else {
this.initing = true
this.props.modal.init(this.refs.webglDom, () => {
cb(this.props.modal)
this.init = true
this.initing = false
})
}
}
render() {
const style = {
height: '100%', ...this.props.style
}
return <div style={style} ref="webglDom" />
}
}
export default WebGL