![見出し画像](https://assets.st-note.com/production/uploads/images/155930991/rectangle_large_type_2_cf5318f9f734a2993119e70d1bacf469.png?width=1200)
漢字がずっと流れるウェブサイトを作る
背景
Word Cascadeというウェブサイトがあります。
![](https://assets.st-note.com/img/1727438138-DanzxJf9phbK78Bq1rFmGeVs.png?width=1200)
眺めていると時間が経つのを忘れてしまう。
「ミノカサゴ」って何だ?
![](https://assets.st-note.com/img/1727439979-pM7SKwGcbR5AruHoUjJBdgsq.png?width=1200)
毒を持った魚でした。
「ハニカム構造」も面白いな〜
![](https://assets.st-note.com/img/1727440060-7EqjmhrpD1eaG0HU5zbkduX8.png?width=1200)
…この感じ、なにかに似ている———
———漢字を見つめているときに似ている。
そうだ、漢字で作ってみよう。もっと発見をするイメージで…
作る
※コードの解説もするので興味がない方は「使い方」まで飛ばしても結構です。
ということで作ります。
やることとしては、
ランダムな漢字を出力するアルゴリズムを考える
漢字を上から下に流す
ぼかしを実装する
の3つですね。
1. ランダムな漢字を出力するアルゴリズムを考える
過去にランダムな漢字を生成するコードを書いたのですが、
これをそのまま組み込むと…
![](https://assets.st-note.com/img/1727440785-wRvteJicTO9L08oMVNajCGdk.png?width=1200)
表示できない文字があまりにも多く、見るに耐えないので、
function getRandomNumberFromRanges(ranges) {
const totalWeight = ranges.reduce((acc, [min, max]) => acc + max - min + 1, 0);
let randomWeight = Math.random() * totalWeight;
for (const [min, max] of ranges) {
const weight = max - min + 1;
if (randomWeight < weight) {
return Math.floor(Math.random() * weight) + min;
}
randomWeight -= weight;
}
}
function randomKanji() {
const ranges = [[19968, 40908], [11904, 12031], [63744, 64217], [131072, 196607]];
return String.fromCodePoint(getRandomNumberFromRanges(ranges));
}
を
function getRandomNumberFromRanges(ranges) {
const totalWeight = ranges.reduce((acc, [min, max]) => acc + max - min + 1, 0);
let randomWeight = Math.random() * totalWeight;
for (const [min, max] of ranges) {
const weight = max - min + 1;
if (randomWeight < weight) {
return Math.floor(Math.random() * weight) + min;
}
randomWeight -= weight;
}
}
function randomKanji() {
// 変更点
const ranges = [[19968, 40908], [11904, 12031], [63744, 64217]];
return String.fromCodePoint(getRandomNumberFromRanges(ranges));
}
にしてrangesの範囲を狭くしました。早く[131072, 196607]にも対応したいです。
![](https://assets.st-note.com/img/1727441019-tPzZvRl9NMeykWjqVAO1SaF0.png?width=1200)
良くなりましたね。
2. 漢字を上から下に流す
本当はcanvasを使った方が良いのでしょうが、使えない使い勝手が悪いのですべてhtmlで行います。そのせいで少し重いです。
具体的には、
document.querySelectorAll(".kanji").forEach(kanji => {
const top = parseFloat(kanji.style.top);
if (top > 100) {
kanji.remove();
} else {
kanji.style.top = `${top + parseFloat(kanji.style.fontSize) / 20}vh`;
if (isBlur == 1) {
kanji.style.filter = `blur(${Math.abs(Number(kanji.style.fontSize.replace("vh", "")) - distance) / fStop + Math.max(5 - top, 0) / 2}vh)`;
}
kanji.style.opacity = Math.max(top, 1) / 5;
}
});
こういったことを毎秒数十回して下に動かしています。
let animation = setInterval(draw, 1000 / 30);
どうしても漢字が突然現れてしまうので、誤魔化すために透明度を徐々に下げていって自然に表示しています。後にぼかしも追加しました。
また、漢字をクリックしたときに辞書に飛びたいので
if (frame % 30 == 0) {
const newKanji = document.createElement('a');
newKanji.textContent = randomKanji();
newKanji.classList.add("kanji");
newKanji.style.fontSize = `${getRandomArbitrary(1, 10)}vh`;
newKanji.style.top = "0vh";
newKanji.style.left = `${getRandomArbitrary(0, 100)}vw`;
// リンクを追加
newKanji.href = "https://zi.tools/zi/" + newKanji.textContent;
newKanji.target = "_blank";
newKanji.rel = "noopener noreferrer";
newKanji.draggable = false;
canvas.appendChild(newKanji);
}
漢字をリンクにしました。
![](https://assets.st-note.com/production/uploads/images/155890300/picture_pc_4474b64e648c0c49181be93a7a0c7bdc.gif?width=1200)
いい感じ!
3. ぼかしを実装する
最後に、漢字を発見するイメージを膨らませるためにぼかしを実装します。
土を掘って宝物を発見する、そんなイメージに近いのかもしれません。
![](https://assets.st-note.com/img/1727442009-x5CpHzdIoXak74fYKgZFtSQ9.png?width=1200)
最初はスライダーを画面の下に表示させようかと思いましたが、イメージを崩さないようにマウスのみの操作で行うようにしました。
実装するにあたって、この動画が参考になりました。
![](https://assets.st-note.com/production/uploads/images/155893431/picture_pc_afea86b5321aea377412f4a69d25ab50.gif?width=1200)
あとは微調整して完成です!
使い方
を開くと
![](https://assets.st-note.com/img/1727443657-DQuVqFL8jgkU4wTIA6Ry3fZ2.png?width=1200)
こんな画面になっているはずなので、マウスをクリックしたまま動かしてみてください。横に動かすとピントが合う位置が、縦に動かすとぼかしの強さが変わります。
また、ぼかしをオフにしたいのならば右下のチェックを外してください。
さらに、漢字をクリックすると辞書に飛ぶことができます。
思う存分楽しんでください。
最後に面白かった漢字をご紹介します。
![](https://assets.st-note.com/img/1727444207-1TLxwSOIj6AZ2oPnEFR74Vd9.png?width=1200)
![](https://assets.st-note.com/img/1727444297-kvUw2GE9zPdrZLW5yYMhAVuS.png)
![](https://assets.st-note.com/img/1727483454-BO8mGUgypfIEks5PRhALHM6v.png?width=1200)
???
(おしまい)
いいなと思ったら応援しよう!
![tozaburo/小林都央](https://assets.st-note.com/production/uploads/images/90332425/profile_335d9e2cbcbb14f2cc08f241fea29502.png?width=600&crop=1:1,smart)