gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
class Turntable { constructor(canvasId) { this.canvas =…
- User
class Turntable { constructor(canvasId) { this.canvas = document.getElementById(canvasId); this.ctx = this.canvas.getContext('2d'); this.refreshButton = document.getElementById('update'); this.rotateButton = document.getElementById('rotate'); this.isSpinning = false; this.spinSpeed = 0.2; this.decelerationRate = 0.01; this.numberOfSectors = 0; // 存储扇形数量
this.refreshButton.addEventListener('click', () => { this.refreshTurntable(); }); this.rotateButton.addEventListener('click', () => { this.startSpinAnimation(); }); } refreshTurntable() { this.numberOfSectors = this.generateRandomNumberOfSectors(); // 更新扇形数量 this.clearTurntable(); this.drawSectors(this.numberOfSectors); } generateRandomNumberOfSectors() { return Math.floor(Math.random() * 8) + 3; // 生成3-10之间的随机整数 } clearTurntable() { this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height); } drawSectors(numberOfSectors) { const sectorAngle = this.calculateSectorAngle(numberOfSectors); const centerX = this.canvas.width / 2; const centerY = this.canvas.height / 2; const radius = Math.min(this.canvas.width, this.canvas.height) / 2 - 20; for (let i = 0; i < numberOfSectors; i++) { const startAngle = i * sectorAngle; const endAngle = (i + 1) * sectorAngle; this.ctx.beginPath(); this.ctx.moveTo(centerX, centerY); this.ctx.arc(centerX, centerY, radius, startAngle, endAngle); this.ctx.closePath(); const hue = (i * 360) / numberOfSectors; const randomColor = this.getRandomColor(); this.ctx.fillStyle = randomColor; this.ctx.fill(); } } getRandomColor() { const letters = '0123456789ABCDEF'; let color = '#'; for (let i = 0; i < 6; i++) { color += letters[Math.floor(Math.random() * 16)]; } return color; } spinAnimation() { this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height); // 清除画布 this.ctx.translate(this.canvas.width / 2, this.canvas.height / 2); this.ctx.rotate(this.spinSpeed); this.ctx.translate(-this.canvas.width / 2, -this.canvas.height / 2); const sectorAngle = this.calculateSectorAngle(this.numberOfSectors); const centerX = this.canvas.width / 2; const centerY = this.canvas.height / 2; const radius = Math.min(this.canvas.width, this.canvas.height) / 2 - 20; for (let i = 0; i < this.numberOfSectors; i++) { const startAngle = i * sectorAngle; const endAngle = (i + 1) * sectorAngle; this.ctx.beginPath(); this.ctx.moveTo(centerX, centerY); this.ctx.arc(centerX, centerY, radius, startAngle, endAngle); this.ctx.closePath(); const hue = (i * 360) / this.numberOfSectors; const randomColor = this.getRandomColor(); this.ctx.fillStyle = randomColor; this.ctx.fill(); } this.spinSpeed -= this.decelerationRate; if (this.spinSpeed > 0) { requestAnimationFrame(() => this.spinAnimation()); } else { this.isSpinning = false; } } calculateSectorAngle(numberOfSectors) { return (2 * Math.PI) / numberOfSectors; } startSpinAnimation() { if (this.isSpinning) return; this.isSpinning = true; this.spinSpeed = 0.2; this.spinAnimation(); } - Assistant
要使旋转转盘的颜色在旋转前后保持一致,可以在调用
drawSectors方法绘制扇形时,先生成一个随机颜色,然后将该颜色赋给所有的扇形。这样无论旋转转盘的角度如何改变,扇形的颜色都将保持一致。在
drawSectors方法中,将以下代码:const randomColor = this.getRandomColor(); this.ctx.fillStyle = randomColor;替换为:
const color = this.getRandomColor(); this.ctx.fillStyle = color;然后,在
spinAnimation方法中,将以下代码:const randomColor = this.getRandomColor(); this.ctx.fillStyle = randomColor;替换为:
this.ctx.fillStyle = color;这样就能保持旋转转盘的扇形颜色不发生改变了。