<canvas id="canvas"></canvas>
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
canvas.width = window.innerWidth * devicePixelRatio;
canvas.height = window.innerHeight * devicePixelRatio;
const canvasW = canvas.width;
const canvasH = canvas.height;
const myNumber = 200; //推移させたい数値の最大値(絶対値)
const division = 1000; //Date.nowをそのまま使うと周期が早すぎるので、希望の数値で割る
const digit = 3; //表示したい小数点以降の桁数
const startTime = Date.now() / division; //アニメーション開始時間
let elapsedTime = 0; //アニメーション開始時間からの経過時間を格納
let x = 0;
function tornado() {
elapsedTime = Date.now() / division - startTime; //開始時間-関数内で現在時間を引けば経過時間が得られる
const period = Math.sin(elapsedTime).toFixed(digit) * myNumber; //-200 〜 +200の間を推移
let periodY = period + 200; //0 〜 +400の間を推移
x++;
ctx.beginPath();
ctx.arc(x,periodY,5,0,Math.PI*2);
ctx.fill();
if (x > canvasW) {
x = 0;
periodY = 200;
}
ctx.stroke();
requestAnimationFrame(tornado);
}
tornado();
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.