-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
scene.js
302 lines (280 loc) · 8.82 KB
/
scene.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
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
293
294
295
296
297
298
299
300
301
302
import Component from "@pencil.js/component";
import OffscreenCanvas from "@pencil.js/offscreen-canvas";
import KeyboardEvent from "@pencil.js/keyboard-event";
import MouseEvent from "@pencil.js/mouse-event";
import Position from "@pencil.js/position";
/**
* @module Scene
*/
const listenForEventsKey = Symbol("_listenForEvents");
/**
* Scene class
* @class
* @extends {module:OffscreenCanvas}
*/
export default class Scene extends OffscreenCanvas {
/**
* Scene constructor
* @param {HTMLElement} [container=document.body] - Container of the renderer
* @param {SceneOptions} [options] - Specific options
*/
constructor (container = window.document.body, options) {
if (container === window.document.body) {
container.style.margin = 0;
container.style.height = `${window.innerHeight}px`;
}
const measures = container.getBoundingClientRect();
if (container instanceof window.HTMLCanvasElement) {
super(null, 0, options);
this.ctx = container.getContext("2d");
}
else {
super(container, 0, options);
container.appendChild(this.ctx.canvas);
}
/**
* @type {Position}
*/
this.cursorPosition = new Position();
/**
* @type {Position}
*/
this.containerPosition = new Position(measures.left + window.scrollX, measures.top + window.scrollY);
/**
* @type {Boolean}
*/
this.isScene = true;
/**
* @type {Boolean}
*/
this.isLooped = false;
/**
* @type {Boolean}
*/
this.isClicked = false;
/**
* @type {Number}
*/
this.fps = 0;
/**
* @type {Number}
*/
this.lastTick = null;
this[listenForEventsKey](container);
}
/**
*
* @param {String} [cursor=Component.cursors.default] - Cursor string
* @return {Scene} Itself
*/
setCursor (cursor = Component.cursors.default) {
this.ctx.canvas.style.cursor = cursor;
return this;
}
/**
* Draw the whole scene
* @return {Scene} Itself
*/
render () {
const animationId = this.isLooped ? window.requestAnimationFrame(this.render.bind(this, undefined)) : null;
try {
super.render(this.ctx);
}
catch (error) {
window.cancelAnimationFrame(animationId);
this.stopLoop();
throw error;
}
const now = window.performance.now();
if (this.isLooped && this.lastTick) {
this.fps = 1000 / (now - this.lastTick);
}
this.lastTick = now;
return this;
}
/**
* Define if is hovered
* @return {Boolean}
*/
isHover () {
return this.options.shown;
}
/**
* Start to render the scene each frame
* @return {Scene} Itself
*/
startLoop () {
this.isLooped = true;
this.render();
return this;
}
/**
* Stop scene from being rendered
* @return {Scene} Itself
*/
stopLoop () {
this.isLooped = false;
this.fps = 0;
return this;
}
/**
* @inheritDoc
* @return {Scene} Itself
*/
hide () {
this.ctx.canvas.style.visibility = "hidden";
return super.hide();
}
/**
* @inheritDoc
* @return {Scene} Itself
*/
show () {
this.ctx.canvas.style.visibility = "";
return super.show();
}
/**
* @inheritDoc
* @param {Object} definition - Scene definition
* @return {Scene}
*/
static from (definition) {
return new Scene(undefined, definition.options);
}
/**
* Build a canvas and set it to fill the entire document.body
* @param {HTMLElement} container - Element holding the canvas
* @inheritDoc
*/
static getDrawingContext (container = window.document.body) {
if (container) {
const { scrollWidth, scrollHeight } = container;
const ctx = super.getDrawingContext(scrollWidth, scrollHeight);
ctx.canvas.style.display = "block";
ctx.canvas.style.position = "absolute";
return ctx;
}
return null;
}
/**
* @typedef {Object} SceneOptions
* @extends OffscreenCanvasOptions
* @prop {String} [cursor=Component.cursors.defaultOptions] - Cursor on hover
*/
/**
* @type {SceneOptions}
*/
static get defaultOptions () {
return {
...super.defaultOptions,
cursor: Component.cursors.default,
};
}
/**
* @typedef {Object} SceneEvents
* @extends ContainerEvent
* @prop {String} change -
*/
/**
* @type {SceneEvents}
*/
static get events () {
return {
...super.events,
change: "change-scene",
};
}
}
/**
* Bind events and call them on targets (can't be called twice)
* @param {HTMLElement} container - Container to bind event to
* @memberOf Scene#
*/
Scene.prototype[listenForEventsKey] = function listenForEvents (container) {
if (this.isReady) {
throw new EvalError("Can't rebind event a second time.");
}
let hovered = null;
let startPosition = null;
const mouseListeners = {
[MouseEvent.events.down]: (target, eventPosition) => {
target.isClicked = true;
startPosition = eventPosition;
},
[MouseEvent.events.move]: (target, eventPosition) => {
if (startPosition) {
target.isClicked = target.isClicked && eventPosition.distance(startPosition) < 10;
}
if (target !== hovered) {
if (hovered) {
hovered.isHovered = false;
if (!hovered.isAncestorOf(target)) {
hovered.fire(new MouseEvent(MouseEvent.events.leave, hovered, eventPosition));
}
}
hovered = target;
}
if (!target.isHovered) {
target.isHovered = true;
target.fire(new MouseEvent(MouseEvent.events.hover, target, eventPosition));
}
this.setCursor(target.options.cursor);
this.cursorPosition.set(eventPosition);
},
[MouseEvent.events.up]: (target, eventPosition, event) => {
startPosition = null;
if (target.isClicked) {
target.fire(new MouseEvent(MouseEvent.events.click, target, eventPosition, event));
target.isClicked = false;
}
},
[MouseEvent.events.wheel]: (target, eventPosition, event) => {
const mouseEvents = MouseEvent.events;
const events = event.deltaY > 0 ?
[mouseEvents.scrollDown, mouseEvents.zoomOut] :
[mouseEvents.scrollUp, mouseEvents.zoomIn];
target.fire(new MouseEvent(events[0], target, eventPosition, event))
.fire(new MouseEvent(events[1], target, eventPosition, event));
},
mouseout: (target, eventPosition, event) => {
target.fire(new MouseEvent(MouseEvent.events.leave, target, eventPosition, event));
},
mouseenter: (target, eventPosition, event) => {
target.fire(new MouseEvent(MouseEvent.events.hover, target, eventPosition, event));
},
[MouseEvent.events.contextMenu]: null,
[MouseEvent.events.doubleClick]: null,
};
Object.keys(mouseListeners).forEach((eventName) => {
container.addEventListener(eventName, (event) => {
if (this.options.shown) {
const eventPosition = (new Position(event.clientX, event.clientY))
.subtract(this.containerPosition)
.add(window.scrollX, window.scrollY);
const target = this.getTarget(eventPosition, this.ctx);
if (target) {
target.fire(new MouseEvent(eventName, target, eventPosition, event));
if (mouseListeners[eventName] instanceof Function) {
mouseListeners[eventName](target, eventPosition, event);
}
}
}
}, {
passive: false,
});
});
const keyboardListener = {
[KeyboardEvent.events.down]: null,
[KeyboardEvent.events.up]: null,
};
Object.keys(keyboardListener).forEach((eventName) => {
window.document.addEventListener(eventName, (event) => {
if (this.options.shown) {
this.fire(new KeyboardEvent(eventName, this, event));
if (keyboardListener[eventName] instanceof Function) {
keyboardListener[eventName](event);
}
}
});
});
};