简体   繁体   English

如何为JavaScript弹跳球创建可调整大小的HTML5画布?

[英]How to create resizable html5 canvas for javascript bouncing ball?

I am trying to create a rotating bouncing ball using javascript inside an html5 canvas. 我正在尝试使用html5画布内的javascript创建旋转的弹跳球。 I want the canvas to fit the browser window width and height, while being resizable 我希望画布适合浏览器窗口的宽度和高度,同时可调整大小

From researching a solution I have landed on two seperate sections of javascript code but I am not familiar enough with javascript to merge them in a way to make this work 通过研究解决方案,我进入了javascript代码的两个单独的部分,但是我对javascript不够熟悉,无法以某种方式将它们合并

 <canvas id="myCanvas"> <h1>Bouncing Ball</h1> </canvas> <script> window.onload = function() { var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); var x = canvas.width / 2; var y = canvas.height / 2; // SPEED var dx = 2; var dy = -2; draw(); function draw() { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.beginPath(); ctx.arc(x, y, 100, 0, Math.PI * 2); ctx.fillStyle = "#9370DB"; ctx.fill(); ctx.closePath(); if (x + dx > 480) { dx = -dx; } if (x + dx < 0) { dx = -dx; } if (y + dy > 680) { dy = -dy; } if (y + dy < 0) { dy = -dy; } x += dx; y += dy; } setInterval(draw, 10); } </script> <script> (function() { var canvas = document.getElementById("myCanvas"), context = canvas.getContext("2d"); // resize the canvas to fill browser window dynamically window.addEventListener('resize', resizeCanvas, false); function resizeCanvas() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; drawStuff(); } resizeCanvas(); function drawStuff() { // do your drawing stuff here } })(); </script> 

Inside the draw() function you're checking if the balls position exceeds the bounds of the canvas. draw()函数内部,您要检查球的位置是否超出画布的边界。

if (x + dx > 480) {

and

if (y + dy > 680) {

Since you want the ball to stay in bounds even if the dimensions of the window have changed we need to get the actual size of the canvas. 因为即使窗口的尺寸已更改,您也希望球保持在边界内,所以我们需要获取画布的实际大小。 Right at the beginning you defined a variable which holds a reference to the canvas. 就在开始时,您定义了一个变量,该变量包含对画布的引用。

var canvas = document.getElementById("myCanvas");

Using this we can get the actual size by calling canvas.width & canvas.height . 使用此方法,我们可以通过调用canvas.widthcanvas.height获得实际大小。

So the above two if statements become: 因此,以上两个if语句变为:

if (x + dx > canvas.width) {
if (y + dy > canvas.height) {

Now if the user resizes the window, the following function will be called: 现在,如果用户调整窗口大小,将调用以下函数:

function resizeCanvas() {
  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;
}

As you can see, we're updating the width & height properties of the canvas with the size of the window. 如您所见,我们正在使用窗口的大小更新画布的width和height属性。

The rest is just combining your two snippets into one - which is not a big deal. 剩下的只是将您的两个摘要合并为一个-没什么大不了的。 Here's a working example: 这是一个工作示例:

 var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); var x = canvas.width / 2; var y = canvas.height / 2; // SPEED var dx = 2; var dy = -2; var radius = 100; function draw() { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.beginPath(); ctx.arc(x, y, radius, 0, Math.PI * 2); ctx.fillStyle = "#9370DB"; ctx.fill(); ctx.closePath(); if (x + dx > canvas.width - radius) { dx = -dx; } if (x + dx < radius) { dx = -dx; } if (y + dy > canvas.height - radius) { dy = -dy; } if (y + dy < radius) { dy = -dy; } x += dx; y += dy; } // resize the canvas to fill browser window dynamically window.addEventListener('resize', resizeCanvas, false); function resizeCanvas() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; } resizeCanvas(); x = canvas.width / 2; y = canvas.height / 2; setInterval(draw, 10); 
 <canvas id="myCanvas"> <h1>Bouncing Ball</h1> </canvas> 

I think that you should change the "bouncing" statement, I would be using radius instead of dx and dy. 我认为您应该更改“弹跳”语句,我将使用半径而不是dx和dy。 Like, if x + radius of the circle is greater than width then reverse the direction, in this case your radius is 100 so: 例如,如果x +圆的半径大于宽度,则反转方向,在这种情况下,半径为100,因此:

<canvas id="myCanvas">
    <h1>Bouncing Ball</h1>
</canvas>

<script>
  window.onload = function() {

    var canvas = document.getElementById("myCanvas");
    var ctx = canvas.getContext("2d");
    var x = canvas.width / 2;
    var y = canvas.height / 2;
    // SPEED
    var dx = 2;
    var dy = -2;

    draw();

    function draw() {

      ctx.clearRect(0, 0, canvas.width, canvas.height);
      ctx.beginPath();

      ctx.arc(x, y, 100, 0, Math.PI * 2);
      ctx.fillStyle = "#9370DB";
      ctx.fill();
      ctx.closePath();

      if (x + 100 > canvas.width) {
        dx = -dx;
      }

      if (x - 100 < 0) {
        dx = +dx;
      }

      if (y + 100 > canvas.height) {
        dy = -dy;
      }

      if (y - 100 < 0) {
        dy = +dy;
      }

      x += dx;
      y += dy;
    }

    setInterval(draw, 10);
  }
</script>

<script>
  (function() {
    var canvas = document.getElementById("myCanvas"),
      context = canvas.getContext("2d");

    // resize the canvas to fill browser window dynamically
    window.addEventListener('resize', resizeCanvas, false);

    function resizeCanvas() {
      canvas.width = window.innerWidth;
      canvas.height = window.innerHeight;

      drawStuff();
    }
    resizeCanvas();

    function drawStuff() {
      // do your drawing stuff here
    }
  })();
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM