Thanks to visit codestin.com
Credit goes to github.com

Skip to content

crazyay/Canva

Repository files navigation

Canvas Builder API with PDF Export

A full-stack application for creating custom canvas designs with shapes, text, and images, and exporting them as optimized PDF files.

πŸš€ Features

  • Canvas Initialization: Create canvases with custom dimensions
  • Shape Drawing: Add rectangles and circles with customizable colors and strokes
  • Text Support: Add text with various fonts, sizes, and colors
  • Image Upload: Upload and position images on the canvas
  • Live Preview: Real-time preview of your canvas
  • PDF Export: Export your design as a high-quality, optimized PDF
  • Size Optimization: Automatic compression and optimization for smaller PDF files

πŸ› οΈ Technology Stack

Backend

  • Node.js with Express.js
  • canvas - HTML5 Canvas API for Node.js
  • pdfkit - PDF generation
  • sharp - Image optimization
  • multer - File upload handling

Frontend

  • React with Vite
  • Axios - HTTP client
  • CSS3 - Modern styling with gradients and animations

πŸ“ Project Structure

rocketium/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ controllers/
β”‚   β”‚   └── canvasController.js    # Canvas manipulation logic
β”‚   β”œβ”€β”€ middleware/
β”‚   β”‚   └── upload.js              # File upload configuration
β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   └── canvas.js              # API routes
β”‚   β”œβ”€β”€ uploads/                   # Uploaded images storage
β”‚   β”œβ”€β”€ server.js                  # Express server
β”‚   └── package.json
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ App.jsx               # Main React component
β”‚   β”‚   β”œβ”€β”€ App.css               # Styling
β”‚   β”‚   β”œβ”€β”€ main.jsx              # Entry point
β”‚   β”‚   └── index.css             # Global styles
β”‚   β”œβ”€β”€ index.html
β”‚   β”œβ”€β”€ vite.config.js
β”‚   └── package.json
β”œβ”€β”€ vercel.json                    # Vercel deployment config
└── README.md

πŸš€ Getting Started

Prerequisites

  • Node.js (v16 or higher)
  • npm or yarn

Installation

  1. Clone the repository
git clone <your-repo-url>
cd rocketium
  1. Install Backend Dependencies
cd backend
npm install
  1. Install Frontend Dependencies
cd ../frontend
npm install

Running Locally

  1. Start the Backend Server
cd backend
npm start
# Or for development with auto-reload:
npm run dev

Backend will run on http://localhost:5000

  1. Start the Frontend Development Server
cd frontend
npm run dev

Frontend will run on http://localhost:3000

  1. Open your browser and navigate to http://localhost:3000

πŸ“‘ API Endpoints

Initialize Canvas

POST /api/canvas/initialize
Content-Type: application/json

{
  "width": 800,
  "height": 600
}

Add Rectangle

POST /api/canvas/add-rectangle
Content-Type: application/json

{
  "sessionId": "string",
  "x": 50,
  "y": 50,
  "width": 200,
  "height": 100,
  "fillColor": "#3498db",
  "strokeColor": "#2c3e50",
  "strokeWidth": 2
}

Add Circle

POST /api/canvas/add-circle
Content-Type: application/json

{
  "sessionId": "string",
  "x": 400,
  "y": 300,
  "radius": 80,
  "fillColor": "#e74c3c",
  "strokeColor": "#c0392b",
  "strokeWidth": 2
}

Add Text

POST /api/canvas/add-text
Content-Type: application/json

{
  "sessionId": "string",
  "text": "Hello Canvas!",
  "x": 300,
  "y": 500,
  "fontSize": 32,
  "fontFamily": "Arial",
  "color": "#2c3e50"
}

Add Image

POST /api/canvas/add-image
Content-Type: multipart/form-data

{
  "sessionId": "string",
  "image": <file>,
  "x": 550,
  "y": 50,
  "width": 200,
  "height": 150
}

Get Preview

POST /api/canvas/preview
Content-Type: application/json

{
  "sessionId": "string"
}

Export PDF

POST /api/canvas/export-pdf
Content-Type: application/json

{
  "sessionId": "string",
  "optimize": true
}

🎨 Usage Guide

  1. Initialize Canvas: Set your desired canvas dimensions and click "Initialize Canvas"
  2. Add Elements: Use the form controls to add rectangles, circles, text, or images
  3. Preview: See your design update in real-time in the preview panel
  4. Export: Click "Export as PDF" to download your design as an optimized PDF file

πŸ”§ Configuration

Environment Variables (Optional)

Create a .env file in the backend directory:

PORT=5000
NODE_ENV=development

Create a .env file in the frontend directory:

VITE_API_URL=http://localhost:5000/api

πŸ“¦ Deployment

Deploy to Vercel

  1. Install Vercel CLI
npm install -g vercel
  1. Deploy
vercel
  1. Set Environment Variables in Vercel Dashboard if needed

Alternative: Manual Deployment

Backend (e.g., Heroku, Railway)

cd backend
git init
git add .
git commit -m "Initial commit"
# Follow your hosting provider's deployment instructions

Frontend (e.g., Netlify, Vercel)

cd frontend
npm run build
# Deploy the 'dist' folder to your hosting provider

πŸ” Features in Detail

PDF Optimization

The application implements several optimization techniques:

  • Image Compression: Uses Sharp library to compress images before adding to PDF
  • PDF Compression: Enables built-in PDF compression in PDFKit
  • Progressive Rendering: Optimizes PNG images for web delivery
  • Quality Control: Balances quality and file size (80% quality, compression level 9)

Canvas Session Management

  • In-memory session storage for development
  • Each canvas instance gets a unique session ID
  • Session data includes canvas context, dimensions, and element history

πŸ› Troubleshooting

Canvas library installation issues

On Windows, you may need to install build tools:

npm install --global windows-build-tools

Port already in use

Change the port in backend/server.js or set PORT environment variable

CORS issues

Ensure CORS is properly configured in backend/server.js

πŸ“ License

This project is created for educational purposes.

πŸ‘¨β€πŸ’» Author

Built as an assignment project demonstrating full-stack development skills.

πŸ™ Acknowledgments

  • Canvas API - Node Canvas Library
  • PDFKit - PDF generation
  • Sharp - Image processing
  • React & Vite - Frontend framework and build tool

About

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published