| 半円の弧を通り上から下に動く |
| 左側の半円が終わったら右側の半円を動くサンプル |
const chick = document.getElementById('chick');
const stopBtn = document.getElementById('stop');
const wall = document.getElementById('wall');
//停止ボタン
stopBtn.style.left = 0 + 'px';
stopBtn.style.top = 300 + 'px';
//下側の白色の壁(下枠に移動したキャラが徐々に見えなくなるようにするため)
wall.style.left = 0 + 'px';
wall.style.top = 300 + 'px';
let stop = 0;//[0]動作可能 [1]停止
let still = 0;//[0]未実行 [1]実行中
let animId = null;
//開始角度と終了角度
const startAngleLeft = -Math.PI / 2;// 上端(北)を指す角度
let thetaLeft = startAngleLeft;
//右側の半円に使用
const startAngleRight = -Math.PI / 2; // 上端
const endAngleRight = Math.PI / 2; // 下端
//左側の半円に使用
const endAngleLeft = startAngleLeft - Math.PI;
let thetaRight = startAngleRight;
//角度増分
const speed = 0.02;
let cx = 50;
let cy = 50;
const r = 50;
//キャラの初期位置
chick.style.left = 50 + 'px';
chick.style.top = 50 + 'px';
let direction = 0;
//再帰的にアニメーションを呼び出し
requestAnimationFrame(test);
//上下移動
function test()
{
//キャラクター(小さな円)を描画
if(direction == 0)
{
//左
x = cx + r * Math.cos(thetaLeft);
y = cy + r * Math.sin(thetaLeft);
}
else
{
//右
x = cx + r * Math.cos(thetaRight);
y = cy + r * Math.sin(thetaRight);
}
//キャラクターの位置を更新
chick.style.left = x + 'px';
chick.style.top = y + 'px';
//角度を更新し、ゴールに達するまでループ
//左
if(direction == 0)
{
if (thetaLeft > endAngleLeft)
{
thetaLeft -= speed;
}
else
{
thetaLeft = startAngleLeft;
//次のy座標
//50:r(半径)の高さの分だけ下にします
cy=y+50;
direction=1;
}
}
else
{
//右
if (thetaRight < endAngleRight)
{
thetaRight += speed;
}
else
{
thetaRight = startAngleRight;
//次のy座標
//50:キャラの高さの分だけ下にします
cy = y + 50;
direction = 0;
}
}
if(y<300)
{
//再帰的にアニメーションを呼び出し
animId=requestAnimationFrame(test);
}
}
//停止ボタン
stopBtn.addEventListener('click', () =>
{
cancelAnimationFrame(animId);
});
「円のパラメータ表示」を使って、ひよこを半円ずつ(左半円→右半円→左半円…)上から下へと連結させて動かしています。
if(direction == 0)
{
//左
x = cx + r * Math.cos(thetaLeft);
y = cy + r * Math.sin(thetaLeft);
}
下記のステップのように上から左側に90度の弧を描いて動くための座標を準備します。
if(direction == 0)
{
if (thetaLeft > endAngleLeft)
{
thetaLeft -= speed;
}
else
{
thetaLeft = startAngleLeft;
//次のy座標
//50:r(半径)の高さの分だけ下にします
cy=y+50;
direction=1;
}
}
if(y<300)
{
//再帰的にアニメーションを呼び出し
animId=requestAnimationFrame(test);
}
stopBtn.addEventListener('click', () =>
{
cancelAnimationFrame(animId);
});
| Moves from top to bottom through a semicircular arc |
| After the left semicircle is completed, move the right semicircle. |
const chick = document.getElementById('chick');
const stopBtn = document.getElementById('stop');
const wall = document.getElementById('wall');
//stop button
stopBtn.style.left = 0 + 'px';
stopBtn.style.top = 300 + 'px';
//The white wall at the bottom (to make characters that move to the bottom frame gradually disappear from view)
wall.style.left = 0 + 'px';
wall.style.top = 300 + 'px';
let stop = 0;//[0] Operational [1] Stopped
let still = 0;//[0] Not running [1] Running
let animId = null;
//Start and end angles
const startAngleLeft = -Math.PI / 2;// Angle pointing to the top (north)
let thetaLeft = startAngleLeft;
//Used for the right semicircle
const startAngleRight = -Math.PI / 2; // top end
const endAngleRight = Math.PI / 2; // v
//Used for the left semicircle
const endAngleLeft = startAngleLeft - Math.PI;
let thetaRight = startAngleRight;
//Angle Increment
const speed = 0.02;
let cx = 50;
let cy = 50;
const r = 50;
//Character's initial position
chick.style.left = 50 + 'px';
chick.style.top = 50 + 'px';
let direction = 0;
//Recursively calling animations
requestAnimationFrame(test);
//Up and down movement
function test()
{
//Draw a character (small circle)
if(direction == 0)
{
//left
x = cx + r * Math.cos(thetaLeft);
y = cy + r * Math.sin(thetaLeft);
}
else
{
//right
x = cx + r * Math.cos(thetaRight);
y = cy + r * Math.sin(thetaRight);
}
//Update character position
chick.style.left = x + 'px';
chick.style.top = y + 'px';
//Update the angle and loop until the goal is reached
//left
if(direction == 0)
{
if (thetaLeft > endAngleLeft)
{
thetaLeft -= speed;
}
else
{
thetaLeft = startAngleLeft;
//Next y coordinate
//50:Lower it by the height of a radius
cy=y+50;
direction=1;
}
}
else
{
//right
if (thetaRight < endAngleRight)
{
thetaRight += speed;
}
else
{
thetaRight = startAngleRight;
//Next y coordinate
//50:Lower it by the height of the character
cy = y + 50;
direction = 0;
}
}
if(y<300)
{
//Recursively calling animations
animId=requestAnimationFrame(test);
}
}
//Stop button
stopBtn.addEventListener('click', () =>
{
cancelAnimationFrame(animId);
});
Using "Circle Parametric Display," the chick moves in connected semicircles from top to bottom (left semicircle → right semicircle → left semicircle, etc.).
if(direction == 0)
{
//left
x = cx + r * Math.cos(thetaLeft);
y = cy + r * Math.sin(thetaLeft);
}
Prepare the coordinates to move in a 90-degree arc from top to left as shown in the steps below.
if(direction == 0)
{
if (thetaLeft > endAngleLeft)
{
thetaLeft -= speed;
}
else
{
thetaLeft = startAngleLeft;
//Next y coordinate
//50:Lower it by the height of a radius
cy=y+50;
direction=1;
}
}
if(y<300)
{
//Recursively calling animations
animId=requestAnimationFrame(test);
}
stopBtn.addEventListener('click', () =>
{
cancelAnimationFrame(animId);
});
| ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。 ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、 または公開したりすることはできません。 当方は、ホームペーよびアプリ利用したいかなる理由によっての障害等が発生しても、 その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について 一切の責任を負わないものとします。 |