A dynamic, interactive web application that creates procedurally generated art in real-time using HTML5 Canvas and JavaScript.
This project creates an evolving digital canvas where geometric shapes (lines and rectangles) are randomly generated with varying properties such as position, size, color, and stroke width. The result is a continuously developing abstract composition that's unique every time.
- Procedural Generation: Shapes are created with randomized properties
- Mixed Geometry: Alternates between lines and rectangles
- Color Variety: Each shape has random fill and stroke colors
- Size Variation: Shapes are generated with varying dimensions and line weights
- Start/Stop: Toggle the generation process on and off
- Snapshot: Capture the current state of the canvas as a PNG image
- Responsive UI: Control buttons adapt to different screen sizes
- Right-aligned on desktop/wide screens
- Centered on mobile/smaller screens
The application initializes an HTML5 Canvas element as the drawing surface:
const canvas = document.getElementById('artboard');
const ctx = canvas.getContext('2d');
canvas.width = 620;
canvas.height = 540;
Two primary shape classes handle the creation of random elements:
-
Line Class
- Generates lines with random start/end points and line widths
- Each line has a unique position and appearance
-
Rectangle Class
- Creates rectangles with random position, size, fill color, stroke color, and line width
- Combines filled rectangles with outlined strokes for visual interest
The drawing process can be controlled through:
function startDrawing() {
if (!isDrawing) {
isDrawing = true;
drawingInterval = setInterval(() => {
// Random shape generation logic
}, 1000);
}
}
function stopDrawing() {
if (isDrawing) {
isDrawing = false;
clearInterval(drawingInterval);
}
}
The application includes the ability to save the current canvas state:
function captureSnapshot(canvas) {
const dataURL = canvas.toDataURL('image/png');
const link = document.createElement('a');
link.href = dataURL;
// Generate a filename with timestamp
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
link.download = `generative-art-${timestamp}.png`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
The UI features a control panel with three buttons:
- Start Button (Green): Begins the generation process
- Stop Button (Red): Pauses the generation process
- Snapshot Button (Blue): Captures and downloads the current canvas state
The control panel is styled with CSS to be visually appealing and responsive:
.controls-container {
position: fixed;
bottom: 30px;
right: 30px;
/* Additional styling */
}
/* Media query for smaller screens */
@media (max-width: 768px) {
.controls-container {
right: auto;
left: 50%;
transform: translateX(-50%);
justify-content: center;
}
}
- Open the application in a web browser
- The generation process starts automatically
- Use the Stop button to pause generation
- Use the Start button to resume generation
- Click the Take Snapshot button to save the current artwork as a PNG file
/
├── index.html # Main HTML document
├── README.md # This documentation file
└── artlab/
├── script.js # Main application logic
├── snapshot.js # Image capture functionality
└── style.css # Styling for the application
Potential improvements for future versions:
- Additional shape types (circles, triangles, polygons)
- User-configurable generation parameters (speed, density, color palette)
- Animation effects for shapes
- Gallery of saved snapshots
- Social sharing capabilities
This project explores the intersection of programming and visual art, demonstrating how simple algorithms can create complex and aesthetically pleasing results.