更新

时间:2016-01-24 20:18:06

标签: javascript jquery canvas

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

DEMO: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();

1 个答案:

答案 0 :(得分:1)

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

我修复了第一个问题:你同时拥有$ canvas,canvas。我删除了$ canvas:

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

这是第二个问题的解决方法。还因为画布,画布的混乱。改变第一线:

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