checkpoint, although draggable elements not moving
This commit is contained in:
parent
b8bf93269f
commit
acb86f23d2
@ -17,3 +17,4 @@ export const ZOOM_IN_FACTOR = 2;
|
|||||||
export const ZOOM_OUT_FACTOR = 0.5;
|
export const ZOOM_OUT_FACTOR = 0.5;
|
||||||
export const SCALE_MAX = 256;
|
export const SCALE_MAX = 256;
|
||||||
export const SCALE_MIN = 1/256;
|
export const SCALE_MIN = 1/256;
|
||||||
|
export const DRAGGABLE_ELEMENT_CLASSNAME = 'lhg-draggable-element';
|
||||||
|
|||||||
18
objects.js
18
objects.js
@ -60,6 +60,24 @@ export class Objects {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
handlePointerDown({x, y}) {
|
||||||
|
// If pointer is touching an object, select the object
|
||||||
|
const touchingObject = this.objectAtLocation(x, y);
|
||||||
|
|
||||||
|
if (touchingObject !== undefined) {
|
||||||
|
this.selectObject(touchingObject);
|
||||||
|
} else {
|
||||||
|
// Otherwise, create a new object
|
||||||
|
this.createObject(x, y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
handlePointerUp({x, y}) {
|
||||||
|
this.doneCreatingObject();
|
||||||
|
this.deselect();
|
||||||
|
}
|
||||||
|
|
||||||
computeFrame(elapsedTime) {
|
computeFrame(elapsedTime) {
|
||||||
// If we're creating an object, increment its mass
|
// If we're creating an object, increment its mass
|
||||||
// with the mass creation rate accelerating over time
|
// with the mass creation rate accelerating over time
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
import {DRAGGABLE_ELEMENT_CLASSNAME} from './config.js';
|
||||||
|
|
||||||
export class Overlay {
|
export class Overlay {
|
||||||
sim = undefined;
|
sim = undefined;
|
||||||
constructor(sim) {
|
constructor(sim) {
|
||||||
@ -36,7 +38,7 @@ export class Overlay {
|
|||||||
|
|
||||||
// Update positions of draggable items
|
// Update positions of draggable items
|
||||||
updateDraggable() {
|
updateDraggable() {
|
||||||
const elements = document.querySelectorAll('.draggable');
|
const elements = document.querySelectorAll(`.${DRAGGABLE_ELEMENT_CLASSNAME}`);
|
||||||
for (let el of elements) {
|
for (let el of elements) {
|
||||||
if (!el.dragging) continue;
|
if (!el.dragging) continue;
|
||||||
const {
|
const {
|
||||||
|
|||||||
19
pointer.js
19
pointer.js
@ -3,6 +3,7 @@ import {
|
|||||||
ZOOM_IN_FACTOR,
|
ZOOM_IN_FACTOR,
|
||||||
ZOOM_OUT_FACTOR,
|
ZOOM_OUT_FACTOR,
|
||||||
DISPLAY_CURSOR_INFO,
|
DISPLAY_CURSOR_INFO,
|
||||||
|
DRAGGABLE_ELEMENT_CLASSNAME,
|
||||||
} from './config.js';
|
} from './config.js';
|
||||||
|
|
||||||
function dispatchEvent(target, eventType, data) {
|
function dispatchEvent(target, eventType, data) {
|
||||||
@ -23,7 +24,7 @@ export class Pointer {
|
|||||||
const el = window;
|
const el = window;
|
||||||
|
|
||||||
el.addEventListener('pointermove', e => {
|
el.addEventListener('pointermove', e => {
|
||||||
// const velocity = this.sim.pointer.getPointerVelocity();
|
this.sim.info['pointermove'] = [`${e.clientX}, `, `${e.clientY}`];
|
||||||
if (this.draggingElement) {
|
if (this.draggingElement) {
|
||||||
// e.preventDefault();
|
// e.preventDefault();
|
||||||
this.draggingElement.dragging.pointerEnd = {
|
this.draggingElement.dragging.pointerEnd = {
|
||||||
@ -39,7 +40,7 @@ export class Pointer {
|
|||||||
el.addEventListener('pointerdown', e => {
|
el.addEventListener('pointerdown', e => {
|
||||||
// e.preventDefault();
|
// e.preventDefault();
|
||||||
let target = e.target;
|
let target = e.target;
|
||||||
while (target && !target.classList.contains('draggable')) {
|
while (target && !target.classList.contains(DRAGGABLE_ELEMENT_CLASSNAME)) {
|
||||||
target = target.parentElement;
|
target = target.parentElement;
|
||||||
}
|
}
|
||||||
if (target?.classList.contains('draggable')) {
|
if (target?.classList.contains('draggable')) {
|
||||||
@ -120,20 +121,12 @@ export class Pointer {
|
|||||||
this.clearPointerHistory();
|
this.clearPointerHistory();
|
||||||
this.updatePointer({x, y});
|
this.updatePointer({x, y});
|
||||||
|
|
||||||
// If pointer is touching an object, select the object
|
this.sim.objects.handlePointerDown({x, y});
|
||||||
const touchingObject = this.sim.objects.objectAtLocation(x, y);
|
|
||||||
|
|
||||||
if (touchingObject !== undefined) {
|
|
||||||
this.sim.objects.selectObject(touchingObject);
|
|
||||||
} else {
|
|
||||||
// Otherwise, create a new object
|
|
||||||
this.sim.objects.createObject(x, y);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
handlePointerUp({x, y}) {
|
handlePointerUp({x, y}) {
|
||||||
this.sim.objects.doneCreatingObject();
|
// TODO: Conditional?
|
||||||
this.sim.objects.deselect();
|
this.sim.objects.handlePointerUp({x, y});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle cursor (mouse or touch) movement
|
// Handle cursor (mouse or touch) movement
|
||||||
|
|||||||
@ -67,7 +67,6 @@ export class Sim {
|
|||||||
|
|
||||||
// Main loop
|
// Main loop
|
||||||
loop(currentTime) {
|
loop(currentTime) {
|
||||||
this.overlay.updateDraggable();
|
|
||||||
const elapsedTime = currentTime - this.time;
|
const elapsedTime = currentTime - this.time;
|
||||||
this.time = currentTime;
|
this.time = currentTime;
|
||||||
if (this.nextZoom) {
|
if (this.nextZoom) {
|
||||||
@ -78,6 +77,7 @@ export class Sim {
|
|||||||
this.objects.computeFrame(elapsedTime);
|
this.objects.computeFrame(elapsedTime);
|
||||||
this.display.fillCanvas();
|
this.display.fillCanvas();
|
||||||
this.display.drawObjects();
|
this.display.drawObjects();
|
||||||
|
this.overlay.updateDraggable();
|
||||||
this.overlay.renderInfo();
|
this.overlay.renderInfo();
|
||||||
requestAnimationFrame(t => this.loop(t));
|
requestAnimationFrame(t => this.loop(t));
|
||||||
}
|
}
|
||||||
|
|||||||
4
tool.js
4
tool.js
@ -2,6 +2,8 @@
|
|||||||
// can call back to toolbar for whatever...
|
// can call back to toolbar for whatever...
|
||||||
// through toolbar can access sim
|
// through toolbar can access sim
|
||||||
|
|
||||||
|
import {DRAGGABLE_ELEMENT_CLASSNAME} from './config.js';
|
||||||
|
|
||||||
export class Tool {
|
export class Tool {
|
||||||
toolbar = undefined;
|
toolbar = undefined;
|
||||||
sim = undefined;
|
sim = undefined;
|
||||||
@ -17,7 +19,7 @@ export class Tool {
|
|||||||
div.style.border = '1px #0fb solid';
|
div.style.border = '1px #0fb solid';
|
||||||
div.style.margin = '5px';
|
div.style.margin = '5px';
|
||||||
div.style.padding = '5px';
|
div.style.padding = '5px';
|
||||||
div.classList.add('draggable');
|
div.classList.add(DRAGGABLE_ELEMENT_CLASSNAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
frame() {}
|
frame() {}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user