javascript animation カラフルボール
参考サイト<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>カラフル</title></head><body> <p style="color:rgb(255, 0, 170);font-size:30px;position: fixed;top:150px;left:120px;">カラフル</p> <canvas id="canvas" width="400" height="400"></canvas> <script> const canvas = document.getElementById("canvas"); const context = canvas.getContext("2d"); let rrr; const tama = { x: 200, y: 200, vx: 5, vy: 2, radius: 25, color1: "blue", color2: "red", color3: "green", color4: "yellow", paint1() { context.beginPath(); context.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true); context.closePath(); context.fillStyle = this.color1; context.fill(); }, paint2() { context.beginPath(); context.arc(this.y, this.x, this.radius, 0, Math.PI * 2, true); context.closePath(); context.fillStyle = this.color2; context.fill(); }, paint3() { context.beginPath(); context.arc(400-this.x, 400-this.y, this.radius, 0, Math.PI * 2, true); context.closePath(); context.fillStyle = this.color3; context.fill(); }, paint4() { context.beginPath(); context.arc(400-this.y, 400-this.x, this.radius, 0, Math.PI * 2, true); context.closePath(); context.fillStyle = this.color4; context.fill(); }, }; function paint() { context.clearRect(0, 0, canvas.width, canvas.height); tama.paint1(); tama.paint2(); tama.paint3(); tama.paint4(); tama.x += tama.vx; tama.y += tama.vy; tama.vy *= 0.99; tama.vy += 0.25; if ( tama.y + tama.vy > canvas.height - tama.radius || tama.y + tama.vy < tama.radius ) { tama.vy = -tama.vy; } if ( tama.x + tama.vx > canvas.width - tama.radius || tama.x + tama.vx < tama.radius ) { tama.vx = -tama.vx; } rrr = window.requestAnimationFrame(paint); } rrr = window.requestAnimationFrame(paint); tama.paint(); </script></body></html>実験サイト