戻る

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は画像をスムーズに動かすためのメソッドですが
今回は文を表示するサンプルとして使用しました。
stringAnimate(str);
str変数に実行したい文を入れます。

requestAnimationFrame(animate)
requestAnimationFrameが実行されます。
実行したいメソッドを通知することで、このサンプルではanimateメソッドが実行されます。

const timeElapsed = currentTime - lastTime;
このサンプルでは0.6秒ごとに1文字を表示切替する仕様にしています。
経過時間を計算して次の1文字を表示するタイミングを取得します。

if (index < str.length)
1文字を表示する判定処理です。

lastTime = currentTime;
この処理により、現在時間(currentTime)をlastTimeに差し替えることで
次の計算の準備をします。

requestAnimationFrame(animate);
この処理をすることで、再帰実行をします。

else
if (index < str.length)のelseです。
1文字ずつすべて表示し終えたので、この仕様では、さいごにすべての文字列を表示させます。
そして、これで処理が終わりなので、requestAnimationFrameは実行していません。

[謝辞]
「イラスト:農民イラスト」さんの画像を使用させていただいております。
ありがとうございます。
[URL]イラスト:農民イラスト
URL[https://nawmin.stores.jp][クリックすると開きます])


戻る
back

Displaying characters using requestAnimationFrame
This is an example that displays a sentence character by character and then displays the entire string at the end.

[sample]
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.
In this example, we used it to display a statement.
stringAnimate(str);
Enter the statement you want to execute in the str variable.

requestAnimationFrame(animate)
requestAnimationFrame is executed.
In this example, the animate method is executed by notifying the desired method.

const timeElapsed = currentTime - lastTime;
This sample is set to display one character every 0.6 seconds.
Calculate the elapsed time to determine the timing to display the next character.

if (index < str.length)
This is the process to determine whether to display one character.

lastTime = currentTime;
This process replaces the current time (currentTime) with lastTime
to prepare for the next calculation.

requestAnimationFrame(animate);
This process enables recursive execution.

else
This is the else part of if (index < str.length).
Since all characters have been displayed one by one, this specification displays the entire string at the end.
Since this is the end of the process, requestAnimationFrame is not executed.

[Acknowledgements]
images used are from "illustration by nawmin.com"
Thank you very much.
[URL]illustration by nawmin.com
URL[https://nawmin.stores.jp][click to open the homepage])


back



著作権情報
ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。
ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、
または公開したりすることはできません。
当方は、ホームペーよびアプリ利用したいかなる理由によっての障害等が発生しても、
その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について
一切の責任を負わないものとします。