戻る

左右移動および上下移動
requestAnimationFrameを使用したサンプル

[サンプル]


[HTML/CSS]

copy
const b0 = document.getElementById('leftright');
const b1 = document.getElementById('updown');
const b2 = document.getElementById('stop');
//左右移動
b0.addEventListener("click", () => 
{
	if(still==1)
	{
		return;
	}
	stop=0;
	still=1;
	requestAnimationFrame(moveLeftRight);
});
//上下移動
b1.addEventListener("click", () => 
{
	if(still==1)
	{
		return;
	}
	stop=0;
	still=1;
	requestAnimationFrame(moveUpDown);
});
b2.addEventListener("click", () => 
{
	stop=1;
});
b0.style.left = 0 + 'px';
b0.style.top = 300 + 'px';
b1.style.left = 100 + 'px';
b1.style.top = 300 + 'px';
b2.style.left = 200 + 'px';
b2.style.top = 300 + 'px';

const chick = document.getElementById('c0');
let x = 0;
let y = 0;
let direction = 1; // 1: 右(下), -1: 左(上)
let speed = 1;
let maxX = 300;
let maxY = 300;
let stop = 0;//[0]動作可能 [1]停止
let still=0;//[0]未実行 [1]実行中
x=100;
y=100;
chickInit();
//ひよこ初期位置
function chickInit()
{
	chick.style.left = 100 + 'px';
	chick.style.top = 100 + 'px';
}
//上下移動
function moveUpDown()
{
	if(stop==1)
	{
		still=0;
		return;
	}
	// 左右移動
	y += speed * direction;

	// 画面端に到達したら方向転換
	if (y+50 >= maxY || y <= 0) 
	{
		direction *= -1;
	}
	chick.style.left = x + 'px';
	chick.style.top = y + 'px';
	
	requestAnimationFrame(moveUpDown);
}
//左右移動
function moveLeftRight() 
{
	if(stop==1)
	{
		still=0;
		return;
	}
	// 左右移動
	x += speed * direction;
	// 画面端に到達したら方向転換
	if (x+50 >= maxX || x <= 0) 
	{
		direction *= -1;
	}
	chick.style.left = x + 'px';
	chick.style.top = y + 'px';
	
	requestAnimationFrame(moveLeftRight);
}

if(still==1)
{
  return;
}
still変数はボタンの実行中かどうか?を判定する変数として使用しています。
左右移動ボタンもしくは上下移動ボタンを実行中は実行しないように制御しています。

function moveUpDown()
上下方向の処理を実行する関数です。
y += speed * direction;
y座標を変更することで上下移動の座標を変更します。
1:下方向
-1:上方向

if (y+50 >= maxY || y <= 0)
「y+50 >= maxY」は下方向の判定処理で画面下の枠を超えるかどうか?
「y <= 0」は上方向の判定処理で画面上の枠を超えるかどうか?
の判定をしています。
判定でtrueを返したら上なら下へ、下なら上へ方向転換をします。

requestAnimationFrame(moveUpDown);
処理は繰り返し、動作を継続させるためrequestAnimationFrameで指定の関数を実行させます。

function moveLeftRight()
左右方向の処理を実行する関数です。
x += speed * direction;
x座標を変更することで上下移動の座標を変更します。
1:右方向
-1:左方向

if (x+50 >= maxX || x <= 0)
「x+50 >= maxX」は右方向の判定処理で画面右の枠を超えるかどうか?
「x <= 0」は左方向の判定処理で画面左の枠を超えるかどうか?
の判定をしています。
判定でtrueを返したら左なら右へ、右なら左へ方向転換をします。

requestAnimationFrame(moveLeftRight);
処理は繰り返し、動作を継続させるためrequestAnimationFrameで指定の関数を実行させます。


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


戻る
back

Left/right and up/down movement
Sample using requestAnimationFrame

[sample]


[HTML/CSS]

copy
const b0 = document.getElementById('leftright');
const b1 = document.getElementById('updown');
const b2 = document.getElementById('stop');
//Move left/right
b0.addEventListener("click", () => 
{
	if(still==1)
	{
		return;
	}
	stop=0;
	still=1;
	requestAnimationFrame(moveLeftRight);
});
//Move up/down
b1.addEventListener("click", () => 
{
	if(still==1)
	{
		return;
	}
	stop=0;
	still=1;
	requestAnimationFrame(moveUpDown);
});
b2.addEventListener("click", () => 
{
	stop=1;
});
b0.style.left = 0 + 'px';
b0.style.top = 300 + 'px';
b1.style.left = 100 + 'px';
b1.style.top = 300 + 'px';
b2.style.left = 200 + 'px';
b2.style.top = 300 + 'px';

const chick = document.getElementById('c0');
let x = 0;
let y = 0;
let direction = 1; // 1: Right (bottom), -1: Left (top)
let speed = 1;
let maxX = 300;
let maxY = 300;
let stop = 0; //[0] Operational [1] Stopped
let still = 0; //[0] Not yet executed [1] Running
x=100;
y=100;
chickInit();
//Chick initial position
function chickInit()
{
	chick.style.left = 100 + 'px';
	chick.style.top = 100 + 'px';
}
//Up and down movement
function moveUpDown()
{
	if(stop==1)
	{
		still=0;
		return;
	}
	// Left and right movement
	y += speed * direction;

	// Change direction when you reach the edge of the screen
	if (y+50 >= maxY || y <= 0) 
	{
		direction *= -1;
	}
	chick.style.left = x + 'px';
	chick.style.top = y + 'px';
	
	requestAnimationFrame(moveUpDown);
}
//Left and right movement
function moveLeftRight() 
{
	if(stop==1)
	{
		still=0;
		return;
	}
	// Left and right movement
	x += speed * direction;
	// Change direction when you reach the edge of the screen
	if (x+50 >= maxX || x <= 0) 
	{
		direction *= -1;
	}
	chick.style.left = x + 'px';
	chick.style.top = y + 'px';
	
	requestAnimationFrame(moveLeftRight);
}

if(still==1)
{
  return;
}
The still variable is used to determine whether the button is currently being executed.
This function prevents execution while the left/right or up/down buttons are being executed.

function moveUpDown()
This function performs up/down operations.
y += speed * direction;
Changing the y coordinate changes the up/down movement coordinate.
1: Downward
-1: Upward

if (y+50 >= maxY || y <= 0)
"y+50 >= maxY" determines whether the bottom edge of the screen will be exceeded in the downward direction.
"y <= 0" determines whether the top edge of the screen will be exceeded in the upward direction.
This checks the following.
If the check returns true, the direction will change to downward if the direction is upward, or to upward if the direction is downward.

requestAnimationFrame(moveUpDown);
The process repeats, and the specified function is executed in requestAnimationFrame to continue the movement.

function moveLeftRight()
This function executes left-right movement.
x += speed * direction;
Changing the x coordinate changes the coordinate for up-down movement.
1: Rightward
-1: Leftward

if (x+50 >= maxX || x <= 0)
"x+50 >= maxX" determines whether the direction goes beyond the right border of the screen.
"x <= 0" determines whether the direction goes beyond the left border of the screen.
This checks whether the direction goes beyond the left border of the screen.
If the check returns true, the direction changes to the right if it is left, or to the left if it is right.

requestAnimationFrame(moveLeftRight);
The process repeats, and the specified function is executed by requestAnimationFrame to continue the action.


[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



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