allow moving object while creating it

This commit is contained in:
Lentil Hoffman 2025-12-21 08:29:24 -06:00
parent 303a423a29
commit f3c8fc85fa
Signed by: lentil
GPG Key ID: 0F5B99F3F4D0C087

View File

@ -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);