pucchinglgl/src/main.ts

133 lines
3.7 KiB
TypeScript
Raw Normal View History

2020-12-29 03:33:50 +09:00
import {
Color,
Mesh,
MeshBasicMaterial,
PerspectiveCamera,
PlaneGeometry,
Raycaster,
Scene,
Vector2,
WebGLRenderer,
} from "three";
import randomInt from "./randomInt";
import keyPosition from "./keyPosition";
import keyLayouts from "./config/keyLayouts";
2020-12-29 11:54:14 +09:00
import pallet from "./config/pallet";
2020-12-29 03:33:50 +09:00
const root = document.body;
const scene = new Scene();
const camera = new PerspectiveCamera();
const grid = { colum: 14, row: 4 } as const;
const mouse = new Vector2(NaN, NaN);
const panels = Array.from({ length: grid.colum * grid.row }, () => {
const geometry = new PlaneGeometry();
2020-12-29 11:46:13 +09:00
const material = new MeshBasicMaterial({ opacity: 0 });
2020-12-29 03:33:50 +09:00
const mesh = new Mesh(geometry, material);
return mesh;
});
const renderer = new WebGLRenderer();
const canvas = renderer.domElement;
const raycaster = new Raycaster();
2020-12-29 11:46:13 +09:00
function render() {
renderer.render(scene, camera);
}
2020-12-29 03:33:50 +09:00
function drawRect() {
raycaster.setFromCamera(mouse, camera);
const [intersect] = raycaster.intersectObjects(panels);
if (!intersect) return;
2020-12-29 11:54:14 +09:00
const color = pallet[randomInt(pallet.length - 1)];
2020-12-29 03:33:50 +09:00
const geometry = new PlaneGeometry(1, 1);
2020-12-29 11:46:13 +09:00
const material = new MeshBasicMaterial({ color });
2020-12-29 03:33:50 +09:00
const mesh = new Mesh(geometry, material);
mesh.scale.set(0.1, 0.1, 1);
Object.assign(mesh.position, intersect.point);
scene.add(mesh);
2020-12-29 11:46:13 +09:00
render();
2020-12-29 03:33:50 +09:00
}
function handleKeydown({ key }: KeyboardEvent) {
2020-12-29 11:46:13 +09:00
if (key === " ") return setup();
2020-12-29 03:33:50 +09:00
const [x = randomInt(grid.colum - 1), y = randomInt(grid.row - 1)] = [
...keyPosition(keyLayouts.default, key),
...keyPosition(keyLayouts.shift, key),
2020-12-29 03:33:50 +09:00
];
const offset = () => Math.random() - 0.5;
mouse.x = ((x + offset()) / (grid.colum - 1)) * 2 - 1;
mouse.y = -((y + offset()) / (grid.row - 1)) * 2 + 1;
drawRect();
}
function handleMouseMove(event: { clientX: number; clientY: number }) {
mouse.x = (event.clientX / canvas.clientWidth) * 2 - 1;
mouse.y = -(event.clientY / canvas.clientHeight) * 2 + 1;
drawRect();
}
function handleTouchmove(event: TouchEvent) {
event.preventDefault();
2020-12-29 03:33:50 +09:00
[...event.touches].map(handleMouseMove);
}
function handleMouseMoveEnd() {
mouse.set(NaN, NaN);
}
2020-12-29 11:46:13 +09:00
function adjustPanelSize() {
const width = camera.aspect / grid.colum;
const height = 1 / grid.row;
for (const [i, panel] of panels.entries()) {
panel.scale.set(width, height, 1);
Object.assign(panel.position, {
x: width * ((i % grid.colum) - (grid.colum - 1) / 2),
y: height * (-Math.floor(i / grid.colum) + (grid.row - 1) / 2),
});
}
}
function adjustCanvasSize() {
camera.aspect = canvas.clientWidth / canvas.clientHeight;
camera.updateProjectionMatrix();
adjustPanelSize();
render();
}
2020-12-29 03:33:50 +09:00
function setup() {
scene.background = new Color();
2020-12-29 11:46:13 +09:00
scene.clear();
2020-12-29 03:33:50 +09:00
scene.add(...panels);
camera.position.z = 1;
2020-12-29 11:46:13 +09:00
adjustCanvasSize();
}
function main() {
renderer.setSize(window.innerWidth, window.innerHeight);
Object.assign(canvas.style, { width: "100%", height: "100%" });
2020-12-29 03:33:50 +09:00
root.addEventListener("keydown", handleKeydown);
root.addEventListener("touchstart", handleTouchmove, { passive: false });
root.addEventListener("touchmove", handleTouchmove, { passive: false });
2020-12-29 03:33:50 +09:00
root.addEventListener("mousedown", () => {
root.addEventListener("mousemove", handleMouseMove);
});
for (const event of ["mouseup", "mouseleave"] as const) {
root.addEventListener(event, () => {
root.removeEventListener("mousemove", handleMouseMove);
});
}
2020-12-29 16:36:33 +09:00
for (const event of ["keyup", "mouseup", "mouseleave", "touchend"] as const) {
root.addEventListener(event, handleMouseMoveEnd);
}
2020-12-29 03:33:50 +09:00
root.appendChild(canvas);
Object.assign(root.style, { overflow: "hidden", overscrollBehavior: "none" });
2020-12-29 11:46:13 +09:00
window.addEventListener("resize", adjustCanvasSize);
2020-12-29 03:33:50 +09:00
setup();
}
main();