-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget.js
More file actions
111 lines (94 loc) · 3.31 KB
/
widget.js
File metadata and controls
111 lines (94 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
async function getScore(username) {
const res = await fetch(`https://api.github.com/users/${username}/events`);
if (!res.ok) return 0; // fallback if user not found
const events = await res.json();
let score = 0;
events.forEach(e => {
if (e.type === "PushEvent") score += 3;
if (e.type === "PullRequestEvent") score += 5;
if (e.type === "IssuesEvent") score += 2;
});
return score;
}
async function getAvatarUrl(username) {
const res = await fetch(`https://api.github.com/users/${username}`);
if (!res.ok) return null;
const data = await res.json();
return data.avatar_url;
}
async function drawMoodRing(username) {
if (!username) return;
const score = await getScore(username);
const avatarUrl = await getAvatarUrl(username);
const canvas = document.getElementById('moodRing');
const ctx = canvas.getContext('2d');
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Background
ctx.fillStyle = '#fff';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Mood ring
const angle = Math.min(score / 50, 1) * 2 * Math.PI;
ctx.beginPath();
ctx.arc(110, 110, 90, -Math.PI / 2, -Math.PI / 2 + angle);
ctx.lineWidth = 20;
ctx.strokeStyle = score < 10 ? '#FF4C4C' : score < 25 ? '#FFD93D' : '#4CAF50';
ctx.stroke();
// Fun random event
if (Math.random() < 0.1) {
ctx.font = '30px Arial';
ctx.fillText('🪳', 90, 120); // Alaskan Bull Worm
}
// Draw avatar in center
if (avatarUrl) {
const img = new Image();
img.crossOrigin = 'anonymous'; // needed to allow downloads
img.src = avatarUrl;
img.onload = () => {
const size = 80; // diameter of avatar
ctx.save();
ctx.beginPath();
ctx.arc(110, 110, size / 2, 0, Math.PI * 2);
ctx.closePath();
ctx.clip();
ctx.drawImage(img, 110 - size / 2, 110 - size / 2, size, size);
ctx.restore();
};
}
// Update share link
const shareLink = `${window.location.origin}${window.location.pathname}?user=${username}`;
const shareLinkElem = document.getElementById('share-link');
shareLinkElem.textContent = ''; // Clear previous content
const a = document.createElement('a');
a.href = shareLink;
a.target = '_blank';
a.rel = 'noopener noreferrer';
a.textContent = shareLink;
shareLinkElem.appendChild(a);
}
let lastUsername = null; // store last generated username
// Generate button
document.getElementById('generateBtn').addEventListener('click', async () => {
const username = document.getElementById('username').value.trim();
if (!username) return alert('Please enter a GitHub username!');
lastUsername = username;
await drawMoodRing(username);
});
// Download button
document.getElementById('downloadBtn').addEventListener('click', () => {
if (!lastUsername) return alert('Please generate the mood ring first!');
const canvas = document.getElementById('moodRing');
const link = document.createElement('a');
link.href = canvas.toDataURL('image/png');
link.download = `${lastUsername}-mood-ring.png`;
link.click();
});
// Auto-load if username provided in URL
window.addEventListener('DOMContentLoaded', () => {
const urlParams = new URLSearchParams(window.location.search);
const presetUser = urlParams.get('user');
if (presetUser) {
document.getElementById('username').value = presetUser;
drawMoodRing(presetUser);
}
});