gpt4 book ai didi

javascript - 如何确定哪个 Canvas 对象首先完成其随机速度动画

转载 作者:行者123 更新时间:2023-11-27 22:59:06 27 4
gpt4 key购买 nike

我是 Javascript 和 canvas 的新手。我正在开发一个简单的 Canvas 竞赛游戏,它随机设置两个矩形(红色和蓝色)的速度。我已经设置好了,所以它们都停在某个点。当一辆车获胜时,它会打印出“Red/Blue Won!”在 Canvas 上。

我已经把赛车部分写下来了,但我一直在想办法如何运行条件语句,把它放在哪里,并在 1 个矩形首先到达比赛终点/某个点时将其打印出来。

 if (square1 gets passed finish line first or stops moving)  {
// Draw to canvas
context.font = "20pt sans-serif";
context.fillText("Red is The Winner!", 5, 25, 300);
context.fillStyle = "red";
}
else if (square2 gets passed finish line first or stops moving) {

context.font = "20pt sans-serif";
context.fillText("Blue is The Winner!", 5, 280, 300);
context.fillStyle = "blue";
}

我不确定这是否是我使用时间制作动画的方式。任何帮助将非常感激。

这是动画代码

  var animateRed = function(prop, val, duration) {
// The calculations required for the step function
var start = new Date().getTime();
var end = start + duration;
var current = square1[prop];
var distance = val - current;

var step = function() {
// Get our current progres
var timestamp = new Date().getTime();
var progress = Math.min((duration - (end - timestamp)) / duration, 1);


// Update the square's property
square1[prop] = current + (distance * progress);
// If the animation hasn't finished, repeat the step.
if (progress < 1) requestAnimationFrame(step);
};

// Start the animation
return step();
};

var animateBlue = function(prop, val, duration) {
// The calculations required for the step function
var start = new Date().getTime();
var end = start + duration;
var current = square2[prop];
var distance = val - current;

var step = function() {
// Get our current progres
var timestamp = new Date().getTime();
var progress = Math.min((duration - (end - timestamp)) / duration, 1);


// Update the square's property
square2[prop] = current + (distance * progress);
// If the animation hasn't finished, repeat the step.
if (progress < 1) requestAnimationFrame(step);
};

// Start the animation
return step();
};


$("#go").on('click', function() {
//Math.floor(Math.random() * 1000) + 100;
var speedRed = Math.floor(Math.random() * 1000) + 500;
var speedBlue = Math.floor(Math.random() * 1000) + 500;
animateRed('x', 450, speedRed);
animateBlue('x', 450, speedBlue);
});

**************** 更新了 Fiddle ********************不小心发错了

这是我的 fiddle :Fiddle

最佳答案

您可以使用Promise.race()

var animateRed = function(prop, val, duration) {
// The calculations required for the step function
return new Promise(function(resolve) {
var start = new Date().getTime();
var end = start + duration;
var current = square1[prop];
var distance = val - current;
// var speedRed = Math.floor(Math.random() * (90 - 20) + 20);
//Math.floor(Math.random() * 2);


var step = function() {
// Get our current progres
var timestamp = new Date().getTime();
var progress = Math.min((duration - (end - timestamp)) / duration, 1);
// Update the square's property
square1[prop] = current + (distance * progress);
// If the animation hasn't finished, repeat the step.
if (progress < 1) {
requestAnimationFrame(step)
} else {
resolve("red")
}
};

// Start the animation
step();
})
};

var animateBlue = function(prop, val, duration) {
// The calculations required for the step function
return new Promise(function(resolve) {
var start = new Date().getTime();
var end = start + duration;
var current = square2[prop];
var distance = val - current;
// var speedBlue = Math.floor(Math.random() * (90 - 20) + 20);

var step = function() {
// Get our current progres
var timestamp = new Date().getTime();
var progress = Math.min((duration - (end - timestamp)) / duration, 1);
// Update the square's property
square2[prop] = current + (distance * progress);
// If the animation hasn't finished, repeat the step.
if (progress < 1) {
requestAnimationFrame(step)
} else {
resolve("blue")
}
};

// Start the animation
step();
})
};

Promise.race([animateRed('x', 450, speedRed),
animateBlue('x', 450, speedBlue)
])
.then(function(winner) {
alert(winner + " wins")
});

var canvas = document.getElementById('canvas');
var goBtn = document.getElementById('go');
goBtn.addEventListener('click', render, false);

if (canvas.getContext) {
// Grab our context
var context = canvas.getContext('2d');

// Make sure we have a valid defintion of requestAnimationFrame
var requestAnimationFrame =
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.msRequestAnimationFrame ||
window.oRequestAnimationFrame ||
function(callback) {
return setTimeout(callback, 16);
};

// Let's define our square
var square1 = {
'x': 0,
'y': 50,
'width': 50,
'height': 50,
'fill': '#FF0000'
};

// Let's define our square
var square2 = {
'x': 0,
'y': 120,
'width': 50,
'height': 50,
'fill': '#4169E1'
};

var render = function() {
// Clear the canvas
context.clearRect(0, 0, canvas.width, canvas.height);

// Draw the square
context.beginPath();
context.rect(square1.x, square1.y, square1.width, square1.height);
context.fillStyle = square1.fill;
context.fill();

// Draw the square
context.beginPath();
context.rect(square2.x, square2.y, square2.width, square2.height);
context.fillStyle = square2.fill;
context.fill();

// Finish Line
context.beginPath();
context.strokeStyle = 'black';
context.moveTo(canvas.width - 110, 0);
context.lineTo(canvas.width - 110, 290);
context.globalCompositeOperation = "destination-over";
context.lineWidth = 10;
context.stroke();

/*
context.font = "20pt sans-serif";
context.fillText("Red is The Winner!", 5, 25, 300);
context.fillStyle = '#FF0000';


context.font = "20pt sans-serif";
context.fillText("Blue is The Winner!", 5, 280, 300);
context.fillStyle = "red";
*/
// Redraw
requestAnimationFrame(render);
};

// Start the redrawing process
render();

var animateRed = function(prop, val, duration) {
// The calculations required for the step function
return new Promise(function(resolve) {
var start = new Date().getTime();
var end = start + duration;
var current = square1[prop];
var distance = val - current;
// var speedRed = Math.floor(Math.random() * (90 - 20) + 20);
//Math.floor(Math.random() * 2);


var step = function() {
// Get our current progres
var timestamp = new Date().getTime();
var progress = Math.min((duration - (end - timestamp)) / duration, 1);
// Update the square's property
square1[prop] = current + (distance * progress);
// If the animation hasn't finished, repeat the step.
if (progress < 1) {
requestAnimationFrame(step)
} else {
resolve("red")
}
};

// Start the animation
step();
})
};

var animateBlue = function(prop, val, duration) {
// The calculations required for the step function
return new Promise(function(resolve) {
var start = new Date().getTime();
var end = start + duration;
var current = square2[prop];
var distance = val - current;
// var speedBlue = Math.floor(Math.random() * (90 - 20) + 20);

var step = function() {
// Get our current progres
var timestamp = new Date().getTime();
var progress = Math.min((duration - (end - timestamp)) / duration, 1);
// Update the square's property
square2[prop] = current + (distance * progress);
// If the animation hasn't finished, repeat the step.
if (progress < 1) {
requestAnimationFrame(step)
} else {
resolve("blue")
}
};

// Start the animation
step();
})
};

if (animateRed() < animateBlue()) {
context.font = "20pt sans-serif";
context.fillText("Red is The Winner!", 5, 25, 300);
context.fillStyle = '#FF0000';
}

//Math.floor(Math.random() * 1000) + 100;
var speedRed = Math.floor(Math.random() * 1000) + 500;
var speedBlue = Math.floor(Math.random() * 1000) + 500;
Promise.race([animateRed('x', 450, speedRed),
animateBlue('x', 450, speedBlue)
])
.then(function(winner) {
alert(winner + " wins")
});

};
canvas {
border: 1px solid black;
}
<canvas width='500' height='300' id='canvas'>Your browser does not support canvas - go get Chrome!</canvas>

<button class="goBtn" id="go">Go</button>

jsfiddle https://jsfiddle.net/py49rzbu/

关于javascript - 如何确定哪个 Canvas 对象首先完成其随机速度动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37289915/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com