allow moving object while creating it
This commit is contained in:
parent
303a423a29
commit
f3c8fc85fa
17
simulator.js
17
simulator.js
@ -96,14 +96,19 @@ export class Sim {
|
||||
this.info['Mouse move'] = [`${e.clientX}, `, `${e.clientY}`];
|
||||
this.renderInfo();
|
||||
}
|
||||
this.handleCursorMove(e.clientX, e.clientY);
|
||||
});
|
||||
|
||||
// Monitor touch events
|
||||
el.addEventListener('touchmove', e => {
|
||||
if (this.DISPLAY_CURSOR_INFO) {
|
||||
this.info['Touch move'] = [`${e.touches[0].pageX}, `, `${e.touches[0].pageY}`];
|
||||
this.renderInfo();
|
||||
}
|
||||
// TODO: If e.touches.length > 1, user may be engaging pinch to zoom
|
||||
this.handleCursorMove(e.touches[0].pageX, e.touches[0].pageY);
|
||||
});
|
||||
|
||||
el.addEventListener('pointerdown', e => {
|
||||
if (this.DISPLAY_CURSOR_INFO) {
|
||||
this.info['Pointer down'] = [`${e.clientX}, `, `${e.clientY}`];
|
||||
@ -111,6 +116,7 @@ export class Sim {
|
||||
}
|
||||
this.createObject(e.clientX, e.clientY);
|
||||
});
|
||||
|
||||
el.addEventListener('pointerup', e => {
|
||||
if (this.DISPLAY_CURSOR_INFO) {
|
||||
this.info['Pointer up'] = [`${e.clientX}, `, `${e.clientY}`];
|
||||
@ -118,6 +124,7 @@ export class Sim {
|
||||
}
|
||||
this.doneCreatingObject();
|
||||
});
|
||||
|
||||
el.addEventListener('click', e => {
|
||||
if (this.DISPLAY_CURSOR_INFO) {
|
||||
this.info['Click'] = [`${e.clientX}, `, `${e.clientY}`];
|
||||
@ -130,6 +137,16 @@ export class Sim {
|
||||
requestAnimationFrame(t => this.loop(t));
|
||||
}
|
||||
|
||||
// Handle cursor (mouse or touch) movement
|
||||
handleCursorMove(x, y) {
|
||||
// If the cursor moves while creating an object, update the position of the object
|
||||
if (this.creatingObject !== undefined) {
|
||||
const obj = this.objects[this.creatingObject];
|
||||
obj.position.x = x;
|
||||
obj.position.y = y;
|
||||
}
|
||||
}
|
||||
|
||||
// Create an object with mass that grows as pointer is held down
|
||||
createObject(x, y) {
|
||||
const obj = new MassObject(x, y);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user