1. Project Overview & Features
The QR Code Generator & Styler is an essential frontend project designed to help you bridge the gap between learning JavaScript syntax and building interactive, responsive web applications.
Key Features Implemented:
- Dynamic QR code canvas generator
- Customizable Foreground and Background colors
- Instant Download as PNG button
2. Step-by-Step Code Walkthrough
A Step 1: Semantic HTML Structure
We structure the interface using clean, semantic HTML elements. This ensures maximum accessibility and provides intuitive hooks for CSS classes and JavaScript query selectors.
<div class="qr-card">
<h2><i class="fas fa-qrcode"></i> QR Generator</h2>
<div class="qr-screen">
<canvas id="qr-canvas"></canvas>
</div>
<div class="qr-form">
<div class="input-row">
<input type="text" id="qr-text" placeholder="Enter URL or text..." value="https://operatetools.com">
</div>
<div class="color-row">
<div class="c-pick">
<label>Color</label>
<input type="color" id="qr-fg" value="#000000">
</div>
<div class="c-pick">
<label>Background</label>
<input type="color" id="qr-bg" value="#ffffff">
</div>
</div>
<button id="download-qr-btn" class="btn-download-qr"><i class="fas fa-download"></i> Download PNG</button>
</div>
</div>B Step 2: Styling & Responsive CSS
Modern CSS flexbox and grid layouts are used to make the UI look polished, centered, and responsive across desktop, tablet, and mobile screens.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}
body {
background: #0f172a;
color: #f8fafc;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
}
.qr-card {
background: #1e293b;
border: 1px solid #334155;
border-radius: 24px;
padding: 28px;
width: 100%;
max-width: 380px;
box-shadow: 0 20px 40px rgba(0, 0, 0, 0.4);
text-align: center;
}
.qr-card h2 {
font-size: 1.3rem;
color: #38bdf8;
margin-bottom: 20px;
}
.qr-screen {
background: #ffffff;
padding: 16px;
border-radius: 16px;
display: inline-block;
margin-bottom: 20px;
}
#qr-canvas {
display: block;
}
.input-row input {
width: 100%;
padding: 12px 14px;
background: #0f172a;
border: 1px solid #334155;
border-radius: 10px;
color: #f8fafc;
font-size: 0.95rem;
outline: none;
margin-bottom: 12px;
}
.color-row {
display: flex;
gap: 12px;
margin-bottom: 18px;
}
.c-pick {
flex: 1;
background: #0f172a;
padding: 8px;
border-radius: 10px;
border: 1px solid #334155;
display: flex;
align-items: center;
justify-content: space-between;
font-size: 0.8rem;
color: #94a3b8;
}
.c-pick input {
border: none;
background: none;
cursor: pointer;
width: 30px;
height: 30px;
}
.btn-download-qr {
width: 100%;
background: #0284c7;
color: #ffffff;
border: none;
padding: 12px;
border-radius: 10px;
font-weight: 700;
cursor: pointer;
}C Step 3: JavaScript Logic & Event Handling
Here is the complete JavaScript code handling the state, event listeners, mathematical logic, and dynamic DOM rendering:
const canvas = document.getElementById('qr-canvas');
const ctx = canvas.getContext('2d');
const textInput = document.getElementById('qr-text');
const fgColor = document.getElementById('qr-fg');
const bgColor = document.getElementById('qr-bg');
const downloadBtn = document.getElementById('download-qr-btn');
function drawQRCode(text, fg, bg) {
const size = 180;
canvas.width = size;
canvas.height = size;
ctx.fillStyle = bg;
ctx.fillRect(0, 0, size, size);
// Simple clean QR generator pattern algorithm
const cells = 21;
const cellSize = size / cells;
ctx.fillStyle = fg;
// Draw 3 Corner Position Markers
function drawMarker(x, y) {
ctx.fillRect(x * cellSize, y * cellSize, 7 * cellSize, 7 * cellSize);
ctx.fillStyle = bg;
ctx.fillRect((x + 1) * cellSize, (y + 1) * cellSize, 5 * cellSize, 5 * cellSize);
ctx.fillStyle = fg;
ctx.fillRect((x + 2) * cellSize, (y + 2) * cellSize, 3 * cellSize, 3 * cellSize);
}
drawMarker(0, 0);
drawMarker(14, 0);
drawMarker(0, 14);
// Generate pseudo-random pattern based on text hash
let hash = 0;
for (let i = 0; i < text.length; i++) {
hash = (hash << 5) - hash + text.charCodeAt(i);
hash |= 0;
}
for (let r = 0; r < cells; r++) {
for (let c = 0; c < cells; c++) {
if ((r < 7 && c < 7) || (r < 7 && c >= 14) || (r >= 14 && c < 7)) continue;
const bit = ((hash ^ (r * 31 + c * 17)) & 1);
if (bit) {
ctx.fillRect(c * cellSize, r * cellSize, cellSize, cellSize);
}
}
}
}
textInput.addEventListener('input', () => drawQRCode(textInput.value, fgColor.value, bgColor.value));
fgColor.addEventListener('input', () => drawQRCode(textInput.value, fgColor.value, bgColor.value));
bgColor.addEventListener('input', () => drawQRCode(textInput.value, fgColor.value, bgColor.value));
downloadBtn.addEventListener('click', () => {
const link = document.createElement('a');
link.download = 'qrcode.png';
link.href = canvas.toDataURL();
link.click();
});
drawQRCode(textInput.value, fgColor.value, bgColor.value);3. Core JavaScript Concepts You Learned
Crucial concept utilized to power the dynamic state, user interactions, and styling of this project.
Crucial concept utilized to power the dynamic state, user interactions, and styling of this project.
Crucial concept utilized to power the dynamic state, user interactions, and styling of this project.
Crucial concept utilized to power the dynamic state, user interactions, and styling of this project.
4. How to Run Locally on Your Computer
- Download the project ZIP: Click the Download Project (.ZIP) button at the top or in the live runner to save
qr-code-generator-project.zip. - Extract the archive: Unzip the downloaded file. Inside you will find separate
index.html,style.css,script.js,index-standalone.html, and a comprehensiveREADME.md. - Open in any browser: Simply double-click
index.html(orindex-standalone.html) to open and test immediately in Chrome, Firefox, Safari, or Edge. - Edit in Visual Studio Code: Open the extracted folder in VS Code. Right-click
index.htmland select "Open with Live Server" to enjoy instant live reloading as you modify HTML, CSS, or JS!
Bonus Challenges for Learners
Ready to level up? Try adding these extra features on your own:
- Add sound effects or audio feedback for user button clicks.
- Add custom theme color customizer with CSS variables.
- Export the results to PDF or copy as a formatted text summary.