【问题标题】:Canvas gets width and height after resize + bug on update调整大小后画布获得宽度和高度 + 更新错误
【发布时间】:2016-01-24 20:18:05
【问题描述】:

我最近开始使用画布。我想在画布上的 mousemove 上移动元素。这不是问题,但后来我添加了一个调整大小的功能以使其流畅。之后,即使我设置了宽度和高度,画布的宽度和高度也会在调整大小后设置(因此您必须调整窗口大小以获得画布的宽度和高度,否则它没有宽度和高度)以及“眼睛“他们自己离开了轨道,所以画布没有清除或更新(?)但我不知道为什么。任何帮助将不胜感激。

演示:https://jsfiddle.net/62jkx9rj/2/

JS:

    window.requestAnimFrame = (function() {
      return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
        function(callback) {
          window.setTimeout(callback, 1000 / 60);
        };
    })();

    var ctx,
      widthCanvas,
      heightCanvas,
      leftEye,
      rightEye,
      mouse;

    Eye = function(pos) {
      this.pos = {
        x: pos.x,
        y: pos.y
      };
      this.center = {
        x: pos.x,
        y: pos.y
      };
      this.translation = {
        x: (window.innerWidth / 2 - canvas.width / 2) + this.center.x,
        y: this.center.y
      };
    }

    Eye.prototype.draw = function() {
      ctx.beginPath();
      ctx.arc(this.pos.x, this.pos.y, 7, 0, Math.PI * 2);
      ctx.fillStyle = '#333';
      ctx.fill();

      ctx.beginPath();
      ctx.arc(this.pos.x, this.pos.y - 4, 3, 0, Math.PI * 2);
      ctx.fillStyle = '#fff';
      ctx.fill();
    }

    Eye.prototype.update = function() {
      var deltaX = mouse.x - this.translation.x;
      var deltaY = mouse.y - this.translation.y;
      var mag = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
      var angleRad = Math.atan2(deltaY, deltaX);
      var newPosX = this.center.x + 6 * Math.cos(angleRad);
      var newPosY = this.center.y + 6 * Math.sin(angleRad);
      this.pos.x += (newPosX - this.pos.x) / 5;
      this.pos.y += (newPosY - this.pos.y) / 5;
    }

    var init = function() {
      var canvas = document.getElementById("canvas");
      var $canvas = $('#canvas');
      ctx = canvas.getContext('2d');
      container = $("body");
      widthCanvas = 300;
      heightCanvas = 250;

      $(window).resize(resizeCanvas);
      function resizeCanvas() {
        widthCanvas = $canvas.attr('width', $(container).width());
        heightCanvas = $canvas.attr('height', $(container).height());

      }
      resizeCanvas();
      canvas.width = widthCanvas;
      canvas.height = heightCanvas;
      leftEye = new Eye({
        x: 130,
        y: 95
      });
      rightEye = new Eye({
        x: 160,
        y: 85
      });
      mouse = {
        x: 0,
        y: 0
      };
      bindEventHandlers();
      draw();
    }

    var draw = function() {
      ctx.clearRect(0, 0, widthCanvas, heightCanvas);
      leftEye.update();
      rightEye.update();
      leftEye.draw();
      rightEye.draw();
      requestAnimFrame(draw);
    }

    var bindEventHandlers = function() {
      document.onmousemove = function(e) {
        mouse.x = e.pageX;
        mouse.y = e.pageY;
      }
    }

    init();

【问题讨论】:

  • 你能写几句关于预期行为的句子吗?我只看到一个红点
  • 尝试调整 jsfiddle 结果窗口的大小,您将看到画布

标签: javascript jquery canvas


【解决方案1】:

这是工作版本:https://jsfiddle.net/62jkx9rj/6/

我已经解决了第一个问题:你有两个 $canvas,canvas。我删除了 $canvas:

function resizeCanvas() {
  widthCanvas = canvas.width = $(container).width();
  heightCanvas = canvas.height = $(container).height();
}

这是解决第二个问题的方法。也是因为$canvas、canvas的混淆。更改第一行绘制:

var draw = function() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);

【讨论】:

  • 感谢您对 Gavriel 的帮助。这是绝对正确的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-15
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 2023-02-07
相关资源
最近更新 更多