fix: add more properties to canvas context
This commit is contained in:
parent
3019db6f06
commit
d37574b90f
4 changed files with 30 additions and 24 deletions
|
|
@ -23,17 +23,13 @@ import welcomeBlocksData from "@/data/welcome.json";
|
||||||
PIXI.settings.SCALE_MODE = PIXI.SCALE_MODES.NEAREST;
|
PIXI.settings.SCALE_MODE = PIXI.SCALE_MODES.NEAREST;
|
||||||
|
|
||||||
function Canvas() {
|
function Canvas() {
|
||||||
const { canvasSize, blocks, setBlocks } = useContext(CanvasContext);
|
const { stageSize, canvasSize, blocks, coords, scale, setStageSize, setBlocks, setCoords, setScale } = useContext(CanvasContext);
|
||||||
const { image, imageDimensions } = useContext(ImageContext);
|
const { image, imageDimensions } = useContext(ImageContext);
|
||||||
const { settings } = useContext(SettingsContext);
|
const { settings } = useContext(SettingsContext);
|
||||||
const textures = useContext(TexturesContext);
|
const textures = useContext(TexturesContext);
|
||||||
const { tool, selectedBlock, cssCursor, setTool, setCssCursor } = useContext(ToolContext);
|
const { tool, selectedBlock, cssCursor, setTool, setCssCursor } = useContext(ToolContext);
|
||||||
|
|
||||||
const stageContainerRef = useRef<HTMLDivElement>(null);
|
const stageContainerRef = useRef<HTMLDivElement>(null);
|
||||||
const [stageSize, setStageSize] = useState<Dimension>({ width: 0, height: 0 });
|
|
||||||
|
|
||||||
const [coords, setCoords] = useState<Position>({ x: 0, y: 0 });
|
|
||||||
const [scale, setScale] = useState(1);
|
|
||||||
|
|
||||||
const [mousePosition, setMousePosition] = useState<Position>({ x: 0, y: 0 });
|
const [mousePosition, setMousePosition] = useState<Position>({ x: 0, y: 0 });
|
||||||
const [mouseCoords, setMouseCoords] = useState<Position>({ x: 0, y: 0 });
|
const [mouseCoords, setMouseCoords] = useState<Position>({ x: 0, y: 0 });
|
||||||
|
|
@ -76,7 +72,7 @@ function Canvas() {
|
||||||
y: mousePosition.y - ((mousePosition.y - coords.y) / scale) * newScale,
|
y: mousePosition.y - ((mousePosition.y - coords.y) / scale) * newScale,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
[coords, mousePosition, scale]
|
[coords, mousePosition, scale, setCoords]
|
||||||
);
|
);
|
||||||
|
|
||||||
const updateCssCursor = useCallback(() => {
|
const updateCssCursor = useCallback(() => {
|
||||||
|
|
@ -134,7 +130,7 @@ function Canvas() {
|
||||||
y: Math.floor((mouseY - coords.y) / (16 * scale)),
|
y: Math.floor((mouseY - coords.y) / (16 * scale)),
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
[dragging, coords, scale, tool, onToolUse]
|
[dragging, coords, scale, tool, onToolUse, setCoords]
|
||||||
);
|
);
|
||||||
|
|
||||||
const onMouseDown = useCallback(() => {
|
const onMouseDown = useCallback(() => {
|
||||||
|
|
@ -156,7 +152,7 @@ function Canvas() {
|
||||||
setScale(newScale);
|
setScale(newScale);
|
||||||
zoomToMousePosition(newScale);
|
zoomToMousePosition(newScale);
|
||||||
},
|
},
|
||||||
[scale, zoomToMousePosition]
|
[scale, zoomToMousePosition, setScale]
|
||||||
);
|
);
|
||||||
|
|
||||||
const onClick = useCallback(() => {
|
const onClick = useCallback(() => {
|
||||||
|
|
@ -166,7 +162,7 @@ function Canvas() {
|
||||||
setScale(newScale);
|
setScale(newScale);
|
||||||
zoomToMousePosition(newScale);
|
zoomToMousePosition(newScale);
|
||||||
}
|
}
|
||||||
}, [tool, holdingAlt, scale, zoomToMousePosition]);
|
}, [tool, holdingAlt, scale, zoomToMousePosition, setScale]);
|
||||||
|
|
||||||
const onKeyDown = (e: KeyboardEvent) => {
|
const onKeyDown = (e: KeyboardEvent) => {
|
||||||
switch (e.key) {
|
switch (e.key) {
|
||||||
|
|
@ -267,8 +263,8 @@ function Canvas() {
|
||||||
)}
|
)}
|
||||||
</Stage>
|
</Stage>
|
||||||
|
|
||||||
<CursorInformation mouseCoords={mouseCoords} blocks={blocks} />
|
<CursorInformation mouseCoords={mouseCoords} />
|
||||||
<CanvasInformation scale={scale} setScale={setScale} setCoords={setCoords} canvasSize={canvasSize} stageSize={stageSize} />
|
<CanvasInformation />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,10 @@
|
||||||
import React from "react";
|
import { useContext } from "react";
|
||||||
|
import { CanvasContext } from "@/context/Canvas";
|
||||||
import { Slider } from "@/components/ui/slider";
|
import { Slider } from "@/components/ui/slider";
|
||||||
|
|
||||||
interface Props {
|
function CanvasInformation() {
|
||||||
scale: number;
|
const { stageSize, canvasSize, scale, setCoords, setScale } = useContext(CanvasContext);
|
||||||
setScale: React.Dispatch<React.SetStateAction<number>>;
|
|
||||||
setCoords: React.Dispatch<React.SetStateAction<Position>>;
|
|
||||||
canvasSize: CanvasSize;
|
|
||||||
stageSize: Dimension;
|
|
||||||
}
|
|
||||||
|
|
||||||
function CanvasInformation({ scale, setScale, setCoords, canvasSize, stageSize }: Props) {
|
|
||||||
const onValueChange = (value: number[]) => {
|
const onValueChange = (value: number[]) => {
|
||||||
const newScale = value[0];
|
const newScale = value[0];
|
||||||
setScale(newScale);
|
setScale(newScale);
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,13 @@
|
||||||
import { useEffect, useState } from "react";
|
import { CanvasContext } from "@/context/Canvas";
|
||||||
|
import { useContext, useEffect, useState } from "react";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
mouseCoords: Position;
|
mouseCoords: Position;
|
||||||
blocks: Block[];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function CursorInformation({ mouseCoords, blocks }: Props) {
|
function CursorInformation({ mouseCoords }: Props) {
|
||||||
|
const { blocks } = useContext(CanvasContext);
|
||||||
|
|
||||||
const [position, setPosition] = useState<Position>({ x: 0, y: 0 });
|
const [position, setPosition] = useState<Position>({ x: 0, y: 0 });
|
||||||
const [block, setBlock] = useState<Block>();
|
const [block, setBlock] = useState<Block>();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,13 +5,22 @@ interface Props {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const CanvasContext = createContext({
|
export const CanvasContext = createContext({
|
||||||
|
stageSize: { width: 0, height: 0 } as Dimension,
|
||||||
canvasSize: { minX: 0, minY: 0, maxX: 0, maxY: 0 },
|
canvasSize: { minX: 0, minY: 0, maxX: 0, maxY: 0 },
|
||||||
blocks: [] as Block[],
|
blocks: [] as Block[],
|
||||||
|
coords: { x: 0, y: 0 } as Position,
|
||||||
|
scale: 0,
|
||||||
|
setStageSize: ((size: Dimension) => {}) as React.Dispatch<React.SetStateAction<Dimension>>,
|
||||||
setBlocks: ((blocks: Block[]) => {}) as React.Dispatch<React.SetStateAction<Block[]>>,
|
setBlocks: ((blocks: Block[]) => {}) as React.Dispatch<React.SetStateAction<Block[]>>,
|
||||||
|
setCoords: ((coords: Position) => {}) as React.Dispatch<React.SetStateAction<Position>>,
|
||||||
|
setScale: ((value: number) => {}) as React.Dispatch<React.SetStateAction<number>>,
|
||||||
});
|
});
|
||||||
|
|
||||||
export const CanvasProvider = ({ children }: Props) => {
|
export const CanvasProvider = ({ children }: Props) => {
|
||||||
|
const [stageSize, setStageSize] = useState({ width: 0, height: 0 } as Dimension);
|
||||||
const [blocks, setBlocks] = useState<Block[]>([]);
|
const [blocks, setBlocks] = useState<Block[]>([]);
|
||||||
|
const [coords, setCoords] = useState<Position>({ x: 0, y: 0 });
|
||||||
|
const [scale, setScale] = useState(1);
|
||||||
|
|
||||||
const canvasSize = useMemo(() => {
|
const canvasSize = useMemo(() => {
|
||||||
let minX = Infinity,
|
let minX = Infinity,
|
||||||
|
|
@ -34,5 +43,9 @@ export const CanvasProvider = ({ children }: Props) => {
|
||||||
};
|
};
|
||||||
}, [blocks]);
|
}, [blocks]);
|
||||||
|
|
||||||
return <CanvasContext.Provider value={{ canvasSize, blocks, setBlocks }}>{children}</CanvasContext.Provider>;
|
return (
|
||||||
|
<CanvasContext.Provider value={{ stageSize, canvasSize, blocks, coords, scale, setStageSize, setBlocks, setCoords, setScale }}>
|
||||||
|
{children}
|
||||||
|
</CanvasContext.Provider>
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue