Skip to content

Commit aaaf050

Browse files
committed
Added drag rotating
1 parent d74c579 commit aaaf050

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Make 3D builds out of little cubes, you can change cube colors, view from a diff
1313
- `alt` + `drag`: Draw with cubes
1414
- `RMB`: Break cube
1515
- `MMB`: Pick color
16+
- `RMB` + `drag`: Rotate view (locked to 90° increments)
1617
- `drag color-picker to cube`: Pick color
1718
- `wheel up`: Zoom in
1819
- `wheel down`: Zoom out

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ <h2 id="controls">Controls</h2>
4343
<li><code>RMB</code>: Break cube</li>
4444
<li><code>MMB</code>: Pick color</li>
4545
<li><code>drag color-picker to cube</code>: Pick color</li>
46+
<li><code>RMB</code> + <code>drag</code>: Rotate view (locked to 90° increments)</li>
4647
<li><code>wheel up</code>: Zoom in</li>
4748
<li><code>wheel down</code>: Zoom out</li>
4849
<li><code>drag</code>: Pan</li>

index.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ const Params = new URLSearchParams(window.location.search);
1414

1515
let currColor = "#ff0000";
1616
let isPanning = false;
17+
let isRotating = false;
18+
let rotateDistance = 0;
19+
const rotateThreshold = 100;
1720
let rotation = 0;
1821
let panX = 0;
1922
let panY = 0;
@@ -330,14 +333,30 @@ function share() {
330333
let downPos = { x: 0, y: 0 };
331334
view.addEventListener("pointerdown", (e) => {
332335
if (e.target === view && !e.altKey) {
333-
isPanning = true;
334-
downPos = { x: e.clientX, y: e.clientY };
336+
if (e.button == 2) {
337+
isRotating = true;
338+
} else {
339+
isPanning = true;
340+
downPos = { x: e.clientX, y: e.clientY };
341+
}
335342
} else if (e.altKey) {
336343
isDragging = true;
337344
dragStartY = e.target.style.getPropertyValue("--y") || 0;
338345
}
339346
});
340-
view.addEventListener("pointermove", (e) => {
347+
document.addEventListener("pointermove", (e) => {
348+
if (isRotating) {
349+
rotateDistance -= e.movementX;
350+
if (rotateDistance >= rotateThreshold) {
351+
rotate(rotation + 90 * Math.floor(rotateDistance / rotateThreshold));
352+
353+
rotateDistance %= rotateThreshold;
354+
} else if (rotateDistance <= -rotateThreshold) {
355+
rotate(rotation - 90 * Math.floor(rotateDistance / -rotateThreshold));
356+
357+
rotateDistance %= -rotateThreshold;
358+
}
359+
}
341360
if (isPanning) {
342361
pan(e.movementX, e.movementY);
343362
}
@@ -346,7 +365,7 @@ view.addEventListener("pointermove", (e) => {
346365
placeCube(worldPos.x, dragStartY, worldPos.z);
347366
}
348367
});
349-
view.addEventListener("pointerup", (e) => {
368+
document.addEventListener("pointerup", (e) => {
350369
if (!isPinching && isPanning && e.target === view && e.button !== 2 && e.clientX - downPos.x === 0 && e.clientY - downPos.y === 0) {
351370
const worldPos = worldCoords(e.clientX, e.clientY);
352371
placeCube(worldPos.x, 0, worldPos.z);
@@ -358,6 +377,7 @@ view.addEventListener("pointerup", (e) => {
358377
}
359378
}
360379
isPanning = false;
380+
isRotating = false;
361381
isBreaking = false;
362382
isPicking = false;
363383
isDragging = false;

0 commit comments

Comments
 (0)