Skip to content

176 web animation techniques, each a live demo with plain settings and a ready-made prompt for your AI assistant. No dependencies, no build step.

fix: cap Starfield on sideways phones, ignore the choice already made and match its README to the code

matine742592609b7
diff --git a/animations/07-ambient-background/starfield/README.md b/animations/07-ambient-background/starfield/README.md
index f656b079..bc27059a 100644
--- a/animations/07-ambient-background/starfield/README.md
+++ b/animations/07-ambient-background/starfield/README.md
@@ -22,12 +22,14 @@ class Star {
}
update() {
+ this.dist += this.speed;
const ratio = this.dist / MAX_DIST;
// Accelerate as the star "approaches" — perspective foreshortening
- this.dist += this.speed * (1 + ratio * 2);
+ this.speed = (ratio * 0.5 + 0.2) * BASE_SPEED * 1.5; // faster toward the edge
// Reset to center when off-screen
if (this.dist > MAX_DIST) {
this.dist = 0;
+ this.speed = (Math.random() * 0.6 + 0.2) * BASE_SPEED;
this.angle = Math.random() * Math.PI * 2;
}
}
@@ -36,7 +38,7 @@ class Star {
const ratio = this.dist / MAX_DIST;
const x = cx + Math.cos(this.angle) * this.dist;
const y = cy + Math.sin(this.angle) * this.dist;
- const size = this.size * (1 + ratio * 2); // grow with distance
+ const size = this.size * (1 + ratio * 1.5); // grow with distance
const opacity = 0.3 + ratio * 0.7; // brighten with distance
ctx.fillStyle = `rgba(255, 255, 255, ${opacity})`;
@@ -73,7 +75,7 @@ class DriftStar {
## Production notes
- **Canvas vs DOM**: DOM elements at star counts above 50 cause heavy layout recalculation. Canvas is the right tool for this effect.
-- **Phones**: the demo draws at most 300 stars on screens narrower than 600px; each star is a separate fill, so the count is the main cost.
+- **Phones**: the demo draws at most 300 stars on phone-sized screens (up to 600px wide, or up to 500px tall for a phone held sideways); each star is a separate fill, so the count is the main cost.
- **`ctx.fillStyle` caching**: setting `fillStyle` per star is expensive. Group stars by opacity bucket and set fillStyle once per bucket (color batching) to reduce canvas state changes.
- **`requestAnimationFrame` throttling**: on 120Hz displays, the loop runs twice as fast. Cap time delta to avoid stars moving at different speeds across devices.
- **Nebula background pairing**: adding a subtle radial gradient (deep purple in one quadrant, deep blue in another) behind the stars dramatically increases realism with minimal performance cost.
diff --git a/animations/07-ambient-background/starfield/index.html b/animations/07-ambient-background/starfield/index.html
index 9037dd3e..19839890 100644
--- a/animations/07-ambient-background/starfield/index.html
+++ b/animations/07-ambient-background/starfield/index.html
@@ -169,7 +169,8 @@
const cvs=document.getElementById('cvs'),ctx=cvs.getContext('2d');
const stage=document.querySelector('.stage');
const slowTog=document.getElementById('slow-tog');
- const PHONE_STARS=300; // a stage narrower than 600px draws at most this many stars
+ const PHONE_STARS=300; // phones and short screens draw at most this many stars
+ const phone=matchMedia('(max-width:600px),(max-height:500px)');
let W=0,H=0,raf=null,lastT=0,paused=false;
let MODE='radial',COUNT=300,SPD=0.4,TWINKLE=true,NEBULA=false,COL='white';
let stars=[];
@@ -224,7 +225,7 @@
}
function initStars(){
- const n=W<600?Math.min(COUNT,PHONE_STARS):COUNT;
+ const n=phone.matches?Math.min(COUNT,PHONE_STARS):COUNT;
stars=Array.from({length:n},()=>new Star(true));
}
@@ -280,6 +281,7 @@
function choices(id,pick){
const buttons=document.querySelectorAll('#'+id+' button');
buttons.forEach(b=>b.addEventListener('click',()=>{
+ if(b.classList.contains('on'))return; // pressing the choice already made does nothing
buttons.forEach(x=>{x.classList.toggle('on',x===b);x.setAttribute('aria-pressed',String(x===b))});
pick(b.dataset);
}));
@@ -293,6 +295,10 @@
resize();
window.addEventListener('resize',resize);
+ // A window that crosses the phone size changes how many stars it draws, even when the stage keeps its size
+ function onPhone(){const n=phone.matches?Math.min(COUNT,PHONE_STARS):COUNT;if(n!==stars.length){initStars();paint(false);}}
+ if(phone.addEventListener)phone.addEventListener('change',onPhone);
+ else if(phone.addListener)phone.addListener(onPhone);
start();
</script>
</body>