Repository navigation
Expand file tree
/
Copy pathbengine.js
More file actions
292 lines (267 loc) · 9.01 KB
/
Copy pathbengine.js
File metadata and controls
292 lines (267 loc) · 9.01 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
const startP = 250
function nopx(string) {
return Number(string.substring(0, string.length-2))
}
export function distance(vec1, vec2) {
const a = Math.pow((vec2.x - vec1.x), 2)
const b = Math.pow((vec2.y - vec1.y), 2)
const c = Math.pow((vec2.z - vec1.z), 2)
return Math.sqrt(a + b + c)
}
export function magnitude(vec) {
return Math.sqrt(vec.x*vec.x + vec.y*vec.y + vec.z*vec.z)
}
export function normalize(vec) {
const v = magnitude(vec)
return {
x: vec.x / v,
y: vec.y / v,
z: vec.z / v
}
}
export function angToVec(angle) {
angle.x *= Math.PI / 180
angle.y *= Math.PI / -180
const x = Math.cos(angle.y) * Math.cos(angle.x)
const y = Math.sin(angle.x)
const z = Math.sin(angle.y) * Math.cos(angle.x)
return {
x: x,
y: y,
z: z
}
}
export function camera(element) {
// HTML element creation
const scene = document.createElement("div")
scene.style.width = "0"
scene.style.height = "0"
scene.style.position = "absolute"
scene.style.left = "50%"
scene.style.top = "50%"
scene.style.transform = "translateX(-50%) translateY(-50%)"
scene.style.transformStyle = "preserve-3d"
element.appendChild(scene)
element.style.perspective = startP +"px"
element.style.overflow = "hidden"
// Variables creation
this.fov = startP
this.pos = {
x: 0,
y: 0,
z: 0
}
this.offset = {
x: 0,
y: 0,
z: 0
}
this.rot = {
x: 0,
y: 0,
z: 0
}
this.render = function() {
scene.style.transformOrigin = "calc(50% + "+ this.pos.x +"px) calc(50% + "+ -this.pos.y +"px) "+ this.pos.z +"px" // Pivot
scene.style.transform = "" // Clears transform
scene.style.transform += "translateX("+ -this.pos.x +"px) " // X pivot offset
scene.style.transform += "translateY("+ this.pos.y +"px) " // Y pivot offset
scene.style.transform += "translateZ("+ -this.pos.z +"px) " // Z pivot offset
scene.style.transform += "translateX(-50%) translateY(-50%) " // Makes element's center go to the real center
scene.style.transform += "translateZ("+ this.fov +"px) " // Z pos
scene.style.transform += "translateX("+ -this.offset.x +"px) " // offset
scene.style.transform += "translateY("+ this.offset.y +"px) " // offset
scene.style.transform += "translateZ("+ -this.offset.z +"px) " // offset
scene.style.transform += "rotateZ("+ this.rot.z +"deg) " // Z rot
scene.style.transform += "rotateX("+ this.rot.x +"deg) " // X rot
scene.style.transform += "rotateY("+ this.rot.y +"deg) " // Y rot
}
this.setFov = function(num) {
this.fov = num
element.style.perspective = num +"px"
this.render()
}
this.setPos = function(options) {
this.pos.x = options.x
this.pos.y = options.y
this.pos.z = options.z
this.render()
}
this.setOffset = function(options) {
this.offset.x = options.x
this.offset.y = options.y
this.offset.z = options.z
this.render()
}
this.setRot = function(options) {
this.rot.x = options.x
this.rot.y = options.y
this.rot.z = options.z
this.render()
}
this.setTransition = function(options) {
const toSet = "transform "+ options.time +"s "+ options.curve
scene.style.transition = toSet
}
this.append = function(sprite) {
scene.appendChild(sprite.body)
sprite.parent = this
}
}
export function sprite(options) {
if (options == null) {options = {}}
// HTML element creation
this.body = document.createElement("div")
this.body.style.position = "absolute"
this.body.style.left = "50%"
this.body.style.top = "50%"
this.body.style.transform = "translateX(-50%) translateY(-50%)"
this.body.style.transformStyle = "preserve-3d"
this.body.style.imageRendering = "pixelated"
this.body.style.pointerEvents = "none"
// Default characteristics and variables creation
this.pos = options.pos || {
x: 0,
y: 0,
z: 0
}
this.rot = options.rot || {
x: 0,
y: 0,
z: 0
}
this.scale = options.scale || {
x: 1,
y: 1,
z: 1
}
this.pivot = options.pivot || {
x: 0,
y: 0,
z: 0
}
this.pivotPercent = options.pivotPercent || {
x: 0,
y: 0
}
this.parent = null
this.render = function() {
const pivotX = "calc(50% + "+ this.pivot.x +"px + "+ this.pivotPercent.x +"%)" // X pivot
const pivotY = "calc(50% + "+ -this.pivot.y +"px + "+ -this.pivotPercent.y +"%)" // Y pivot
const pivotZ = this.pivot.z +"px" // Z pivot
this.body.style.transformOrigin = pivotX + pivotY + pivotZ // Joined together for readability
this.body.style.transform = "" // Clears transform
this.body.style.transform += "translateX(-50%) translateY(-50%) " // Makes element's center go to the real center
this.body.style.transform += "translateX("+ -this.pivot.x +"px)" // X pivot compensation
this.body.style.transform += "translateY("+ this.pivot.y +"px)" // Y pivot compensation
this.body.style.transform += "translateX("+ -this.pivotPercent.x +"%)" // X pivot percent compensation
this.body.style.transform += "translateY("+ this.pivotPercent.y +"%)" // Y pivot percent compensation
this.body.style.transform += "translateX("+ this.pos.x +"px) " // X pos
this.body.style.transform += "translateY("+ -this.pos.y +"px) " // Y pos
this.body.style.transform += "translateZ("+ this.pos.z +"px) " // Z pos
this.body.style.transform += "rotate3d(0, 1, 0, "+ this.rot.y +"deg) " // Y rot
this.body.style.transform += "rotate3d(1, 0, 0, "+ this.rot.x +"deg) " // X rot
this.body.style.transform += "rotate3d(0, 0, 1, "+ this.rot.z +"deg) " // Z rot
this.body.style.transform += "scale3d("+ this.scale.x +", "+ this.scale.y +", "+ this.scale.z +")" // Scale
}
this.body.addEventListener("mousedown", (event) => {
if (this.onClick) {
event.stopPropagation()
//console.log(event.offsetX)
const x = event.offsetX - (nopx(this.body.style.width) / 2)
const y = (event.offsetY - (nopx(this.body.style.height) / 2)) * -1
this.onClick(x, y)
//console.log("clicked at "+ x +" "+ y)
}
})
this.setPos = function(options) {
this.pos.x = options.x
this.pos.y = options.y
this.pos.z = options.z
this.render()
}
this.setRot = function(options) {
this.rot.x = options.x
this.rot.y = options.y
this.rot.z = options.z
this.render()
}
this.setScale = function(options) {
if (typeof options == "number") {
this.scale.x = options
this.scale.y = options
this.scale.z = options
} else {
this.scale.x = options.x
this.scale.y = options.y
this.scale.z = options.z
}
this.render()
}
this.setPivot = function(options) {
this.pivot.x = options.x
this.pivot.y = options.y
this.pivot.z = options.z
this.render()
}
this.setPivotPercent = function(options) {
this.pivotPercent.x = options.x
this.pivotPercent.y = options.y
this.pivotPercent.z = options.z
this.render()
}
this.setTexture = function(url) {
const body = this.body
if (url) {
const img = new Image();
img.onload = function() {
body.style.width = img.width +"px"
body.style.height = img.height +"px"
body.style.backgroundImage = url
}
img.src = url
} else {
body.style.width = ""
body.style.height = ""
body.style.backgroundImage = ""
}
}
this.setOnClick = function(func) {
this.onClick = func
if (func == null) {
this.body.style.pointerEvents = "none"
} else {
this.body.style.pointerEvents = "all"
}
}
this.setTransition = function(options) {
this.body.style.transition = "transform "+ options
}
this.append = function(sprite) {
this.body.appendChild(sprite.body)
sprite.parent = this
}
this.getGlobalRot = function() {
let myRot = this.rot
if (this.parent instanceof camera) {
return myRot
} else {
const parentRot = this.parent.getGlobalRot()
myRot.x += parentRot.x
myRot.y += parentRot.y
myRot.z += parentRot.z
}
return myRot
}
this.getNormal = function() {
const normal = angToVec({
x: this.rot.x,
y: this.rot.y - 90
})
return normal
}
if (options.texture) {
this.setTexture(options.texture)
}
this.render()
}