Skip to content

Commit b2b478f

Browse files
Merge pull request #1214 from Rushabt11/feature/adding-download-option
Feature/adding download option
2 parents 47c663c + d50dbc7 commit b2b478f

3 files changed

Lines changed: 84 additions & 5 deletions

File tree

Projects/QR Code Generator/Index.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ <h1>QR Code Generator</h1>
2020
</div>
2121
<div class="qr-code">
2222
<img src="" alt="qr-code">
23+
<button class="btnshare" title="Share">➡️</button>
2324
</div>
25+
<button class="btnpng">Download Qr</button>
26+
27+
2428
</div>
25-
29+
2630
<script src="script.js"></script>
2731

2832
</body>

Projects/QR Code Generator/script.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const wrapper = document.querySelector(".generator"),
22
qrInput = wrapper.querySelector(".form input"),
33
generateBtn = wrapper.querySelector(".form button"),
4-
qrImg = wrapper.querySelector(".qr-code img");
4+
qrImg = wrapper.querySelector(".qr-code img"),
5+
downloadBtn=wrapper.querySelector(".btnpng"),
6+
sharebtn=wrapper.querySelector(".btnshare");
57
let preValue;
68

79
generateBtn.addEventListener("click", () => {
@@ -13,6 +15,8 @@ generateBtn.addEventListener("click", () => {
1315
qrImg.addEventListener("load", () => {
1416
wrapper.classList.add("active");
1517
generateBtn.innerText = "Generate QR Code";
18+
downloadBtn.style.display="block";
19+
1620
});
1721
});
1822

@@ -21,4 +25,33 @@ qrInput.addEventListener("keyup", () => {
2125
wrapper.classList.remove("active");
2226
preValue = "";
2327
}
24-
});
28+
});
29+
downloadBtn.addEventListener('click',async()=>{
30+
const response=await fetch(qrImg.src);
31+
const blob=await response.blob();
32+
const url=URL.createObjectURL(blob);
33+
const link=document.createElement('a');
34+
link.href=url;
35+
link.download="qr.png";
36+
link.click();
37+
URL.revokeObjectURL(url);
38+
})
39+
sharebtn.addEventListener('click',()=>
40+
{
41+
if (navigator.share) {
42+
navigator.share({
43+
title: "QR Code",
44+
url: qrImg.src
45+
})
46+
.then(function()
47+
{
48+
console.log("QR code shared successfully");
49+
})
50+
.catch(function(error) {
51+
console.error("Error sharing QR code:", error);
52+
});
53+
}
54+
else {
55+
console.log("Sharing not supported in this browser.");
56+
}
57+
})

Projects/QR Code Generator/style.css

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ body {
2626
}
2727

2828
.generator.active {
29-
height: 530px;
29+
height: 600px;
3030
}
3131

3232
header h1 {
@@ -85,6 +85,7 @@ button) {
8585
pointer-events: none;
8686
justify-content: center;
8787
border: 1px solid #ccc;
88+
position: relative;
8889
}
8990

9091
.generator.active .qr-code {
@@ -96,14 +97,51 @@ button) {
9697
.qr-code img {
9798
width: 170px;
9899
}
100+
.btnshare{
101+
position: absolute;
102+
bottom: 10px;
103+
right: 10px;
104+
display: flex;
105+
align-items: center;
106+
justify-content: center;
107+
width: 40px;
108+
height: 40px;
109+
background-color: #05ae10;
110+
border-radius: 50%;
111+
transition: background-color 0.2s ease;
112+
font-size: 20px;
113+
border: none;
114+
color: #fff;
115+
}
116+
117+
.qr-code:hover .btnshare {
118+
background-color: #04a30f;
119+
cursor: pointer;
120+
}
121+
.btnpng
122+
{
123+
color: #fff;
124+
cursor: pointer;
125+
margin-top: 20px;
126+
font-size: 19px;
127+
background: #05ae10;
128+
width:100%;
129+
height:55px;
130+
border:none;
131+
outline: none;
132+
border-radius: 5px;
133+
transition: 0.1s ease-in;
134+
display: none;
135+
}
136+
99137

100138
@media (max-width: 430px) {
101139
.generator {
102140
height: 255px;
103141
padding: 16px 20px;
104142
}
105143
.generator.active {
106-
height: 510px;
144+
height: 600px;
107145
}
108146
header p {
109147
color: #696969;
@@ -115,4 +153,8 @@ button) {
115153
.qr-code img {
116154
width: 160px;
117155
}
156+
.btnpng
157+
{
158+
height: 52px;
159+
}
118160
}

0 commit comments

Comments
 (0)