How mirayatech/ninjasketch works
Main Components and Layers
• User Interface Layer:
• The React components found under src/components (action-bar, control-panel, info) form the primary visual and interactive elements. These components provide the tools (pencil, line, rectangle, text) and UI elements for users to interact with.
• The main canvas area is rendered in src/App.tsx (and indirectly in other components) and is where drawing elements appear.
• Business Logic / State Management:
• The custom hooks (useHistory and usePressedKeys in src/hooks) are responsible for state management, including modification history (undo/redo) and handling keyboard events.
• External Libraries and Frameworks:
• Rough.js is integrated for achieving the sketchy, hand-drawn rendering style.
• Vite (see vite.config.ts) is used as the build tool, and the project leverages TypeScript for type safety.
• Testing frameworks like Cypress (structure under cypress directory) and Vitest or Testing Library are included for end-to-end and unit testing.
Relationships and Interactions
• User Interactions (via mouse, keyboard, and scroll events) are captured by the UI components and forwarded to the state management hooks. The useHistory hook, for instance, helps manage the drawing’s state for undo/redo capabilities.
• The drawing tools (action-bar/control-panel selections) modify the application state, which then triggers updates in the canvas rendering.
• Utility functions process the adjustments such as calculating coordinates, detecting proximity to points, and resizing logic. They act as the computational intermediary between raw user interaction data and the rendered output.
Architectural Patterns & Design Principles
• Component-Based Architecture: The clear separation into React components facilitates isolation of UI concerns. Grouping of related components (action-bar, control-panel, info) suggests a modular design.
• Separation of Concerns: UI rendering, state management (via hooks), and drawing logic (through utilities) are distinct, thereby making the system easier to reason about and maintain.
• Layered Structure: Your design distinguishes between presentation (React components), business logic (hooks and utilities), and integration with external libraries (Rough.js, Vite) while testing is isolated (Cypress).
• Emphasis on Event-Driven Interaction: The usage of custom hooks for history and pressed keys underline an event-driven approach to handling user interactions.