| requestAnimationFrameを使用した文字の表示 |
| 文を1文字ずつ表示して、さいごにすべての文字列を表示するサンプルです。 |
copy
let str ="吾輩は猫である";
function stringAnimate(str)
{
let lastTime =0;
let index = 0;
//currentTime:アニメーション開始からの経過時間
function animate(currentTime)
{
//経過時間を計算
const timeElapsed = currentTime - lastTime;
//0.6秒を経過したら、if文のステップを実行
if(timeElapsed > 600)
{
const test = document.getElementById('test1');
if (index < str.length)
{
//指定のインデックス位置から1文字を取得
test.innerText=str.substring(index,index+1);
//次の計測をするため差し替えます
lastTime = currentTime;
//次のインデックス位置にインクリメントします
index++;
requestAnimationFrame(animate);// 次のフレームで再度実行
}
else
{
//さいごに、すべての文字列を表示します
test.innerText=str;
}
}
else
{
requestAnimationFrame(animate);// 次のフレームで再度実行
}
}
requestAnimationFrame(animate);
}
// アニメーション開始
stringAnimate(str);
requestAnimationFrameは画像をスムーズに動かすためのメソッドですが| [URL]イラスト:農民イラスト | |
| URL[https://nawmin.stores.jp][クリックすると開きます]) |
| Displaying characters using requestAnimationFrame |
| This is an example that displays a sentence character by character and then displays the entire string at the end. |
copy
let str ="I Am a Cat";
function stringAnimate(str)
{
let lastTime =0;
let index = 0;
//currentTime:Time elapsed since the start of the animation
function animate(currentTime)
{
//Calculate elapsed time
const timeElapsed = currentTime - lastTime;
//After 0.6 seconds have passed, execute the step in the if statement
if(timeElapsed > 600)
{
const test = document.getElementById('test1');
if (index < str.length)
{
//Gets one character from the specified index position.
test.innerText=str.substring(index,index+1);
//Replace for next measurement
lastTime = currentTime;
//Increments to the next index position
index++;
requestAnimationFrame(animate);// Run again next frame
}
else
{
//Finally, display all the strings.
test.innerText=str;
}
}
else
{
requestAnimationFrame(animate);// Run again next frame
}
}
requestAnimationFrame(animate);
}
// Animation Start
stringAnimate(str);
requestAnimationFrame is a method used to smoothly animate an image.| [URL]illustration by nawmin.com | |
| URL[https://nawmin.stores.jp][click to open the homepage]) |
| ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。 ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、 または公開したりすることはできません。 当方は、ホームペーよびアプリ利用したいかなる理由によっての障害等が発生しても、 その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について 一切の責任を負わないものとします。 |