nihongo-ninja-dojo / script.js
RefreshBytes's picture
Please make a website for learning Japanese that is interactive with lots of animations that make it easy and not boring quickly using HTML, CSS, JS code.
9e01f6f verified
Raw History Blame Contribute Delete
3.75 kB
// Shared JavaScript across all pages
// Scroll animation handler
document.addEventListener('DOMContentLoaded', function() {
const fadeElements = document.querySelectorAll('.fade-in');
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('visible');
}
});
}, { threshold: 0.1 });
fadeElements.forEach(element => {
observer.observe(element);
});
});
// Audio pronunciation function
function playJapaneseAudio(text) {
const utterance = new SpeechSynthesisUtterance(text);
utterance.lang = 'ja-JP';
utterance.rate = 0.8;
speechSynthesis.speak(utterance);
}
// Particle effect for correct answers
function createParticles(x, y, count = 20) {
for (let i = 0; i < count; i++) {
const particle = document.createElement('div');
particle.className = 'particle';
particle.style.left = `${x}px`;
particle.style.top = `${y}px`;
const angle = Math.random() * Math.PI * 2;
const velocity = 2 + Math.random() * 2;
const vx = Math.cos(angle) * velocity;
const vy = Math.sin(angle) * velocity;
document.body.appendChild(particle);
setTimeout(() => {
particle.style.transform = `translate(${vx * 100}px, ${vy * 100}px)`;
}, 10);
setTimeout(() => {
particle.remove();
}, 1000);
}
}
// Local storage for user progress
const userProgress = {
get: function(key) {
try {
return JSON.parse(localStorage.getItem(`ninja_${key}`)) || 0;
} catch {
return 0;
}
},
set: function(key, value) {
localStorage.setItem(`ninja_${key}`, JSON.stringify(value));
},
updateStreak: function() {
const today = new Date().toDateString();
const lastVisit = this.get('last_visit');
if (lastVisit !== today) {
const streak = this.get('streak') || 0;
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
if (lastVisit === yesterday.toDateString() || !lastVisit) {
this.set('streak', streak + 1);
} else {
this.set('streak', 1);
}
this.set('last_visit', today);
return this.get('streak');
}
};
// Typing animation for Japanese text
function typeJapaneseText(element, text, speed = 50) {
let i = 0;
element.textContent = '';
function type() {
if (i < text.length) {
element.textContent += text.charAt(i);
i++;
setTimeout(type, speed);
}
}
type();
}
// Shuffle array for quiz questions
function shuffleArray(array) {
const shuffled = [...array];
for (let i = shuffled.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
}
return shuffled;
}
// API call to get Japanese vocabulary
async function getJapaneseVocabulary(level = 'n5') {
try {
// Using a free Japanese API
const response = await fetch(`https://jisho.org/api/v1/search/words?keyword=%23common&page=1`);
const data = await response.json();
return data.data || [];
} catch (error) {
console.error('Failed to fetch vocabulary:', error);
return [];
}
}
// Initialize app
document.addEventListener('DOMContentLoaded', function() {
// Update user streak
userProgress.updateStreak();
console.log('Nihongo Ninja Dojo loaded successfully!');
});