-
Notifications
You must be signed in to change notification settings - Fork 0
/
scipt.js
83 lines (72 loc) · 2.69 KB
/
scipt.js
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
// HTML Elements
const emojiGallery = document.getElementById('emojiGallery');
const shareCodeArea = document.getElementById('shareCode');
const canvas = document.getElementById('emojiCanvas');
const ctx = canvas.getContext('2d');
const emojiSearch = document.getElementById('emojiSearch');
const API_URL = 'https://emoji-api.com/emojis?access_key=c1e940e6dbe3fbd13928416585178ed4f99c690c';
let emojis = [];
let currentEmoji = null;
let emojiSize = 150; // Default size for the emoji
let emojiColor = 'black'; // Default color for the emoji
// Fetch all emojis from the API
async function fetchEmojis() {
try {
const response = await fetch(API_URL);
if (!response.ok) throw new Error('Failed to fetch emojis');
emojis = await response.json();
updateEmojiGallery(emojis);
} catch (error) {
console.error('Error fetching emojis:', error);
}
}
// Update emoji gallery with a list of emojis
function updateEmojiGallery(emojisToDisplay) {
emojiGallery.innerHTML = ''; // Clear existing gallery
emojisToDisplay.forEach(emoji => {
const emojiDiv = document.createElement('div');
emojiDiv.className = 'emoji';
emojiDiv.textContent = emoji.character;
emojiDiv.onclick = () => loadEmoji(emoji); // Load emoji when clicked
emojiGallery.appendChild(emojiDiv);
});
}
// Load selected emoji into the canvas
function loadEmoji(emoji) {
currentEmoji = emoji; // Store the current selected emoji
ctx.clearRect(0, 0, canvas.width, canvas.height); // Clear the canvas
ctx.font = `${emojiSize}px Arial`;
ctx.fillStyle = emojiColor; // Use the selected color
ctx.fillText(emoji.character, 75, 150); // Adjust the position and size as needed
}
// Search emojis based on input
function searchEmojis() {
const query = emojiSearch.value.toLowerCase();
const filteredEmojis = emojis.filter(emoji => emoji.character.toLowerCase().includes(query));
updateEmojiGallery(filteredEmojis);
}
// Change the emoji size
function changeEmojiSize(size) {
emojiSize = size;
if (currentEmoji) {
loadEmoji(currentEmoji); // Re-load emoji with new size
}
}
// Change the emoji color
function changeEmojiColor(color) {
emojiColor = color;
if (currentEmoji) {
loadEmoji(currentEmoji); // Re-load emoji with new color
}
}
// Generate shareable code
function generateShareableCode() {
if (currentEmoji) {
const code = `<div style="font-size: ${emojiSize}px; color: ${emojiColor};">${currentEmoji.character}</div>`;
shareCodeArea.value = code;
} else {
shareCodeArea.value = 'Please select an emoji first!';
}
}
// Call fetchEmojis when the page loads
window.onload = fetchEmojis;