Skip to content

Latest commit

 

History

History
433 lines (326 loc) · 12.3 KB

File metadata and controls

433 lines (326 loc) · 12.3 KB

React Image Editor

React Image Editor

MIT License TypeScript React Vite GitHub Stars

A powerful, web-based image editor built with React, TypeScript, and Canvas API

🚀 Live Demo | 📖 Documentation | 🐛 Report Bug | 💡 Request Feature

✨ Features

🖼️ Image Operations

  • Upload & Display: Drag-and-drop or click to upload images (PNG, JPEG, WebP, GIF)
  • Download: Export edited images in high quality
  • Real-time Preview: See changes instantly as you edit

🎨 Filters & Effects

  • Brightness: Adjust image brightness (-100 to +100)
  • Contrast: Enhance or reduce contrast (-100 to +100)
  • Grayscale: Convert to grayscale with intensity control
  • Sepia: Apply vintage sepia tone effect
  • Blur: Add blur effect with adjustable radius
  • Invert: Invert colors for negative effect

🔄 Transformations

  • Rotate: 90°, 180°, 270° rotation
  • Flip: Horizontal and vertical flipping
  • Resize: Custom dimensions with aspect ratio options
  • Crop: Select and crop specific regions (with visual overlay)

🔧 Advanced Features

  • Undo/Redo: Full editing history (up to 50 states)
  • Non-destructive Editing: Original image always preserved
  • Performance Optimized: Efficient Canvas API operations
  • Responsive Design: Works on desktop, tablet, and mobile
  • Keyboard Shortcuts: Ctrl+Z (undo), Ctrl+Y (redo)

🚀 Quick Start

Prerequisites

  • Node.js 18.0+
  • npm 8.0+ or yarn 1.22+

Installation

  1. Clone the repository

    git clone https://github.com/yuis-ice/react-image-editor.git
    cd react-image-editor
  2. Install dependencies

    npm install
    # or
    yarn install
  3. Start development server

    npm run dev
    # or
    yarn dev
  4. Open your browser Navigate to http://localhost:5174

Production Build

npm run build
# or
yarn build

The built files will be in the dist/ directory.

📖 Usage

Basic Workflow

  1. Upload an Image: Click the upload button or drag & drop an image
  2. Apply Filters: Use the filter panel to adjust brightness, contrast, etc.
  3. Transform: Rotate, flip, or resize your image
  4. Crop (if needed): Select the crop tool and drag to select an area
  5. Undo/Redo: Use the history buttons or keyboard shortcuts
  6. Download: Save your edited image

Keyboard Shortcuts

Shortcut Action
Ctrl + Z Undo
Ctrl + Y Redo
Ctrl + S Save/Download
Escape Cancel current operation

🏗️ Architecture

Tech Stack

Technology Purpose Version
React UI Framework 18.0+
TypeScript Type Safety 5.0+
Zustand State Management 4.0+
Canvas API Image Processing Native
Vite Build Tool 5.0+
CSS3 Styling Modern

Project Structure

react-image-editor/
├── 📁 src/
│   ├── 📁 components/          # React components
│   │   ├── CanvasComponent.tsx # Main canvas display
│   │   ├── FilterPanel.tsx     # Filter controls
│   │   ├── TransformPanel.tsx  # Transform controls
│   │   ├── ToolBar.tsx         # Tool selection
│   │   └── FileControls.tsx    # File operations
│   ├── 📁 store/              # Zustand stores
│   │   └── imageEditorStore.ts # Main state store
│   ├── 📁 utils/              # Utility functions
│   │   └── imageProcessor.ts   # Canvas API operations
│   ├── 📁 styles/             # CSS styles
│   └── 📄 App.tsx             # Main application
├── 📁 public/                 # Static assets
├── 📁 .github/                # GitHub templates
└── 📄 package.json            # Dependencies

State Management

The application uses Zustand for state management with the following structure:

interface ImageState {
  // Image data
  originalImage: HTMLImageElement | null;
  currentImageData: ImageData | null;
  
  // Filters
  brightness: number;
  contrast: number;
  grayscale: number;
  sepia: number;
  blur: number;
  invert: boolean;
  
  // Transformations
  rotation: number;
  flipHorizontal: boolean;
  flipVertical: boolean;
  width: number;
  height: number;
  
  // History
  history: ImageState[];
  currentHistoryIndex: number;
  
  // UI state
  selectedTool: Tool;
  cropArea: CropArea | null;
}

🤝 Contributing

We welcome contributions! Please see our Contributing Guide for details.

Quick Contributing Steps

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Development Guidelines

  • Follow the TypeScript style guide
  • Use Canvas API for image operations (avoid external libraries)
  • Write tests for new features
  • Update documentation for API changes
  • Follow the existing code style and patterns

📝 API Reference

Core Functions

imageProcessor.ts

// Apply filters to image
applyFilters(imageData: ImageData, filters: FilterState): ImageData

// Transform image (rotate, flip, resize)
transformImage(image: HTMLImageElement, transforms: TransformState): ImageData

// Crop image to specified area
cropImage(imageData: ImageData, cropArea: CropArea): ImageData

imageEditorStore.ts

// Load new image
loadImage(file: File): void

// Apply filter changes
updateFilter(filterName: string, value: number): void

// Undo/Redo operations
undo(): void
redo(): void

// Save current state to history
saveToHistory(): void

🧪 Testing

# Run all tests
npm test

# Run tests in watch mode
npm run test:watch

# Run tests with coverage
npm run test:coverage

📊 Performance

Benchmarks

Operation Time (1MP image) Time (5MP image) Memory Usage
Filter Application ~50ms ~200ms +10MB
Rotation ~30ms ~120ms +15MB
Crop ~20ms ~80ms -Variable

Optimization Tips

  • Large Images: Consider resizing before editing for better performance
  • Multiple Filters: Apply filters in batch when possible
  • Memory: Use the crop tool to reduce image size for complex operations

🌐 Browser Support

Browser Version Support Level
Chrome 90+ ✅ Full
Firefox 85+ ✅ Full
Safari 14+ ✅ Full
Edge 90+ ✅ Full
Mobile Safari 14+ ⚠️ Limited
Chrome Mobile 90+ ⚠️ Limited

Note: Mobile support is limited due to canvas performance constraints.

📄 License

This project is licensed under the MIT License. See the LICENSE file for details.

🙏 Acknowledgments

  • Canvas API for powerful image manipulation capabilities
  • Zustand for elegant state management
  • Vite for lightning-fast development experience
  • React Team for the amazing framework
  • TypeScript Team for type safety

🔗 Links

📈 Roadmap

  • Advanced Filters: HSL, curves, levels
  • Layer System: Multiple image layers
  • Drawing Tools: Brush, shapes, text
  • Batch Processing: Edit multiple images
  • Plugin System: Extensible architecture
  • Cloud Storage: Save/load from cloud
  • AI Features: Auto-enhance, object removal
  • Mobile App: React Native version

⭐ Star this repo if you find it helpful!

Made with ❤️ by yuis-ice

│ ├── ToolBar.tsx # Tool selection and history controls │ ├── FilterPanel.tsx # Filter sliders and adjustments │ ├── TransformPanel.tsx # Rotation, flip, and resize controls │ └── FileControls.tsx # File upload and download ├── store/ │ └── imageEditorStore.ts # Zustand store definition ├── utils/ │ └── imageProcessor.ts # Canvas API utilities └── App.tsx # Main application component ```

Getting Started

Prerequisites

  • Node.js 18+ (recommended)
  • npm or yarn

Installation

  1. Install dependencies:
npm install
  1. Start the development server:
npm run dev
  1. Open your browser and navigate to http://localhost:5173

Building for Production

npm run build

The built files will be in the dist directory.

Usage

  1. Load an Image: Click "Open Image" to select an image file from your device
  2. Apply Filters: Use the sliders to adjust brightness, contrast, and grayscale
  3. Transform: Rotate or flip your image using the transform controls
  4. Resize: Click "Resize" to change the image dimensions
  5. Undo/Redo: Use the history controls to navigate through your edits
  6. Download: Save your edited image by clicking "Download"

Implementation Details

Filter Operations

Filters are applied using pixel-by-pixel manipulation of ImageData:

  • Brightness: Adds/subtracts values from RGB channels
  • Contrast: Applies contrast formula: factor * (pixel - 128) + 128
  • Grayscale: Converts to grayscale using luminance formula: 0.299*R + 0.587*G + 0.114*B

Transform Operations

Transformations use Canvas 2D context methods:

  • Rotation: Uses context.rotate() with angle conversion
  • Flip: Uses context.scale() with negative values
  • Resize: Uses drawImage() with scaling parameters

History Management

  • Automatic snapshots before significant operations
  • Efficient memory usage with ImageData cloning
  • Configurable history size (default: 50 operations)

Browser Compatibility

  • Chrome 80+
  • Firefox 75+
  • Safari 13+
  • Edge 80+

License

This project is licensed under the MIT License.

Roadmap

  • Advanced cropping tool with visual selection
  • Additional filters (blur, sharpen, sepia)
  • Layer support
  • Batch processing
  • Keyboard shortcuts
  • Export to multiple formats (JPEG, WebP)
  • Performance optimizations for large images
  • Touch/mobile gesture support

You can also install eslint-plugin-react-x and eslint-plugin-react-dom for React-specific lint rules:

// eslint.config.js
import reactX from 'eslint-plugin-react-x'
import reactDom from 'eslint-plugin-react-dom'

export default tseslint.config([
  globalIgnores(['dist']),
  {
    files: ['**/*.{ts,tsx}'],
    extends: [
      // Other configs...
      // Enable lint rules for React
      reactX.configs['recommended-typescript'],
      // Enable lint rules for React DOM
      reactDom.configs.recommended,
    ],
    languageOptions: {
      parserOptions: {
        project: ['./tsconfig.node.json', './tsconfig.app.json'],
        tsconfigRootDir: import.meta.dirname,
      },
      // other options...
    },
  },
])