Skip to content

Commit

Permalink
Added the last commits. Resolved the conflict (again).
Browse files Browse the repository at this point in the history
  • Loading branch information
ouariachi committed Feb 27, 2024
2 parents e1657b8 + a813bc2 commit 8747c64
Show file tree
Hide file tree
Showing 15 changed files with 467 additions and 318 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"@fontsource-variable/jost": "5.0.17",
"@vercel/analytics": "1.2.2",
"astro": "4.4.5",
"canvas-confetti": "1.9.2"
"canvas-confetti": "1.9.2",
"three": "^0.161.0"
},
"devDependencies": {
"@antfu/eslint-config": "0.43.1",
Expand All @@ -27,6 +28,7 @@
"@astrojs/vercel": "7.3.4",
"@midudev/tailwind-animations": "0.0.7",
"@types/canvas-confetti": "^1.6.4",
"@types/three": "^0.161.2",
"eslint": "8.57.0",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-astro": "0.31.4",
Expand Down
Binary file added public/smoke.webp
Binary file not shown.
4 changes: 2 additions & 2 deletions src/components/HeroLogo.astro
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
fill="none"
viewBox="0 0 800 579"
role="img"
aria-label="Logotipo de la Velada 4"
><title>Logotipo de la Velada 4</title>
aria-label="Logotipo de La Velada 4"
>
<path
id="logo"
fill="currentColor"
Expand Down
127 changes: 127 additions & 0 deletions src/components/SmokeBackground.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<div id="smoke-bkg" class="fixed top-0 -z-10 h-full w-full"></div>

<script>
import * as THREE from "three"

const reducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)")

if (!reducedMotion.matches) {
const $bkg = document.getElementById("smoke-bkg")

let w = window.innerWidth
let h = window.innerHeight

const THEME = {
dark: {
background: 0x666666,
light: 0xffffff,
opacity: 1,
},
light: {
background: 0xeeeeee,
light: 0xffffff,
opacity: 0.2,
},
} as const

const themePreference = window.getThemePreference()
let currentTheme = THEME[themePreference]

// inicializar Three.js
// 3 cosas básicas: escena, cámara, renderizador

// escena 🖼️
const scene = new THREE.Scene()

// camara 📹
// 75 -> ángulo de visión
const camera = new THREE.PerspectiveCamera(75, w / h, 1, 1000)
camera.position.z = 10
scene.add(camera)

// ▶️ renderizador
const renderer = new THREE.WebGLRenderer()
renderer.setSize(w, h)
// color de fondo
renderer.setClearColor(currentTheme.background, 1)

$bkg?.appendChild(renderer.domElement)

// añadir una luz directional
const light = new THREE.DirectionalLight(currentTheme.light, 0.5)
// posicion de la luz
light.position.set(-1, 3, 1)
scene.add(light)

const smokeParticles: THREE.Mesh[] = []

const loader = new THREE.TextureLoader()
loader.crossOrigin = "" // <- en localhost no pasa nada, pero si desplegamos a un servidor, puede ser necesario

loader.load("/smoke.webp", (texture) => {
// 1. geometria
// crear un plano geométrico de 300x300
const smokeGeo = new THREE.PlaneGeometry(300, 300)

// 2. material
const smokeMaterial = new THREE.MeshLambertMaterial({
map: texture,
transparent: true,
opacity: currentTheme.opacity,
})

const NUM_OF_PARTICLES = 300
for (let p = 0; p < NUM_OF_PARTICLES; p++) {
// crear la malla con la geometria y el material
const particle = new THREE.Mesh(smokeGeo, smokeMaterial)
// posicionar aleatoriamente
// en la x, y, z
particle.position.set(
Math.random() * 500 - 250, // X (de -250 a 250)
Math.random() * 500 - 250, // Y (de -250 a 250)
Math.random() * 1000 - 100 // Z (de -100 a 900)
)
// aleatoriamente la z
particle.rotation.z = Math.random() * 360
// añadimos la particula en la escena
scene.add(particle)
// añadimos la particula al array
smokeParticles.push(particle)
}
})

function resize() {
h = window.innerHeight
w = window.innerWidth
camera.aspect = w / h
camera.updateProjectionMatrix() // este metodo lo tenéis que ejecutar siempre que cambiais los parámetros de la cámara
renderer.setSize(w, h)
}

function animate() {
requestAnimationFrame(animate)

smokeParticles.forEach((particle) => {
particle.rotation.z += 0.001
})

renderer.render(scene, camera)
}

animate()

// se va a disparar continuamente mientras hace el resize
window.addEventListener("resize", resize)

window.addEventListener("theme-changed", () => {
const themePreference = window.getThemePreference()
currentTheme = THEME[themePreference]
light.color.setHex(currentTheme.light)
renderer.setClearColor(currentTheme.background, 1)

smokeParticles.forEach((particle) => {
particle.material.opacity = currentTheme.opacity
})
})
}
</script>
67 changes: 24 additions & 43 deletions src/components/ThemeToggle.astro
Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,46 @@ import MoonIcon from "@/icons/moon.astro"
---

<button
id="themeToggle"
class="inline-flex text-primary transition any-hover:scale-125 any-hover:opacity-70"
id="themeToggle"
class="inline-flex text-primary transition any-hover:scale-125 any-hover:opacity-70"
>
<SunIcon
class="opacity-100 transition-transform dark:-rotate-90 dark:opacity-0"
/>
<MoonIcon
class="absolute rotate-90 opacity-0 transition-transform dark:rotate-0 dark:opacity-100"
/>
<span class="sr-only"></span>
<SunIcon class="opacity-100 transition-transform dark:-rotate-90 dark:opacity-0" />
<MoonIcon
class="absolute rotate-90 opacity-0 transition-transform dark:rotate-0 dark:opacity-100"
/>
<span class="sr-only"></span>
</button>

<script is:inline>
const getThemePreference = () => {
if (typeof localStorage !== "undefined" && localStorage.getItem("theme")) {
return localStorage.getItem("theme")
}
return window.matchMedia("(prefers-color-scheme: dark)").matches
? "dark"
: "light"
}
const isDark = getThemePreference() === "dark"
document.documentElement.classList[isDark ? "add" : "remove"]("dark")

const toggleTheme = () => {
function handleToggleClick() {
const element = document.documentElement
element.classList.toggle("dark")

const isDark = element.classList.contains("dark")
localStorage.setItem("theme", isDark ? "dark" : "light")
const isDark = element.classList.contains("dark")
localStorage.setItem("theme", isDark ? "dark" : "light")

updateThemeMessage(isDark ? "dark" : "light")
}

updateThemeMessage(isDark ? "dark" : "light");
}
function updateThemeMessage(theme) {
const translation = theme === "dark" ? "oscuro" : "claro"
const actualThemeMsg = `Alternar tema, el tema actual es ${translation}`
const span = document.querySelector("span.sr-only")
span.innerHTML = actualThemeMsg

window.dispatchEvent(new Event("theme-changed"))
}

const handleThemePreferenceChange = (e) => {
const isDark = document.documentElement.classList.contains("dark")
const toggle = (e.matches && !isDark) || (!e.matches && isDark)
if (toggle) {
toggleTheme()
}
}

const handleToggleClick = () => {
toggleTheme()
if (toggle) handleToggleClick()
}

const darkModePreference = window.matchMedia("(prefers-color-scheme: dark)")
darkModePreference.addEventListener("change", handleThemePreferenceChange)
darkModePreference.addListener(handleThemePreferenceChange) // backward compatibility
document
.getElementById("themeToggle")
.addEventListener("click", handleToggleClick)

const updateThemeMessage = (theme) => {
const translation = theme === "dark" ? "oscuro" : "claro";
const actualThemeMsg = `Alternar tema, el tema actual es ${translation}`;
let span = document.querySelector("span.sr-only");
span.innerHTML = actualThemeMsg;
};

updateThemeMessage(getThemePreference());
document.getElementById("themeToggle").addEventListener("click", handleToggleClick)
updateThemeMessage(window.getThemePreference())
</script>
4 changes: 4 additions & 0 deletions src/env.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
/// <reference types="astro/client" />

interface Window {
getThemePreference(): "dark" | "light"
}
Loading

0 comments on commit 8747c64

Please sign in to comment.