我是Javascript和canvas的新手。我正在制作一个简单的帆布比赛游戏,随机设置速度两个矩形(红色和蓝色)。我把它设置好,所以它们都停在某一点上。当一辆车获胜时,它打印出“Red / Blue Won!”在画布上。
我让比赛部分失效了,但是我仍然坚持要弄清楚如何运行条件语句,把它放在哪里,并在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
答案 0 :(得分:1)
您可以使用Button
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")
});
&#13;
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")
});
};
&#13;
canvas {
border: 1px solid black;
}
&#13;
jsfiddle https://jsfiddle.net/py49rzbu/