QR Code Generator JS

 

QR Code Generator: A Modern Web Application

In today’s digital world, QR codes have become an essential tool for quickly sharing information. Whether you’re linking to a website, sharing contact details, or providing access to a digital menu, QR codes offer a seamless bridge between the physical and digital worlds. This blog post explores a simple yet powerful QR code generator web application built with HTML, CSS, and JavaScript.

Introduction to the QR Code Generator

Our QR code generator allows users to create customized QR codes within seconds. With options to adjust size and color, this tool provides flexibility while maintaining ease of use. The application generates high-quality QR codes that can be instantly downloaded and used for various purposes.

Key Features

  • User-friendly Interface: Clean, responsive design that works on all devices
  • Customization Options: Choose from different sizes and colors
  • Instant Generation: Create QR codes with a single click
  • Download Functionality: Save your QR codes as PNG images
  • Input Validation: Ensures valid data entry before generation
  • Responsive Design: Works seamlessly across mobile and desktop devices

How It Works

The application uses a combination of HTML for structure, CSS for styling, and JavaScript for functionality. At its core, it leverages the popular qrcode.js library to handle the actual QR code generation.

The User Interface

The interface is designed with simplicity in mind. Users are presented with:

  1. An input field for the text or URL
  2. Dropdown menus for size and color selection
  3. Buttons for generating and clearing the QR code
  4. A dedicated area for displaying the generated QR code
  5. A download button that appears once the QR code is generated

The Code Behind the Magic

Let’s dive into some of the key code components that make this application work:

HTML Structure

The HTML structure provides the foundation for our application:

<div class="container">
<h1>QR Code Generator</h1>

<div class="input-group">
<label for="text">Text or URL</label>
<input type="text" id="text" placeholder="Enter text or URL here">
<div class="error" id="input-error"></div>
</div>

<div class="options">
<div style="flex: 1;">
<label for="size">Size</label>
<select id="size">
<option value="100">Small (100x100)</option>
<option value="200" selected>Medium (200x200)</option>
<option value="300">Large (300x300)</option>
<option value="400">Extra Large (400x400)</option>
</select>
</div>

<div style="flex: 1;">
<label for="color">Color</label>
<select id="color">
<option value="#000000" selected>Black</option>
<option value="#4a6cf7">Blue</option>
<option value="#198754">Green</option>
<option value="#dc3545">Red</option>
<option value="#6610f2">Purple</option>
</select>
</div>
</div>

<div class="button-container">
<button id="generate-btn">Generate QR Code</button>
<button id="clear-btn" class="secondary">Clear</button>
</div>

<div class="qr-container">
<div id="qrcode"></div>
<button id="download-btn" class="download-btn">Download QR Code</button>
</div>
</div>

CSS Styling

The CSS provides a modern, clean aesthetic with attention to details like shadows, borders, and spacing:

.container {
background-color: white;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
padding: 30px;
width: 100%;
max-width: 500px;
}
button {
padding: 12px;
background-color: #4a6cf7;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
font-weight: bold;
flex: 1;
transition: background-color 0.3s;
}
button:hover {
background-color: #3a57d5;
}
.options {
display: flex;
gap: 10px;
margin-bottom: 20px;
}

JavaScript Functionality

The JavaScript handles the core functionality, including generating the QR code and enabling downloads:

function generateQRCode() {
const text = textInput.value.trim();
const size = parseInt(sizeSelect.value);
const color = colorSelect.value;

// Validation
if (!text) {
inputError.textContent = 'Please enter some text or URL';
return;
} else {
inputError.textContent = '';
}

// Clear previous QR code
qrcodeDiv.innerHTML = '';

// Generate new QR code
qrcode = new QRCode(qrcodeDiv, {
text: text,
width: size,
height: size,
colorDark: color,
colorLight: '#ffffff',
correctLevel: QRCode.CorrectLevel.H
});

// Show download button
downloadBtn.style.display = 'block';
}
function downloadQRCode() {
const qrImage = qrcodeDiv.querySelector('img');
if (qrImage) {
const link = document.createElement('a');
link.href = qrImage.src;
link.download = 'qrcode.png';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}

Technical Insights

QR Code Generation Library

The application uses the qrcode.js library, which is imported via CDN:

<script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>

This library handles the complex task of converting text or URLs into properly formatted QR codes, allowing us to focus on creating a great user experience.

Download Functionality

The download feature works by creating a temporary anchor element that points to the QR code image source. This element is programmatically clicked and then removed from the DOM:

const link = document.createElement('a');
link.href = qrImage.src;
link.download = 'qrcode.png';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);

This approach provides a seamless download experience without requiring server-side processing.

Use Cases for QR Codes

QR codes generated by this application can be used for:

  1. Business Cards: Link to your professional profile or portfolio
  2. Restaurant Menus: Provide contactless access to digital menus
  3. Event Tickets: Quick verification at entry points
  4. WiFi Access: Allow guests to connect to networks easily
  5. Product Information: Link to detailed specifications or user manuals
  6. Marketing Materials: Connect print media to digital content
  7. Contact Sharing: Quickly share contact information

Performance Considerations

The application is designed to be lightweight and fast. By using a CDN for the QR code library, we ensure quick loading times without burdening the application with large dependencies. All processing happens on the client side, eliminating the need for server requests when generating QR codes.

Conclusion

This QR code generator demonstrates how powerful web applications can be built with just HTML, CSS, and JavaScript. By combining a clean interface with practical functionality, we’ve created a tool that’s both useful and user-friendly.

Comments