Create Menus and Bug Fixes (#664)

Removed:
Save and Load Scene for later.
Added undo and redo as button.
Hide Generate Image
Added a menu ( still needs logic code )
Fixed Opacity on brush.

---------

Co-authored-by: CrossPr0duct <alicextensor@gmail.com>
This commit is contained in:
ArEnScandCrossPr0duct authored and GitHub committed 2025-06-09 17:36:16 -04:00
1 parent 8208206e5a
commit 3efe9db4ca
5 files changed
+254 -40

No files matched your search

@@ -16,7 +16,7 @@ import Konva from 'konva';
import { setCanvasRenderBitmap } from "../../signals/canvasRenderBitmap"
import { captureStageImageBitmap } from "./hooks/useUpdateSnapshot"
import { useStateSceneLoader } from "./hooks/useStateSceneLoader";
import { ContextMenuContainer } from "./components/ui/ContextMenu";
const PageDraw = () => {
//useStateSceneLoader();
@@ -142,10 +142,11 @@ const PageDraw = () => {
store.createTriangle(centerX, centerY);
}
}}
onPaintBrush={(hex: string, size: number): void => {
onPaintBrush={(hex: string, size: number, opacity: number): void => {
store.setActiveTool("draw");
store.setBrushColor(hex);
store.setBrushSize(size);
store.setBrushOpacity(opacity);
}}
onEraser={(size: number): void => {
store.setActiveTool("eraser");
@@ -209,6 +210,7 @@ const PageDraw = () => {
activeToolId={store.activeTool}
/>
<div className="relative z-0">
<ContextMenuContainer>
<PaintSurface
nodes={store.nodes}
selectedNodeIds={store.selectedNodeIds}
@@ -224,6 +226,7 @@ const PageDraw = () => {
stageRef={stageRef}
transformerRefs={transformerRefs}
/>
</ContextMenuContainer>
</div>
<div className="absolute bottom-6 left-6 z-20 flex items-center gap-2">
<PopoverMenu
@@ -185,6 +185,14 @@ export const PaintSurface = ({
isWithinLeftPanel(stagePoint)
) {
const lineId = `line-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
let opacity;
if (activeTool === "draw") {
opacity = store.brushOpacity
} else {
opacity = 1
}
const newLineNode: LineNode = {
id: lineId,
type: "line",
@@ -192,6 +200,7 @@ export const PaintSurface = ({
stroke: activeTool === "draw" ? brushColor : fillColor || "#ffffff",
strokeWidth: brushSize / stage.scaleX(),
draggable: true,
opacity: opacity // Add opacity to the line node
};
store.selectNode(null);
store.addLineNode(newLineNode);
@@ -738,11 +747,12 @@ export const PaintSurface = ({
id={lineNode.id}
points={lineNode.points}
stroke={lineNode.stroke}
opacity={lineNode.opacity ?? 1} // Use the line node's opacity or default to 1
strokeWidth={
isSelected
? (lineNode.strokeWidth || 0) + 2
: lineNode.strokeWidth
} // Ensure strokeWidth is number
}
tension={0.5}
lineCap="round"
lineJoin="round"
@@ -0,0 +1,174 @@
import React, { useEffect, useRef } from 'react';
interface MenuPosition {
x: number;
y: number;
}
interface MenuItem {
icon: React.ReactNode;
label: string;
onClick: () => void;
divider?: boolean;
}
interface ContextMenuProps {
items: MenuItem[];
onClose: () => void;
position: MenuPosition;
}
export const ContextMenu: React.FC<ContextMenuProps> = ({ items, onClose, position }) => {
const menuRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (menuRef.current && !menuRef.current.contains(event.target as Node)) {
onClose();
}
};
document.addEventListener('mousedown', handleClickOutside);
return () => document.removeEventListener('mousedown', handleClickOutside);
}, [onClose]);
return (
<div
ref={menuRef}
style={{
left: `${position.x}px`,
top: `${position.y}px`,
}}
className="fixed z-50 min-w-[220px] bg-[#1A1A1A] rounded-lg shadow-xl border border-[#333333] py-1 select-none"
>
{items.map((item, index) => (
<React.Fragment key={index}>
<button
onClick={() => {
item.onClick();
onClose();
}}
className="w-full px-4 py-2.5 flex items-center gap-3 hover:bg-[#333333] text-[#ECECEC] text-[13px] transition-colors duration-75 ease-in-out"
>
<span className="w-5 h-5 flex items-center justify-center text-[#ECECEC]">
{item.icon}
</span>
<span className="font-normal">{item.label}</span>
</button>
{item.divider && <div className="h-[1px] bg-[#333333] mx-2 my-1" />}
</React.Fragment>
))}
</div>
);
};
// Example usage with icons
const menuItems: MenuItem[] = [
{
icon: <svg className="w-4 h-4" viewBox="0 0 24 24" fill="none" stroke="currentColor">
<path d="M12 3L4 7.5L12 12L20 7.5L12 3Z" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
<path d="M4 16.5L12 21L20 16.5" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
<path d="M4 12L12 16.5L20 12" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
</svg>,
label: 'Convert to 3D',
onClick: () => console.log('Convert to 3D')
},
{
icon: <svg className="w-4 h-4" viewBox="0 0 24 24" fill="none" stroke="currentColor">
<path d="M4 4v16h16M4 20l16-16" strokeWidth="2" strokeLinecap="round"/>
</svg>,
label: 'Remove background',
onClick: () => console.log('Remove background')
},
{
icon: <svg className="w-4 h-4" viewBox="0 0 24 24" fill="none" stroke="currentColor">
<path d="M12 10V14M8 6H16C17.1046 6 18 6.89543 18 8V16C18 17.1046 17.1046 18 16 18H8C6.89543 18 6 17.1046 6 16V8C6 6.89543 6.89543 6 8 6Z" strokeWidth="2" strokeLinecap="round"/>
</svg>,
label: 'Lock',
onClick: () => console.log('Lock'),
divider: true
},
{
icon: <svg className="w-4 h-4" viewBox="0 0 24 24" fill="none" stroke="currentColor">
<path d="M5 15L12 8L19 15" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
</svg>,
label: 'Bring to front',
onClick: () => console.log('Bring to front')
},
{
icon: <svg className="w-4 h-4" viewBox="0 0 24 24" fill="none" stroke="currentColor">
<path d="M8 14L12 10L16 14" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
</svg>,
label: 'Bring forward',
onClick: () => console.log('Bring forward')
},
{
icon: <svg className="w-4 h-4" viewBox="0 0 24 24" fill="none" stroke="currentColor">
<path d="M8 10L12 14L16 10" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
</svg>,
label: 'Send backward',
onClick: () => console.log('Send backward')
},
{
icon: <svg className="w-4 h-4" viewBox="0 0 24 24" fill="none" stroke="currentColor">
<path d="M5 9L12 16L19 9" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
</svg>,
label: 'Send to back',
onClick: () => console.log('Send to back'),
divider: true
},
{
icon: <svg className="w-4 h-4" viewBox="0 0 24 24" fill="none" stroke="currentColor">
<path d="M16 8V6M16 8H8M16 8V16M8 8V6M8 8V16M8 16H6M8 16H16M16 16H18M6 6H8M8 6H16M16 6H18" strokeWidth="2" strokeLinecap="round"/>
</svg>,
label: 'Duplicate',
onClick: () => console.log('Duplicate')
},
{
icon: <svg className="w-4 h-4" viewBox="0 0 24 24" fill="none" stroke="currentColor">
<path d="M7 16L17 8M7 8L17 16" strokeWidth="2" strokeLinecap="round"/>
</svg>,
label: 'Flip Horizontal',
onClick: () => console.log('Flip Horizontal')
},
{
icon: <svg className="w-4 h-4" viewBox="0 0 24 24" fill="none" stroke="currentColor">
<path d="M8 7L16 17M8 17L16 7" strokeWidth="2" strokeLinecap="round"/>
</svg>,
label: 'Flip Vertical',
onClick: () => console.log('Flip Vertical')
},
{
icon: <svg className="w-4 h-4" viewBox="0 0 24 24" fill="none" stroke="currentColor">
<path d="M4 12V6C4 4.89543 4.89543 4 6 4H18C19.1046 4 20 4.89543 20 6V18C20 19.1046 19.1046 20 18 20H12" strokeWidth="2" strokeLinecap="round"/>
<path d="M9 15H4V20" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
</svg>,
label: 'Fit to screen',
onClick: () => console.log('Fit to screen')
}
];
// Container component that handles the right-click
export const ContextMenuContainer: React.FC<{ children: React.ReactNode }> = ({ children }) => {
const [showMenu, setShowMenu] = React.useState(false);
const [position, setPosition] = React.useState<MenuPosition>({ x: 0, y: 0 });
const handleContextMenu = (e: React.MouseEvent) => {
e.preventDefault();
setPosition({ x: e.clientX, y: e.clientY });
setShowMenu(true);
};
return (
<div onContextMenu={handleContextMenu} className="w-full h-full">
{children}
{showMenu && (
<ContextMenu
items={menuItems}
position={position}
onClose={() => setShowMenu(false)}
/>
)}
</div>
);
};
@@ -13,6 +13,8 @@ import {
faSparkles,
faFileImport,
faFileExport,
faUndo,
faRedo
} from "@fortawesome/pro-solid-svg-icons";
import "../../App.css";
import { HsvaColorPicker, HsvaColor } from "react-colorful";
@@ -40,7 +42,7 @@ function useDebounced<T extends (...args: A) => void, A extends unknown[]>(
export interface SideToolbarProps {
onSelect: () => void;
onAddShape: (shape: "rectangle" | "circle" | "triangle") => void;
onPaintBrush: (hex: string, size: number) => void;
onPaintBrush: (hex: string, size: number,opacity: number) => void;
onEraser: (size: number) => void;
onCanvasBackground: (hex: string) => void;
onGenerateImage: () => void;
@@ -72,6 +74,7 @@ const SideToolbar: React.FC<SideToolbarProps> = ({
v: 100,
a: 1,
});
const [bgHsva, setBgHsva] = useState<HsvaColor>({
h: 0,
s: 100,
@@ -81,9 +84,11 @@ const SideToolbar: React.FC<SideToolbarProps> = ({
/* debounced parent calls */
const sendPaint = useDebounced<
(hex: string, size: number) => void,
[string, number]
>(onPaintBrush, 75);
(hex: string, size: number, opacity: number) => void,
[string, number, number]
>(onPaintBrush, 75, 1);
const sendBg = useDebounced<(hex: string) => void, [string]>(
onCanvasBackground,
75,
@@ -117,7 +122,8 @@ const SideToolbar: React.FC<SideToolbarProps> = ({
const BrushPopout = makePicker(
brushHsva,
setBrushHsva,
(hex) => sendPaint(hex, brushSize),
(hex) => sendPaint(hex, brushSize, brushHsva.a),
<>
<div className="relative">
<p className="mb-2 text-sm font-medium text-white">Brush Size</p>
@@ -125,7 +131,7 @@ const SideToolbar: React.FC<SideToolbarProps> = ({
value={brushSize}
onChange={(size) => {
setBrushSize(size);
sendPaint(hsvaToHex(brushHsva), size);
sendPaint(hsvaToHex(brushHsva), size, brushHsva.a);
}}
min={1}
max={64}
@@ -179,14 +185,14 @@ const SideToolbar: React.FC<SideToolbarProps> = ({
</div>
),
},
{
id: "generate",
label: "Generate Image",
icon: <FontAwesomeIcon icon={faSparkles} className="h-5 w-5" />,
onClick: () => {
onGenerateImage();
},
},
// {
// id: "generate",
// label: "Generate Image",
// icon: <FontAwesomeIcon icon={faSparkles} className="h-5 w-5" />,
// onClick: () => {
// onGenerateImage();
// },
// },
{
id: "upload",
label: "Upload Image",
@@ -206,7 +212,7 @@ const SideToolbar: React.FC<SideToolbarProps> = ({
/>
),
onClick: () => {
sendPaint(hsvaToHex(brushHsva), brushSize);
sendPaint(hsvaToHex(brushHsva), brushSize, brushHsva.a);
},
popout: BrushPopout,
},
@@ -251,35 +257,51 @@ const SideToolbar: React.FC<SideToolbarProps> = ({
popout: BgPopout,
},
{
id: "save-scene",
label: "Save Scene",
icon: <FontAwesomeIcon icon={faFileExport} className="h-5 w-5" />,
id: "undo",
label: "Undo",
icon: <FontAwesomeIcon icon={faUndo} className="h-5 w-5" />,
onClick: () => {
store.saveSceneToFile();
store.undo(); // Assuming store has an undo method
},
},
{
id: "load-scene",
label: "Load Scene",
icon: <FontAwesomeIcon icon={faFileImport} className="h-5 w-5" />,
id: "redo",
label: "Redo",
icon: <FontAwesomeIcon icon={faRedo} className="h-5 w-5" />,
onClick: () => {
const input = document.createElement("input");
input.type = "file";
input.accept = ".json";
input.onchange = async (e: Event) => {
const target = e.target as HTMLInputElement;
if (target.files && target.files[0]) {
const success = await store.loadSceneFromFile(target.files[0]);
if (success) {
console.log("Scene loaded successfully");
} else {
console.error("Failed to load scene");
}
}
};
input.click();
store.redo(); // Assuming store has a redo method
},
},
// {
// id: "save-scene",
// label: "Save Scene",
// icon: <FontAwesomeIcon icon={faFileExport} className="h-5 w-5" />,
// onClick: () => {
// store.saveSceneToFile();
// },
// },
// {
// id: "load-scene",
// label: "Load Scene",
// icon: <FontAwesomeIcon icon={faFileImport} className="h-5 w-5" />,
// onClick: () => {
// const input = document.createElement("input");
// input.type = "file";
// input.accept = ".json";
// input.onchange = async (e: Event) => {
// const target = e.target as HTMLInputElement;
// if (target.files && target.files[0]) {
// const success = await store.loadSceneFromFile(target.files[0]);
// if (success) {
// console.log("Scene loaded successfully");
// } else {
// console.error("Failed to load scene");
// }
// }
// };
// input.click();
// },
// },
];
/* ------------------------------------------------ render ---------- */
@@ -9,6 +9,7 @@ export type LineNode = {
stroke: string;
strokeWidth: number;
draggable: boolean;
opacity?: number; // Add opacity property
x?: number;
y?: number;
rotation?: number;
@@ -33,6 +34,7 @@ interface SceneState {
activeTool: ActiveTool;
brushColor: string;
brushSize: number;
brushOpacity: number;
fillColor: string;
// Cursor state
@@ -84,6 +86,7 @@ interface SceneState {
// Toolbar actions
setActiveTool: (tool: ActiveTool) => void;
setBrushColor: (color: string) => void;
setBrushOpacity: (opacity: number) => void;
setBrushSize: (size: number) => void;
setFillColor: (color: string) => void;
@@ -122,6 +125,7 @@ export const useSceneStore = create<SceneState>((set, get) => ({
activeTool: 'select',
brushColor: '#000000',
brushSize: 5,
brushOpacity: 1,
fillColor: 'white',
// Cursor initial state
@@ -719,6 +723,7 @@ export const useSceneStore = create<SceneState>((set, get) => ({
setBrushColor: (color: string) => set({ brushColor: color }),
setBrushSize: (size: number) => set({ brushSize: size }),
setFillColor: (color: string) => set({ fillColor: color }),
setBrushOpacity: (opacity: number) => set({ brushOpacity: opacity }),
// Cursor actions
setCursorPosition: (position: { x: number; y: number } | null) => set({ cursorPosition: position }),