From 1bf1b960db18987fbbd54090f22a0c5409453991 Mon Sep 17 00:00:00 2001 From: movsb Date: Tue, 24 Dec 2024 05:48:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=81=E5=91=A8=E5=B9=B4=E5=BF=AB=E4=B9=90?= =?UTF-8?q?=F0=9F=8E=89=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/modules/renderers/anniversary/gen.go | 29 +++++++ .../modules/renderers/anniversary/script.js | 84 +++++++++++++++++++ .../modules/renderers/anniversary/style.scss | 10 +++ service/modules/renderers/options.go | 2 + 4 files changed, 125 insertions(+) create mode 100644 service/modules/renderers/anniversary/gen.go create mode 100644 service/modules/renderers/anniversary/script.js create mode 100644 service/modules/renderers/anniversary/style.scss diff --git a/service/modules/renderers/anniversary/gen.go b/service/modules/renderers/anniversary/gen.go new file mode 100644 index 00000000..ef1f2397 --- /dev/null +++ b/service/modules/renderers/anniversary/gen.go @@ -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())) + }) +} diff --git a/service/modules/renderers/anniversary/script.js b/service/modules/renderers/anniversary/script.js new file mode 100644 index 00000000..ef341694 --- /dev/null +++ b/service/modules/renderers/anniversary/script.js @@ -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); diff --git a/service/modules/renderers/anniversary/style.scss b/service/modules/renderers/anniversary/style.scss new file mode 100644 index 00000000..151a502c --- /dev/null +++ b/service/modules/renderers/anniversary/style.scss @@ -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; +} diff --git a/service/modules/renderers/options.go b/service/modules/renderers/options.go index 44453be4..391d3fab 100644 --- a/service/modules/renderers/options.go +++ b/service/modules/renderers/options.go @@ -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ใ€‚