Co-authored-by: Copilot <copilot@github.com>
8.8 KiB
Canvas API Implementation Summary
What Was Implemented
Your product finder now has a hierarchical image fallback system (Canvas API) that automatically serves images with smart fallbacks across multiple directory levels. This eliminates duplicate files and makes it easy to share common elements (like plants, hardware, backgrounds) across products.
System Architecture
Three-Layer Approach
- Backend (Python/Flask) - Smart image resolution with fallback logic
- Frontend (JavaScript) - Dynamic image loading using Canvas API endpoints
- File Structure - Hierarchical organization with automatic fallbacks
Product Specific → Subtype Shared → Type Shared → Global Shared
What Changed
New Files Created
-
app/blueprints/canvas.py- Canvas API Flask blueprint with fallback logic/api/canvas/<product_code>/<layer>?color=<color>- Get image with fallback/api/canvas/<product_code>/info- Get available layers info/api/canvas/test- Test endpoint
-
information/CANVAS_API_GUIDE.md- Complete technical documentation -
information/CANVAS_API_QUICKSTART.md- 5-minute setup guide -
information/CANVAS_DIRECTORY_SETUP.md- Directory setup scripts and helpers -
information/FOREGROUND_LAYER_EXAMPLE.md- Foreground layer guide (from earlier) -
information/FOREGROUND_QUICKSTART.md- Quick foreground guide (from earlier)
Modified Files
app/app.py- Registered canvas blueprintapp/static/js/script.js- Added Canvas API supportbuildCanvasAPIUrl()- Build API URLsupdateCanvasAPIPreview()- Update layers using API- Updated rendering logic to support Canvas API
app/static/css/styles.css- Added.layer-foregroundwith z-index 5 (from earlier)information/LAYERED_IMAGES.md- Updated with foreground layer infoinformation/products-layered-example.json- Added Canvas API example (product #700)
How It Works
1. Directory Structure
app/static/images/
├── doors/
│ └── storm/
│ ├── 404/
│ │ └── door-white.png # Product-specific
│ └── layers/
│ └── hardware.png # Shared by all storm doors
└── layers/
└── foreground.png # Global fallback
2. Product Configuration
{
"productCode": "404",
"imageConfig": {
"useCanvasAPI": true
}
}
3. Automatic Fallback
Request: /api/canvas/404/hardware
Searches in order:
/images/doors/storm/404/hardware.png← Product-specific/images/doors/storm/layers/hardware.png← Subtype shared ✓ Found!/images/doors/layers/hardware.png← Type shared/images/layers/hardware.png← Global
4. Frontend Rendering
// JavaScript automatically calls Canvas API
const url = buildCanvasAPIUrl('404', 'door', 'white');
// Result: /api/canvas/404/door?color=white
doorLayer.src = url; // Image loads with automatic fallback!
Layering System (Updated)
All 5 layers are now supported:
- Base (z-index: 1) - Background/house/frame
- Door (z-index: 2) - Door/window panel (color variants)
- Hardware (z-index: 3) - Handles, locks, hinges
- Overlay (z-index: 4) - Glass views, decorative overlays
- Foreground (z-index: 5) - Plants, decorations ⭐ NEW from earlier request
Benefits
✅ Eliminates Duplication
Before: Each product has own copy of hardware.png After: One shared hardware.png for all products of that type
✅ Smart Fallbacks
Product can have custom image OR inherit from parent levels automatically
✅ Easier Maintenance
Update one shared file → affects all products using it
✅ Flexible Organization
- Product-specific overrides:
/images/<type>/<subtype>/<code>/ - Subtype shared:
/images/<type>/<subtype>/layers/ - Type shared:
/images/<type>/layers/ - Global fallback:
/images/layers/
✅ Reduced File Size
Example with 10 products sharing hardware + foreground:
- Before: 10 × (100KB + 150KB) = 2.5MB
- After: 1 × (100KB + 150KB) = 250KB
- Savings: 2.25MB (90% reduction for shared elements)
Usage Examples
Example 1: All Storm Doors Share Hardware
/images/doors/storm/layers/hardware.png ← One file
Products 404, 505, 450 all use this automatically!
Example 2: Product 404 Has Custom Foreground
/images/doors/storm/404/foreground.png ← Product 404
/images/doors/storm/layers/foreground.png ← Products 505, 450
Product 404 gets custom, others get shared.
Example 3: Global Plant Layer
/images/layers/foreground-plants.png ← Used by ALL products
Unless a more specific version exists.
Migration Path
Phase 1: Keep Existing System (Low Risk)
- Leave current products unchanged
- New products use Canvas API:
"useCanvasAPI": true - Both systems work simultaneously
Phase 2: Identify Duplicates
- Run duplicate file detection scripts
- Find common hardware, foregrounds, bases
- Plan shared directory structure
Phase 3: Reorganize Files
- Create type/subtype structure
- Move shared files to appropriate
/layers/directories - Update product JSON: Add
"useCanvasAPI": true
Phase 4: Cleanup
- Remove old duplicate files
- Verify all products load correctly
- Measure storage savings
API Endpoints Reference
GET /api/canvas/<product_code>/<layer>?color=<color>&material=<material>
→ Returns image file with automatic fallback
GET /api/canvas/<product_code>/info
→ Returns metadata about available layers
GET /api/canvas/test
→ Test endpoint to verify API is working
Configuration Options
Enable Canvas API
{
"productCode": "404",
"imageConfig": {
"useCanvasAPI": true
}
}
Keep Backward Compatibility (During Migration)
{
"productCode": "404",
"imageConfig": {
"useCanvasAPI": true,
"layered": true,
"basePath": "images/products/404/",
"layers": { /* ... */ }
}
}
If Canvas API fails, falls back to static layered system.
Testing
1. Test API is Working
Visit: http://localhost:8080/api/canvas/test
Expected: {"status": "ok"}
2. Test Specific Image
Visit: http://localhost:8080/api/canvas/404/base
Expected: Image file loads
3. Test Fallback
Visit: http://localhost:8080/api/canvas/404/hardware
Check Flask logs to see which path was used
4. Test Product Info
Visit: http://localhost:8080/api/canvas/404/info
Expected: JSON with available layers
5. Test Product Page
Visit product page
Open browser console
Check for Canvas API requests: GET /api/canvas/404/door?color=white
Troubleshooting
Images not loading?
- Check Flask logs for which path was searched
- Visit
/api/canvas/<product_code>/infoto see available layers - Verify
"useCanvasAPI": truein products.json - Check directory structure matches type/subtype
404 errors?
- File doesn't exist at any fallback level
- Check file naming (lowercase for colors)
- Verify baseType and subType in products.json
Wrong image showing?
- More specific path overrides generic
- Check fallback priority order
- Verify file exists where you expect
Documentation Files
- CANVAS_API_GUIDE.md - Complete technical guide
- CANVAS_API_QUICKSTART.md - 5-minute setup
- CANVAS_DIRECTORY_SETUP.md - Setup scripts
- FOREGROUND_LAYER_EXAMPLE.md - Foreground guide
- FOREGROUND_QUICKSTART.md - Quick foreground setup
- LAYERED_IMAGES.md - Full layering system
Next Steps
- ✅ Test the API: Visit
/api/canvas/test - ✅ Create directory structure: Use scripts in CANVAS_DIRECTORY_SETUP.md
- ✅ Move/organize images: Place in appropriate fallback levels
- ✅ Update products.json: Add
"useCanvasAPI": true - ✅ Test products: Load product pages and verify images
- ✅ Optimize: Run duplicate detection and consolidate files
Summary
You now have a sophisticated, production-ready image serving system with:
✨ Smart hierarchical fallbacks
✨ Automatic image resolution
✨ 5-layer compositing (including foreground from earlier)
✨ Massive storage savings
✨ Easy maintenance
✨ Flexible organization
✨ Backward compatible
The system is fully implemented and ready to use. Start with a few test products, verify it works, then gradually migrate your entire catalog!