-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (50 loc) · 1.12 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
64
var css = require('css')
var rect = require('rect')
var v = require('vector')
var domEl = function (el) {
if (el) return el
else return document.createElement('div')
}
module.exports = function (parentEl) {
var dom = {}
dom.el = [domEl]
// listeners
dom.init = function (e) {
if (e.id) e.el.attributes.id = e.id
if (e.class) e.setClass(e.class)
if (e.classList) {
e.setClass()
e.classList.forEach(function (c) {
e.el.classList.add(c)
})
}
e.resize(e.mesh.size)
}
dom.start = function (e) {
parentEl.appendChild(e.el)
}
dom.render = function (e) {
e.moveTo(v(e.mesh.pos).sub(e.offset))
}
dom.stop = function (e) {
parentEl.removeChild(e.el)
}
// methods
dom.moveTo = function (pos) {
css(this.el, {
left: Math.round(pos.left)
, top: Math.round(pos.top)
})
}
dom.resize = function (size) {
css(this.el, {
width: size.width
, height: size.height
})
}
dom.setClass = function (className) {
this.el.className = 'entity entity-dom'
if (className) this.el.classList.add(className)
}
return dom
}