From c0df9e0816122ca31a7856e6206793997fb2b015 Mon Sep 17 00:00:00 2001 From: trafficlunar Date: Fri, 13 Dec 2024 17:09:33 +0000 Subject: [PATCH] feat: add canvas information component shows canvas size, scale, and a slider to change the scale --- package.json | 1 + src/components/Canvas.tsx | 4 +- src/components/information/Canvas.tsx | 60 +++++++++++++++++++ .../Cursor.tsx} | 9 +-- src/index.css | 26 ++++---- 5 files changed, 82 insertions(+), 18 deletions(-) create mode 100644 src/components/information/Canvas.tsx rename src/components/{CursorInformation.tsx => information/Cursor.tsx} (65%) diff --git a/package.json b/package.json index 52abae8..145f870 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "dependencies": { "@pixi/react": "7", "@radix-ui/react-menubar": "^1.1.2", + "@radix-ui/react-slider": "^1.2.2", "@radix-ui/react-toggle": "^1.1.0", "@radix-ui/react-toggle-group": "^1.1.0", "class-variance-authority": "^0.7.1", diff --git a/src/components/Canvas.tsx b/src/components/Canvas.tsx index c1bef37..c79c71e 100644 --- a/src/components/Canvas.tsx +++ b/src/components/Canvas.tsx @@ -7,7 +7,8 @@ import { TexturesContext } from "@/context/TexturesContext"; import Blocks from "./Blocks"; import Cursor from "./Cursor"; -import CursorInformation from "./CursorInformation"; +import CursorInformation from "./information/Cursor"; +import CanvasInformation from "./information/Canvas"; // Set scale mode to NEAREST PIXI.settings.SCALE_MODE = PIXI.SCALE_MODES.NEAREST; @@ -139,6 +140,7 @@ function Canvas() { + ); } diff --git a/src/components/information/Canvas.tsx b/src/components/information/Canvas.tsx new file mode 100644 index 0000000..0b07bdc --- /dev/null +++ b/src/components/information/Canvas.tsx @@ -0,0 +1,60 @@ +import React, { useEffect, useMemo, useState } from "react"; +import { Slider } from "@/components/ui/slider"; + +interface Props { + scale: number; + setScale: React.Dispatch>; + blocks: Block[]; +} + +function CanvasInformation({ scale, setScale, blocks }: Props) { + const canvasSize = useMemo(() => { + let minX = Infinity, + maxX = -Infinity; + let minY = Infinity, + maxY = -Infinity; + + blocks.forEach((coord) => { + if (coord.x < minX) minX = coord.x; + if (coord.x > maxX) maxX = coord.x; + if (coord.y < minY) minY = coord.y; + if (coord.y > maxY) maxY = coord.y; + }); + + return { + x: maxX - minX, + y: maxY - minY, + }; + }, [blocks]); + + const onValueChange = (value: number[]) => { + setScale(value[0]); + }; + + return ( +
+
+
{Math.floor(scale * 100)}%
+
+ W: {canvasSize.x} + H: {canvasSize.y} +
+
+ +
+ +
+
+ ); +} + +export default CanvasInformation; diff --git a/src/components/CursorInformation.tsx b/src/components/information/Cursor.tsx similarity index 65% rename from src/components/CursorInformation.tsx rename to src/components/information/Cursor.tsx index ab95559..bd74a4a 100644 --- a/src/components/CursorInformation.tsx +++ b/src/components/information/Cursor.tsx @@ -20,12 +20,9 @@ function CursorInformation({ mouseCoords, blocks }: Props) { return (
-
- {block?.name ?? "air"} -
- -
- X: {position.x} +
{block?.name ?? "air"}
+
+ X: {position.x} Y: {position.y}
diff --git a/src/index.css b/src/index.css index 6383cf9..876a9e1 100644 --- a/src/index.css +++ b/src/index.css @@ -1,24 +1,28 @@ -@import url('https://fonts.googleapis.com/css2?family=Geist:wght@100..900&display=swap'); +@import url("https://fonts.googleapis.com/css2?family=Geist:wght@100..900&display=swap"); @tailwind base; @tailwind components; @tailwind utilities; :root { - font-family: Geist, Inter, system-ui, Avenir, Helvetica, Arial, sans-serif; + font-family: Geist, Inter, system-ui, Avenir, Helvetica, Arial, sans-serif; - font-synthesis: none; - text-rendering: optimizeLegibility; - -webkit-font-smoothing: antialiased; - -moz-osx-font-smoothing: grayscale; + font-synthesis: none; + text-rendering: optimizeLegibility; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; } @layer base { - :root { - --radius: 0.5rem; - } + :root { + --radius: 0.5rem; + } } body { - @apply bg-zinc-50 dark:bg-zinc-950 dark:text-zinc-50; -} \ No newline at end of file + @apply bg-zinc-50 dark:bg-zinc-950 dark:text-zinc-50; +} + +.info-child { + @apply bg-white dark:bg-zinc-950 px-2 py-1 rounded shadow-xl w-fit border border-zinc-200 dark:border-zinc-800; +}