A interactive React application that allows users to create, manipulate, and manage rectangles on a canvas with full undo/redo functionality.
- Create Rectangles: Generate new rectangles with random colors, positions, and sizes
- Delete Rectangles: Remove selected rectangles from the canvas
- Drag & Drop: Move rectangles around the canvas by dragging
- Resize: Resize rectangles using corner handles when selected
- Selection: Click to select/deselect rectangles with visual feedback
- Undo/Redo: Full history management with undo and redo capabilities
- Visual Selection: Selected rectangles display a blue border and resize handles
- Resize Handles: Four corner handles (top-left, top-right, bottom-left, bottom-right) with appropriate cursors
- Minimum Size Constraints: Rectangles maintain minimum dimensions (50x50px)
- Click Outside to Deselect: Automatic deselection when clicking outside rectangles
- React 18 with TypeScript
- Zustand for state management
- CSS Modules for styling
- Vite for build tooling
- clsx for conditional CSS classes
src/
├── component/
│ ├── ButtonList/ # Control buttons (Create, Delete, Undo, Redo)
│ ├── Canvas/ # Main drawing area
│ ├── Playground/ # Main container component
│ └── Rectangle/ # Individual rectangle component
├── store/
│ └── rectangleStore.ts # Zustand store for state management
├── types.ts # TypeScript type definitions
├── utils.ts # Utility functions
├── constant.ts # Application constants
└── App.tsx # Root component
- Handles mouse interactions for dragging and resizing
- Manages selection state and visual feedback
- Provides corner resize handles with proper cursor indicators
- Enforces minimum size constraints during resize operations
- Provides Create, Delete, Undo, and Redo buttons
- Buttons are disabled when actions are not available
- Each button has distinct styling for easy identification
- Renders all rectangles in a relative positioned container
- Manages rectangle selection state
- Handles rectangle updates from drag/resize operations
- Manages rectangle state with Map-based storage
- Implements command pattern for undo/redo functionality
- Maintains action history for state management
- Supports append, remove, and update operations
The application uses a sophisticated state management system with:
- Rectangle Map: Efficient storage using
Map<number, RectState> - Action History: Array of actions for undo/redo functionality
- History Index: Tracks current position in history
- Command Pattern: Actions are reversible for proper undo/redo
append: Add new rectangleremove: Delete existing rectangleupdate: Modify rectangle properties (position, size)
# Install dependencies
npm install
# Start development server
npm run dev
# Build for production
npm run build
# Preview production build
npm run preview- Creating Rectangles: Click the "Create" button to add a new rectangle with random properties
- Selecting Rectangles: Click on any rectangle to select it (blue border appears)
- Moving Rectangles: Drag selected rectangles to new positions
- Resizing Rectangles: Use the corner handles on selected rectangles to resize
- Deleting Rectangles: Select a rectangle and click the "Delete" button
- Undo/Redo: Use the respective buttons to navigate through action history
- Efficient Map-based rectangle storage
- Minimal re-renders through proper state management
- Optimized event handling with proper cleanup
- Smooth animations and transitions
- Intuitive mouse interactions
- Visual feedback for all interactions
- Proper cursor indicators for different operations
- TypeScript for type safety
- Modular component architecture
- CSS Modules for scoped styling
- Proper separation of concerns
Modern browsers supporting:
- ES6+ features
- CSS Grid/Flexbox
- Mouse events
- React 19
Potential improvements could include:
- Multi-selection support
- Copy/paste functionality
- Keyboard shortcuts
- Export/import capabilities
- Grid snapping
- Color picker for rectangles
- Layer management (z-index controls)
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is open source and available under the MIT License.