javascript - 球在不同大小的 Canvas 中弹跳 javascript

标签 javascript html canvas

因此,对于我正在进行的一个项目,我想要四个不同大小的 Canvas ,其中四个球以相同的速度从墙上弹起。当所有 Canvas 大小相同时,代码工作正常,但是一旦我尝试调整 Canvas 高度和宽度,球就会继续在第一个 Canvas 大小的区域中弹跳。我不确定我做错了什么,我真的很感激一些帮助。我对 JavaScript 还很陌生。

这是 HTML:

<canvas id="canvas1" width="200" height="200"></canvas>
<canvas id="canvas2" width="200" height="400"></canvas>
<canvas id="canvas3" width="400" height="200"></canvas>
<canvas id="canvas4" width="200" height="200"></canvas>

这是 Javascript:

var canvas = document.getElementById("canvas1");
var ctx = canvas.getContext("2d");
var x = canvas.width/2;
var y = canvas.height-30;
var dx = -2;
var dy = 2;
var ballRadius = 10;

var canvas2 = document.getElementById("canvas2");
var ctx2 = canvas2.getContext("2d");
var x2 = canvas2.width/2;
var y2 = canvas2.height-30;

var canvas3 = document.getElementById("canvas3");
var ctx3 = canvas3.getContext("2d");
var x3 = canvas3.width/2;
var y3 = canvas3.height-30;

var canvas4 = document.getElementById("canvas4");
var ctx4 = canvas4.getContext("2d");
var x4 = canvas4.width/2;
var y4 = canvas4.height-30;

function drawBall() {
    ctx.beginPath();
    ctx.arc(x, y, ballRadius, 0, Math.PI*2);
    ctx.fillStyle = "#0095DD";
    ctx.fill();
    ctx.closePath();

    ctx2.beginPath();
    ctx2.arc(x2, y2, ballRadius, 0, Math.PI*2);
    ctx2.fillStyle = "#0095DD";
    ctx2.fill();
    ctx2.closePath();

    ctx3.beginPath();
    ctx3.arc(x3, y3, ballRadius, 0, Math.PI*2);
    ctx3.fillStyle = "#0095DD";
    ctx3.fill();
    ctx3.closePath();

    ctx4.beginPath();
    ctx4.arc(x4, y4, ballRadius, 0, Math.PI*2);
    ctx4.fillStyle = "#0095DD";
    ctx4.fill();
    ctx4.closePath();
}

function draw() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx2.clearRect(0, 0, canvas2.width, canvas2.height);
    ctx3.clearRect(0, 0, canvas3.width, canvas3.height);
    ctx4.clearRect(0, 0, canvas4.width, canvas4.height);

    drawBall();
    x += dx;
    y += dy;
    x2 += dx;
    y2 += dy;
    x3 += dx;
    y3 += dy;
    x4 += dx;
    y4 += dy;

    if(y + dy > canvas.height-ballRadius || y + dy < ballRadius) {
        dy = -dy; }
    if(x + dx > canvas.width-ballRadius || x + dx < ballRadius) {
        dx = -dx; }

    if(y2 + dy > canvas2.height-ballRadius || y2 + dy < ballRadius) {
        dy = -dy; }
    if(x2 + dx > canvas2.width-ballRadius || x2 + dx < ballRadius) {
        dx = -dx; }

    if(y3 + dy > canvas3.height-ballRadius || y3 + dy < ballRadius) {
        dy = -dy; }
    if(x3 + dx > canvas3.width-ballRadius || x3 + dx < ballRadius) {
        dx = -dx; }

    if(y4 + dy > canvas4.height-ballRadius || y4 + dy < ballRadius) {
        dy = -dy; }
    if(x4 + dx > canvas4.width-ballRadius || x4 + dx < ballRadius) {
        dx = -dx; }
    }
    setInterval(draw, 10);

最佳答案

问题在于所有球都具有相同的速度(dx,dy),因此当其中一个球撞到墙壁时,会导致所有球反弹。要解决这个问题,您需要知道每个球的速度(dx2、dy2 等)

关于javascript - 球在不同大小的 Canvas 中弹跳 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36969913/

相关文章:

html - beautifulSoup解析标签表HTML,尤其是colspan和rowspan

javascript - 在 Canvas 上旋转 2 个图像

javascript - 我需要一个系统来发布 JavaScript 游戏的高分,但它必须很难被愚弄

javascript - formvalidation.io 中的表单验证多个表单之间存在冲突

javascript - 使用 CSS 和 JavaScript 的总客户端宽度

javascript - 处理 SQL 时出错 : TypeError: window. openDatabase 不是函数

javascript - jQuery自动滚动滚动条

events - Fabric.js - 同步对象 :modified event to another client

javascript - 如何使用 JSOM 从 SharePoint 列表中获取数据?

javascript - 如何使用 redux-observable 正确获取二进制图像?