// init variables
var canvas = document.getElementById('canvas'),
context = canvas.getContext('2d'),
width = canvas.width = window.innerWidth,
height = canvas.height = window.innerHeight,
halfWidth = width/2,
halfHeight = height/2,
deadLine = height - 100,
fov = 250,
offScreenY = -100,
currentPosition = 0,
balls = [],
noteSpeed = 0.3,
animId;
var sound = new Howl({
src: ['https://rawcdn.githack.com/noeszc/atwood/master/assets/sounds/mario.ogg'],
onload: function(){ setup(); },
onend : function(){ stop(); }
});
function setup(){
sound.play();
sound.mute();
drawDeadLine();
gameLoop();
}
function gameLoop() {
animId = requestAnimationFrame(gameLoop, canvas);
context.clearRect(0, 0, width, height);
updateNotes();
drawDeadLine();
}
function updateNotes() {
currentPosition = (sound.seek() * 1000);
notes.forEach(function(note, index) {
var notePosition = (currentPosition * noteSpeed) - ((note.position * noteSpeed) - deadLine);
if (notePosition > offScreenY && notePosition < height) {
var ball = new Ball3d(5, '#000000');
var scale = (fov + notePosition)/fov;
ball.x = halfWidth;
ball.y = halfHeight + notePosition * scale;
ball.scaleX = ball.scaleY = scale;
ball.draw(context);
balls.push(ball);
} else {
// elimino la nota
balls.splice(index, 1);
}
});
}
// dibuja la linea base donde deben llegar las notas al ritmo
function drawDeadLine() {
context.beginPath();
context.moveTo(0, deadLine);
context.lineTo(width, deadLine);
context.stroke();
}
function stop() {
context.clearRect(0, 0, width, height);
cancelAnimationFrame(animId);
drawDeadLine();
console.log('======= end music =========');
console.log(balls);
}
body {
margin: 0;
}
canvas {
display: block;
}
<script src="https://rawcdn.githack.com/noeszc/atwood/master/scripts/ball3D.js"></script>
<script src="https://rawcdn.githack.com/noeszc/atwood/master/scripts/notes.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/howler/2.0.2/howler.min.js"></script>
<canvas id="canvas"></canvas>
很難知道你想要什麼。在猜測嘗試'ball.y =高度 - notePosition *規模;' – Blindman67
感謝您的迴應,我嘗試的是筆記來自遙遠的地方,如以下[參考](https://www.youtube.com /手錶?ν= hCumqHZ0Yi8) –