Skip to content

Commit

Permalink
Canvas main to test (#5)
Browse files Browse the repository at this point in the history
Canvas main to test
  • Loading branch information
olegsvs authored Oct 1, 2024
2 parents aae48ce + fecc31e commit 355844f
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 72 deletions.
109 changes: 56 additions & 53 deletions src/components/PlayerCanvasBackground/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,77 +6,80 @@ import React, {
useContext,
useMemo,
useState,
} from 'react';
import { useGetCanvasImages } from './queries';
import { Player } from '../../utils/types';
} from 'react'
import { useGetCanvasImages } from './queries'
import { Player } from '../../utils/types'

export type CanvasImage = {
id: number;
rotation: number;
x: number;
y: number;
url: string;
width: number;
height: number;
zIndex: number;
scaleX: number;
scaleY: number;
};
id: number
rotation: number
x: number
y: number
url: string
width: number
height: number
zIndex: number
scaleX: number
scaleY: number
}

type FlipFunction = () => void;
type FlipFunction = () => void

interface ContextType {
images: CanvasImage[];
isEditMode: boolean;
setIsEditMode: Dispatch<SetStateAction<boolean>>;
selectedImage: CanvasImage | null;
setSelectedImage: Dispatch<SetStateAction<CanvasImage | null>>;
player: Player;
flipFunction: FlipFunction | null;
setFlipFunction: (func: FlipFunction | null) => void;
images: CanvasImage[]
isEditMode: boolean
setIsEditMode: Dispatch<SetStateAction<boolean>>
selectedImage: CanvasImage | null
setSelectedImage: Dispatch<SetStateAction<CanvasImage | null>>
player: Player
flipFunction: FlipFunction | null
setFlipFunction: (func: FlipFunction | null) => void
}

const Context = createContext<ContextType | undefined>(undefined);
const Context = createContext<ContextType | undefined>(undefined)

export const PlayerCanvasBackgroundContextProvider = ({ children, player }: {
children: React.ReactNode,
export const PlayerCanvasBackgroundContextProvider = ({
children,
player,
}: {
children: React.ReactNode
player: Player
}) => {
const [isEditMode, setIsEditMode] = useState(false);
const [selectedImage, setSelectedImage] = useState<CanvasImage | null>(null);
const flipFunctionRef = React.useRef<FlipFunction | null>(null);
const [isEditMode, setIsEditMode] = useState(false)
const [selectedImage, setSelectedImage] = useState<CanvasImage | null>(null)
const flipFunctionRef = React.useRef<FlipFunction | null>(null)

const { data } = useGetCanvasImages(player.id);
const { data } = useGetCanvasImages(player.id)

const setFlipFunction = useCallback((func: FlipFunction | null) => {
flipFunctionRef.current = func;
}, []);

const value = useMemo(() => ({
images: data,
isEditMode,
setIsEditMode,
selectedImage,
setSelectedImage,
player,
flipFunction: flipFunctionRef.current,
setFlipFunction: setFlipFunction,
}), [data, isEditMode, selectedImage, player, flipFunctionRef.current, setFlipFunction]);
flipFunctionRef.current = func
}, [])

return (
<Context.Provider value={value}>
{children}
</Context.Provider>
);
};
const value = useMemo(
() => ({
images: data,
isEditMode,
setIsEditMode,
selectedImage,
setSelectedImage,
player,
flipFunction: flipFunctionRef.current,
setFlipFunction: setFlipFunction,
}),
[data, isEditMode, selectedImage, player, setFlipFunction]
)

return <Context.Provider value={value}>{children}</Context.Provider>
}

export const usePlayerCanvasBackgroundContext = () => {
const context = useContext(Context);
const context = useContext(Context)

if (!context) {
throw new Error('usePlayerCanvasBackgroundContext must be used within PlayerCanvasBackgroundContextProvider');
throw new Error(
'usePlayerCanvasBackgroundContext must be used within PlayerCanvasBackgroundContextProvider'
)
}

return context;
};
return context
}
16 changes: 12 additions & 4 deletions src/components/PlayerCanvasBackground/ui/canvas-stage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import { KonvaEventObject } from 'konva/lib/Node'
const URLImage = ({
image,
setImages,
centerX,
}: {
image: CanvasImage
setImages: React.Dispatch<React.SetStateAction<CanvasImage[]>>
centerX: number
}) => {
const { url, id, scaleX, scaleY, zIndex, ...restProps } = image

Expand Down Expand Up @@ -61,7 +63,7 @@ const URLImage = ({
...image,
scaleX: newScaleX,
scaleY: node.scaleY(),
x: node.x(),
x: node.x() - centerX,
y: node.y(),
rotation,
width: node.width(),
Expand All @@ -83,7 +85,7 @@ const URLImage = ({
console.log('handleDragEnd()')
const updatedImage = {
...image,
x: e.target.x(),
x: e.target.x() - centerX,
y: e.target.y(),
}

Expand Down Expand Up @@ -122,7 +124,7 @@ const URLImage = ({
rotation: node.rotation(),
width: Math.abs(Math.max(20, node.width() * transformScaleX)),
height: Math.abs(node.height() * transformScaleY),
x: node.x(),
x: node.x() - centerX,
y: node.y(),
scaleX: newScaleX,
scaleY: newScaleY,
Expand All @@ -141,6 +143,7 @@ const URLImage = ({
ref={imageRef}
image={img}
{...restProps}
x={restProps.x + centerX}
draggable
onClick={() => {
console.log('image clicked', image)
Expand Down Expand Up @@ -235,7 +238,12 @@ export function CanvasStage({
>
<Layer>
{imageList.map((image) => (
<URLImage key={image.id} image={image} setImages={setImageList} />
<URLImage
key={image.id}
image={image}
setImages={setImageList}
centerX={width / 2}
/>
))}
</Layer>
</Stage>
Expand Down
2 changes: 2 additions & 0 deletions src/components/PlayerCanvasBackground/ui/control-buttons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ export function ControlButtons({
disableEditMode = window.confirm('Вы забыли сохранить изображения')
}
setIsEditMode(!disableEditMode)
setSelectedImage(null)
setFlipFunction(null)
}

return (
Expand Down
43 changes: 28 additions & 15 deletions src/components/PlayerCanvasBackground/ui/static-canvas.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from 'react'
import React, { useRef } from 'react'
import { Box } from '@mui/material'
import { usePlayerCanvasBackgroundContext } from '../context'
import { useRefDimensions } from '../use-ref-dimensions'

function rotateImage(
function adjustCoordinates(
x: number,
y: number,
width: number,
Expand Down Expand Up @@ -41,17 +42,13 @@ function rotateImage(

export function StaticCanvas() {
const { images } = usePlayerCanvasBackgroundContext()
const containerRef = useRef<HTMLDivElement>(null)
const dimensions = useRefDimensions(containerRef)

return (
<Box
sx={{
position: 'absolute',
backgroundColor: 'yellow',
zIndex: 0,
}}
>
{images.map((image) => {
const { x, y } = rotateImage(
const renderItems = () => {
if (dimensions.width > 0) {
return images.map((image) => {
const { x, y } = adjustCoordinates(
image.x,
image.y,
image.width,
Expand All @@ -69,16 +66,32 @@ export function StaticCanvas() {
sx={{
position: 'absolute',
top: y,
left: x,
left: dimensions.width / 2 + x,
width: `${Math.abs(image.width)}px`,
height: `${Math.abs(image.height)}px`,
transform: `rotate(${image.rotation}deg) scaleX(${image.scaleX}) scaleY(${image.scaleY})`, //scaleX(${image.scaleX}) // * image.scaleX
transform: `rotate(${image.rotation}deg) scaleX(${image.scaleX}) scaleY(${image.scaleY})`,
userSelect: 'none',
}}
draggable={false}
/>
)
})}
})
}

return null
}

return (
<Box
ref={containerRef}
sx={{
position: 'absolute',
zIndex: 0,
width: '100%',
height: '100%',
}}
>
{renderItems()}
</Box>
)
}

0 comments on commit 355844f

Please sign in to comment.