Skip to content

Commit

Permalink
十周年快乐🎉!
Browse files Browse the repository at this point in the history
  • Loading branch information
movsb committed Dec 23, 2024
1 parent 84f265c commit 1bf1b96
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
29 changes: 29 additions & 0 deletions service/modules/renderers/anniversary/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package friends

import (
"embed"

"github.com/movsb/taoblog/modules/utils"
"github.com/movsb/taoblog/modules/utils/dir"
dynamic "github.com/movsb/taoblog/service/modules/renderers/_dynamic"
"github.com/movsb/taoblog/theme/modules/sass"
)

//go:generate sass --no-source-map style.scss style.css

//go:embed style.css script.js
var _root embed.FS

func init() {
dynamic.RegisterInit(func() {
dynamic.Dynamic[`anniversary`] = dynamic.Content{
Styles: []string{
string(utils.Must1(_root.ReadFile(`style.css`))),
},
Scripts: []string{
string(utils.Must1(_root.ReadFile(`script.js`))),
},
}
sass.WatchDefaultAsync(string(dir.SourceAbsoluteDir()))
})
}
84 changes: 84 additions & 0 deletions service/modules/renderers/anniversary/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
function setupAnniversary(){

if (!new Date().toDateString().includes('Dec 24 2024')) {
return;
}

if (location.pathname != '/') {
return;
}

const canvas = document.createElement('canvas');
canvas.classList.add('anniversary');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
document.body.appendChild(canvas);

const ctx = canvas.getContext('2d');
const emojis = ['🎂', '🎄'];

const particles = [];

class Particle {
constructor(x, y, emoji) {
this.x = x;
this.y = y;
this.size = Math.random() * 40 + 20;
this.speed = Math.random() + 1;
this.emoji = emoji;
}
draw() {
ctx.font = `${this.size}px Arial`;
ctx.textAlign = 'center';
ctx.fillText(this.emoji, this.x, this.y);
}
update() {
this.y += this.speed;
if (this.y > canvas.height) {
this.y = -this.size;
this.x = Math.random() * canvas.width;
}
}
}

function init() {
for (let i = 0; i < 30; i++) {
const randomEmoji = emojis[Math.floor(Math.random() * emojis.length)];
const x = Math.random() * canvas.width;
const y = Math.random() * canvas.height - canvas.height;
particles.push(new Particle(x, y, randomEmoji));
}
}

let timer = undefined;
timer = setTimeout(function(){
clearTimeout(timer);
timer = undefined;
}, 10000);

function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
particles.forEach(particle => {
particle.update();
particle.draw();
});
if (timer) {
requestAnimationFrame(animate);
} else {
canvas.remove();
}
}

init();
animate();

window.addEventListener('resize', () => {
particles.length = 0;
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
init();
});

}

document.addEventListener('DOMContentLoaded', setupAnniversary);
10 changes: 10 additions & 0 deletions service/modules/renderers/anniversary/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
canvas.anniversary {
display: block;
box-sizing: border-box;
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
z-index: -1;
}
2 changes: 2 additions & 0 deletions service/modules/renderers/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"go.abhg.dev/goldmark/hashtag"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"

_ "github.com/movsb/taoblog/service/modules/renderers/anniversary"
)

// 后面统一改成 Option。
Expand Down

0 comments on commit 1bf1b96

Please sign in to comment.