Initial commit: QR Code Generator with custom image support

- React + Vite project setup
- QR code generation using qrcode library
- Textarea input for QR code content
- Custom image/SVG upload functionality
- Adjustable image size with slider control
- Modern UI with dark/light mode support
- Real-time QR code generation
- Responsive design for all screen sizes
This commit is contained in:
Michael Mainguy 2025-07-31 19:01:13 -05:00
commit 7fdb20dee0
10 changed files with 2733 additions and 0 deletions

24
.gitignore vendored Normal file
View File

@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules
dist
dist-ssr
*.local
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

13
index.html Normal file
View File

@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>QR Codes</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>

1993
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

20
package.json Normal file
View File

@ -0,0 +1,20 @@
{
"name": "qrcodes",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"devDependencies": {
"@vitejs/plugin-react": "^4.7.0",
"vite": "^7.0.4"
},
"dependencies": {
"qrcode": "^1.5.4",
"react": "^19.1.1",
"react-dom": "^19.1.1"
}
}

1
public/vite.svg Normal file
View File

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>

After

Width:  |  Height:  |  Size: 1.5 KiB

13
src/App.jsx Normal file
View File

@ -0,0 +1,13 @@
import React from 'react'
import TextAreaComponent from './components/TextAreaComponent'
function App() {
return (
<div className="App">
<h1>QR Code Generator</h1>
<TextAreaComponent />
</div>
)
}
export default App

View File

@ -0,0 +1,247 @@
import React, { useState, useEffect, useRef } from 'react'
import QRCode from 'qrcode'
const TextAreaComponent = () => {
const [text, setText] = useState('')
const [qrCodeUrl, setQrCodeUrl] = useState('')
const [customImage, setCustomImage] = useState(null)
const [customImageUrl, setCustomImageUrl] = useState('')
const [imageSize, setImageSize] = useState(25) // Size as percentage of QR code
const canvasRef = useRef(null)
// Generate QR code when text, custom image, or image size changes
useEffect(() => {
if (text.trim()) {
generateQRCode(text)
} else {
setQrCodeUrl('')
}
}, [text, customImage, imageSize])
const generateQRCode = async (inputText) => {
try {
// Generate QR code as data URL
const url = await QRCode.toDataURL(inputText, {
width: 256,
margin: 2,
color: {
dark: '#000000',
light: '#FFFFFF'
}
})
if (customImage) {
// Create QR code with custom image overlay
const qrWithImage = await addImageToQRCode(url, customImageUrl)
setQrCodeUrl(qrWithImage)
} else {
setQrCodeUrl(url)
}
} catch (error) {
console.error('Error generating QR code:', error)
setQrCodeUrl('')
}
}
const addImageToQRCode = async (qrCodeUrl, imageUrl) => {
return new Promise((resolve) => {
const canvas = document.createElement('canvas')
const ctx = canvas.getContext('2d')
// Set canvas size
canvas.width = 256
canvas.height = 256
// Create image objects
const qrImage = new Image()
const customImage = new Image()
qrImage.onload = () => {
// Draw QR code
ctx.drawImage(qrImage, 0, 0, 256, 256)
customImage.onload = () => {
// Calculate image size based on user preference
const calculatedImageSize = 256 * (imageSize / 100)
const imageX = (256 - calculatedImageSize) / 2
const imageY = (256 - calculatedImageSize) / 2
// Create circular clipping path for the image
ctx.save()
ctx.beginPath()
ctx.arc(imageX + calculatedImageSize/2, imageY + calculatedImageSize/2, calculatedImageSize/2, 0, 2 * Math.PI)
ctx.clip()
// Draw custom image
ctx.drawImage(customImage, imageX, imageY, calculatedImageSize, calculatedImageSize)
// Restore context
ctx.restore()
// Add white border around the image
ctx.strokeStyle = '#FFFFFF'
ctx.lineWidth = 3
ctx.beginPath()
ctx.arc(imageX + calculatedImageSize/2, imageY + calculatedImageSize/2, calculatedImageSize/2, 0, 2 * Math.PI)
ctx.stroke()
// Convert canvas to data URL
resolve(canvas.toDataURL('image/png'))
}
// Handle SVG loading errors
customImage.onerror = () => {
console.error('Error loading custom image')
resolve(qrCodeUrl) // Return QR code without custom image
}
customImage.src = imageUrl
}
qrImage.onerror = () => {
console.error('Error loading QR code')
resolve('')
}
qrImage.src = qrCodeUrl
})
}
const handleImageUpload = (event) => {
const file = event.target.files[0]
if (file) {
// Validate file type - accept images and SVG
const validTypes = ['image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/webp', 'image/svg+xml']
if (!validTypes.includes(file.type)) {
alert('Please select an image file (JPEG, PNG, GIF, WebP, or SVG)')
return
}
// Validate file size (max 2MB)
if (file.size > 2 * 1024 * 1024) {
alert('Image file size must be less than 2MB')
return
}
const reader = new FileReader()
reader.onload = (e) => {
setCustomImage(file)
setCustomImageUrl(e.target.result)
}
reader.readAsDataURL(file)
}
}
const removeCustomImage = () => {
setCustomImage(null)
setCustomImageUrl('')
}
const handleTextChange = (e) => {
setText(e.target.value)
}
const handleClear = () => {
setText('')
}
return (
<div className="textarea-container">
<label htmlFor="text-input" className="textarea-label">
Enter text to generate QR code:
</label>
<textarea
id="text-input"
value={text}
onChange={handleTextChange}
placeholder="Enter your text here..."
className="textarea-input"
rows={6}
cols={50}
/>
<div className="image-upload-section">
<label htmlFor="image-upload" className="image-upload-label">
Add custom image or SVG to QR code center:
</label>
<div className="image-upload-controls">
<input
id="image-upload"
type="file"
accept="image/*,.svg"
onChange={handleImageUpload}
className="image-upload-input"
/>
{customImage && (
<button
onClick={removeCustomImage}
className="remove-image-button"
>
Remove Image
</button>
)}
</div>
{customImage && (
<div className="image-preview">
<img
src={customImageUrl}
alt="Custom image preview"
className="preview-image"
/>
<span className="image-name">{customImage.name}</span>
</div>
)}
{customImage && (
<div className="image-size-control">
<label htmlFor="image-size" className="size-label">
Image Size: {imageSize}%
</label>
<input
id="image-size"
type="range"
min="10"
max="40"
value={imageSize}
onChange={(e) => setImageSize(parseInt(e.target.value))}
className="size-slider"
/>
<div className="size-labels">
<span>Small</span>
<span>Large</span>
</div>
</div>
)}
</div>
<div className="textarea-actions">
<button
onClick={handleClear}
className="clear-button"
disabled={!text}
>
Clear
</button>
<span className="character-count">
Characters: {text.length}
</span>
</div>
{qrCodeUrl && (
<div className="qr-code-container">
<h3>QR Code {customImage && '(with custom image)'}</h3>
<img
src={qrCodeUrl}
alt="QR Code"
className="qr-code-image"
/>
<p className="qr-code-info">
Scan this QR code with any QR code reader app
</p>
</div>
)}
</div>
)
}
export default TextAreaComponent

10
src/main.jsx Normal file
View File

@ -0,0 +1,10 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.jsx'
import './style.css'
ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<App />
</React.StrictMode>,
)

405
src/style.css Normal file
View File

@ -0,0 +1,405 @@
:root {
font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
line-height: 1.5;
font-weight: 400;
color-scheme: light dark;
color: rgba(255, 255, 255, 0.87);
background-color: #242424;
font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
-webkit-text-size-adjust: 100%;
}
a {
font-weight: 500;
color: #646cff;
text-decoration: inherit;
}
a:hover {
color: #535bf2;
}
body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
}
h1 {
font-size: 3.2em;
line-height: 1.1;
text-align: center;
margin-bottom: 2rem;
}
#root {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}
.App {
width: 100%;
max-width: 600px;
margin: 0 auto;
}
.textarea-container {
display: flex;
flex-direction: column;
gap: 1rem;
background: rgba(255, 255, 255, 0.05);
padding: 2rem;
border-radius: 12px;
border: 1px solid rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
}
.textarea-label {
font-size: 1.1rem;
font-weight: 500;
color: #e0e0e0;
text-align: left;
}
.textarea-input {
width: 100%;
min-height: 120px;
padding: 1rem;
border: 2px solid rgba(255, 255, 255, 0.2);
border-radius: 8px;
background: rgba(255, 255, 255, 0.05);
color: #ffffff;
font-size: 1rem;
font-family: inherit;
resize: vertical;
transition: all 0.3s ease;
box-sizing: border-box;
}
.textarea-input:focus {
outline: none;
border-color: #646cff;
box-shadow: 0 0 0 3px rgba(100, 108, 255, 0.1);
}
.textarea-input::placeholder {
color: rgba(255, 255, 255, 0.5);
}
.textarea-actions {
display: flex;
justify-content: space-between;
align-items: center;
gap: 1rem;
}
.clear-button {
padding: 0.75rem 1.5rem;
background: #646cff;
color: white;
border: none;
border-radius: 6px;
font-size: 0.9rem;
font-weight: 500;
cursor: pointer;
transition: all 0.3s ease;
}
.clear-button:hover:not(:disabled) {
background: #535bf2;
transform: translateY(-1px);
}
.clear-button:disabled {
background: rgba(255, 255, 255, 0.1);
color: rgba(255, 255, 255, 0.3);
cursor: not-allowed;
}
.character-count {
font-size: 0.9rem;
color: rgba(255, 255, 255, 0.7);
font-weight: 500;
}
.image-upload-section {
margin: 1.5rem 0;
padding: 1.5rem;
background: rgba(255, 255, 255, 0.05);
border-radius: 8px;
border: 1px solid rgba(255, 255, 255, 0.1);
}
.image-upload-label {
display: block;
font-size: 1rem;
font-weight: 500;
color: #e0e0e0;
margin-bottom: 1rem;
}
.image-upload-controls {
display: flex;
gap: 1rem;
align-items: center;
margin-bottom: 1rem;
}
.image-upload-input {
flex: 1;
padding: 0.75rem;
border: 2px solid rgba(255, 255, 255, 0.2);
border-radius: 6px;
background: rgba(255, 255, 255, 0.05);
color: #ffffff;
font-size: 0.9rem;
cursor: pointer;
transition: all 0.3s ease;
}
.image-upload-input:hover {
border-color: rgba(255, 255, 255, 0.4);
}
.image-upload-input:focus {
outline: none;
border-color: #646cff;
box-shadow: 0 0 0 3px rgba(100, 108, 255, 0.1);
}
.remove-image-button {
padding: 0.75rem 1rem;
background: #dc3545;
color: white;
border: none;
border-radius: 6px;
font-size: 0.9rem;
font-weight: 500;
cursor: pointer;
transition: all 0.3s ease;
}
.remove-image-button:hover {
background: #c82333;
transform: translateY(-1px);
}
.image-preview {
display: flex;
align-items: center;
gap: 1rem;
padding: 1rem;
background: rgba(255, 255, 255, 0.05);
border-radius: 6px;
border: 1px solid rgba(255, 255, 255, 0.1);
}
.preview-image {
width: 40px;
height: 40px;
border-radius: 50%;
object-fit: cover;
border: 2px solid rgba(255, 255, 255, 0.2);
}
.image-name {
font-size: 0.9rem;
color: rgba(255, 255, 255, 0.8);
font-weight: 500;
}
.image-size-control {
margin-top: 1rem;
padding: 1rem;
background: rgba(255, 255, 255, 0.05);
border-radius: 6px;
border: 1px solid rgba(255, 255, 255, 0.1);
}
.size-label {
display: block;
font-size: 0.9rem;
font-weight: 500;
color: #e0e0e0;
margin-bottom: 0.5rem;
}
.size-slider {
width: 100%;
height: 6px;
border-radius: 3px;
background: rgba(255, 255, 255, 0.2);
outline: none;
-webkit-appearance: none;
appearance: none;
margin-bottom: 0.5rem;
}
.size-slider::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 18px;
height: 18px;
border-radius: 50%;
background: #646cff;
cursor: pointer;
border: 2px solid #ffffff;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}
.size-slider::-moz-range-thumb {
width: 18px;
height: 18px;
border-radius: 50%;
background: #646cff;
cursor: pointer;
border: 2px solid #ffffff;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}
.size-labels {
display: flex;
justify-content: space-between;
font-size: 0.8rem;
color: rgba(255, 255, 255, 0.6);
font-weight: 500;
}
.qr-code-container {
margin-top: 2rem;
padding: 1.5rem;
background: rgba(255, 255, 255, 0.05);
border-radius: 12px;
border: 1px solid rgba(255, 255, 255, 0.1);
text-align: center;
}
.qr-code-container h3 {
margin: 0 0 1rem 0;
color: #e0e0e0;
font-size: 1.2rem;
}
.qr-code-image {
border: 2px solid rgba(255, 255, 255, 0.2);
border-radius: 8px;
background: white;
margin: 0 auto;
display: block;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
max-width: 100%;
height: auto;
}
.qr-code-info {
margin: 1rem 0 0 0;
font-size: 0.9rem;
color: rgba(255, 255, 255, 0.7);
font-style: italic;
}
@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #ffffff;
}
a:hover {
color: #747bff;
}
.textarea-container {
background: rgba(0, 0, 0, 0.02);
border: 1px solid rgba(0, 0, 0, 0.1);
}
.textarea-label {
color: #213547;
}
.textarea-input {
border: 2px solid rgba(0, 0, 0, 0.2);
background: rgba(255, 255, 255, 0.8);
color: #213547;
}
.textarea-input::placeholder {
color: rgba(0, 0, 0, 0.5);
}
.clear-button:disabled {
background: rgba(0, 0, 0, 0.1);
color: rgba(0, 0, 0, 0.3);
}
.character-count {
color: rgba(0, 0, 0, 0.7);
}
.image-upload-section {
background: rgba(0, 0, 0, 0.02);
border: 1px solid rgba(0, 0, 0, 0.1);
}
.image-upload-label {
color: #213547;
}
.image-upload-input {
border: 2px solid rgba(0, 0, 0, 0.2);
background: rgba(255, 255, 255, 0.8);
color: #213547;
}
.image-upload-input:hover {
border-color: rgba(0, 0, 0, 0.4);
}
.image-preview {
background: rgba(0, 0, 0, 0.02);
border: 1px solid rgba(0, 0, 0, 0.1);
}
.preview-image {
border: 2px solid rgba(0, 0, 0, 0.2);
}
.image-name {
color: rgba(0, 0, 0, 0.8);
}
.image-size-control {
background: rgba(0, 0, 0, 0.02);
border: 1px solid rgba(0, 0, 0, 0.1);
}
.size-label {
color: #213547;
}
.size-slider {
background: rgba(0, 0, 0, 0.2);
}
.size-labels {
color: rgba(0, 0, 0, 0.6);
}
.qr-code-container {
background: rgba(0, 0, 0, 0.02);
border: 1px solid rgba(0, 0, 0, 0.1);
}
.qr-code-container h3 {
color: #213547;
}
.qr-code-image {
border: 2px solid rgba(0, 0, 0, 0.2);
}
.qr-code-info {
color: rgba(0, 0, 0, 0.7);
}
}

7
vite.config.js Normal file
View File

@ -0,0 +1,7 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
})