From e3be64d253d3b6fa3b1a2849f7be501cb9472487 Mon Sep 17 00:00:00 2001 From: Kohei Watanabe Date: Tue, 29 Dec 2020 11:46:13 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=E3=83=91=E3=83=95=E3=82=A9=E3=83=BC?= =?UTF-8?q?=E3=83=9E=E3=83=B3=E3=82=B9=E3=81=AE=E6=94=B9=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.ts | 79 ++++++++++++++++++++++++----------------------------- 1 file changed, 35 insertions(+), 44 deletions(-) diff --git a/src/main.ts b/src/main.ts index 309fd8b..07acf2b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -20,10 +20,7 @@ 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(); - const material = new MeshBasicMaterial({ - color: new Color("white"), - transparent: true, - }); + const material = new MeshBasicMaterial({ opacity: 0 }); const mesh = new Mesh(geometry, material); return mesh; }); @@ -31,48 +28,29 @@ const renderer = new WebGLRenderer(); const canvas = renderer.domElement; const raycaster = new Raycaster(); +function render() { + renderer.render(scene, camera); +} + function drawRect() { - initPanels(panels); raycaster.setFromCamera(mouse, camera); const [intersect] = raycaster.intersectObjects(panels); if (!intersect) return; - (intersect.object as Mesh< - PlaneGeometry, - MeshBasicMaterial - >).material.opacity = 0.25; const c16s = ["red", "green", "blue", "cyan", "magenta", "yellow"]; const c16 = c16s[randomInt(c16s.length - 1)]; const color = new Color(c16); const geometry = new PlaneGeometry(1, 1); - const material = new MeshBasicMaterial({ - color, - }); + const material = new MeshBasicMaterial({ color }); const mesh = new Mesh(geometry, material); mesh.scale.set(0.1, 0.1, 1); Object.assign(mesh.position, intersect.point); scene.add(mesh); -} - -function initPanels(panels: Mesh[]) { - const width = camera.aspect / grid.colum; - const height = 1 / grid.row; - for (const [i, panel] of panels.entries()) { - panel.material.opacity = 0; - 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), - }); - } + render(); } function handleKeydown({ key }: KeyboardEvent) { - if (key === " ") { - scene.clear(); - setup(); - return; - } + if (key === " ") return setup(); const [x = randomInt(grid.colum - 1), y = randomInt(grid.row - 1)] = [ ...keyPosition(keyLayouts.default, key), @@ -101,11 +79,36 @@ function handleMouseMoveEnd() { mouse.set(NaN, NaN); } +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(); +} + function setup() { scene.background = new Color(); - initPanels(panels); + scene.clear(); scene.add(...panels); camera.position.z = 1; + adjustCanvasSize(); +} + +function main() { + renderer.setSize(window.innerWidth, window.innerHeight); + Object.assign(canvas.style, { width: "100vw", height: "100vh" }); root.addEventListener("keydown", handleKeydown); root.addEventListener("touchmove", handleTouchmove); root.addEventListener("keyup", handleMouseMoveEnd); @@ -119,21 +122,9 @@ function setup() { handleMouseMoveEnd(); }); } -} - -function tick() { - window.requestAnimationFrame(tick); - camera.aspect = canvas.clientWidth / canvas.clientHeight; - camera.updateProjectionMatrix(); - renderer.render(scene, camera); -} - -function main() { - renderer.setSize(window.innerWidth, window.innerHeight); - Object.assign(canvas.style, { width: "100vw", height: "100vh" }); root.appendChild(canvas); + window.addEventListener("resize", adjustCanvasSize); setup(); - tick(); } main();