A powerful web-based image editor built with Lit and Fabric.js
npm install @creaditor/image-editorA powerful, feature-rich web-based image editor built with Lit and Fabric.js. This component provides a complete image editing solution with cropping, text editing, shape drawing, and more.
- š¼ļø Image Upload & Gallery: Upload images or choose from a built-in gallery
- āļø Crop & Resize: Advanced cropping with aspect ratio controls and resize handles
- š Text Editing: Add and edit text with rich formatting options
- š· Shape Drawing: Draw various shapes (rectangles, circles, lines)
- šØ Canvas Manipulation: Resize canvas, zoom, and pan functionality
- š Internationalization: Multi-language support
- š± Responsive Design: Works on desktop and mobile devices
- ā” Web Components: Built with Lit for easy integration
``bash`
npm install @creaditor/image-editor
`html
height="600"
lang="en">
`
`javascript
import '@creaditor/image-editor/my-element.js';
// Get the editor element
const editor = document.querySelector('cdtr-image-editor');
// Set initial image
editor.imageUrl = 'https://example.com/image.jpg';
// Listen for save events
editor.addEventListener('save-image', (event) => {
const { canvas, width, height, imageWidth, imageHeight, cmds } = event.detail;
const image = cmds.getImage();
// Handle the saved image data
});
`
You can control the size of uploaded images using the imageWidth and imageHeight properties:
`html`
height="600"
imageWidth="400"
imageHeight="300"
lang="en">
When specified, uploaded images will be scaled to fit within these dimensions while maintaining their aspect ratio. If not specified, images will be scaled to fit within 80% of the canvas dimensions.
| Property | Type | Default | Description |
|----------|------|---------|-------------|
| width | Number | 800 | Canvas width in pixels |height
| | Number | 600 | Canvas height in pixels |imageWidth
| | Number | undefined | Desired width of uploaded images in pixels |imageHeight
| | Number | undefined | Desired height of uploaded images in pixels |lang
| | String | 'en' | Language code for internationalization |imageUrl
| | String | undefined | Initial image URL to load |galleryImages
| | Array | [] | Array of gallery images |
| Event | Detail | Description |
|-------|--------|-------------|
| save-image | {canvas, width, height, imageWidth, imageHeight, cmds} | Fired when image is saved |image-uploaded
| | {file: File} | Fired when new image is uploaded |canvas-resized
| | {width: number, height: number} | Fired when canvas is resized |
#### addImage(file: File)
Adds an image to the canvas from a File object.
#### loadImageFromUrl(url: string)
Loads an image from a URL.
#### saveImage()save-image
Saves the current canvas as an image and triggers the event.
#### clearCanvas()
Clears the entire canvas.
The component uses CSS custom properties for theming:
`css`
cdtr-image-editor {
--primary-color: #007bff;
--secondary-color: #6c757d;
--background-color: #ffffff;
--border-color: #dee2e6;
--text-color: #212529;
}
`bashInstall dependencies
npm install
$3
`
src/
āāā components/ # UI components
ā āāā canvas.ts # Main canvas component
ā āāā navbar.ts # Top navigation bar
ā āāā sidebar.ts # Sidebar with tools
ā āāā upload-button.ts # File upload component
ā āāā ...
āāā store/ # State management
āāā utils/ # Utility functions
āāā i18n/ # Internationalization
āāā styles.ts # Global styles
`$3
The project uses TypeScript and Rollup for building:
`bash
Development build
npm run buildProduction build with minification
npm run build:prodWatch mode for development
npm run build:watch
`$3
`bash
Run all tests
npm testRun tests in development mode
npm run test:devRun tests in production mode
npm run test:prodWatch mode for tests
npm run test:watch
`Browser Support
- Chrome 80+
- Firefox 75+
- Safari 13+
- Edge 80+
Dependencies
- Lit: Web components framework
- Fabric.js: Canvas manipulation library
- Cropper.js: Image cropping functionality
- TypeScript: Type safety and development experience
Contributing
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`)This project is licensed under the BSD-3-Clause License - see the LICENSE file for details.
- š§ Email: support@creaditor.com
- š Issues: GitHub Issues
- š Documentation: Full Documentation
See CHANGELOG.md for a list of changes and version history.