【问题标题】:HTML5: canvas animated not work in actionHTML5:画布动画不起作用
【发布时间】:2016-06-16 10:42:12
【问题描述】:

我使用以下代码在我的 div 上显示 HTML5 画布动画: HTML:

<div class="ls-slide" id="asb">

  <div id="smile" style="width:100%;height:359px;">

    <canvas style="position: absolute; width: 100%; z-index: 3; display: block;" height="359" width="2111">

    </canvas>

  </div>
</div>

JS:

window.onload = function() {
  travers();

  var lastHeight = $("#asb").height();



  var canvas = document.querySelector('canvas');
  ctx = canvas.getContext('2d');

  color = '#aaa';
  var check = respondCanvas();
  canvas.width = check[1];
  canvas.height = lastHeight;
  canvas.style.display = 'block';
  ctx.fillStyle = color;
  ctx.lineWidth = .1;
  ctx.strokeStyle = color;

  function respondCanvas() {
    var width = [];
    width[0] = $('#smile').width(); //max width
    width[1] = $('canvas').width(); //max width

    return width;

    //Call a function to redraw other content (texts, images etc)
  }

  function checkheight() {
    var height = $('#asb').height(); //max width
    //console.log(height);
    // return height;
  }

  var mousePosition = {
    x: 30 * canvas.width / 100,
    y: 30 * canvas.height / 100
  };

  if (canvas.width <= 1000) {
    var numdot = 100;
  } else if (canvas.width <= 800) {
    var numdot = 80;
  } else if (canvas.width <= 500) {
    var numdot = 35;
  } else {
    var numdot = 300;
  }

  var dots = {
    nb: numdot,
    distance: 70,
    d_radius: 50,
    array: []
  };

  function Dot() {
    this.x = Math.random() * canvas.width;
    this.y = Math.random() * canvas.height;

    this.vx = -.5 + Math.random();
    this.vy = -.5 + Math.random();

    this.radius = Math.random();
  }

  Dot.prototype = {
    create: function() {
      ctx.beginPath();
      ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
      ctx.fill();
    },

    animate: function() {
      for (i = 0; i < dots.nb; i++) {

        var dot = dots.array[i];

        if (dot.y < 0 || dot.y > canvas.height) {
          dot.vx = dot.vx;
          dot.vy = -dot.vy;
        } else if (dot.x < 0 || dot.x > canvas.width) {
          dot.vx = -dot.vx;
          dot.vy = dot.vy;
        }
        dot.x += dot.vx;
        dot.y += dot.vy;
      }
    },

    line: function() {
      for (i = 0; i < dots.nb; i++) {
        for (j = 0; j < dots.nb; j++) {
          i_dot = dots.array[i];
          j_dot = dots.array[j];

          if ((i_dot.x - j_dot.x) < dots.distance && (i_dot.y - j_dot.y) < dots.distance && (i_dot.x - j_dot.x) > -dots.distance && (i_dot.y - j_dot.y) > -dots.distance) {
            if ((i_dot.x - mousePosition.x) < dots.d_radius && (i_dot.y - mousePosition.y) < dots.d_radius && (i_dot.x - mousePosition.x) > -dots.d_radius && (i_dot.y - mousePosition.y) > -dots.d_radius) {
              ctx.beginPath();
              ctx.moveTo(i_dot.x, i_dot.y);
              ctx.lineTo(j_dot.x, j_dot.y);
              ctx.stroke();
              ctx.closePath();
            }
          }
        }
      }
    }
  };

  function createDots() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    for (i = 0; i < dots.nb; i++) {
      dots.array.push(new Dot());
      dot = dots.array[i];

      dot.create();
    }

    dot.line();
    dot.animate();
  }

  $("canvas").mousemove(function(parameter) {

    mousePosition.x = parameter.pageX - 0;
    mousePosition.y = parameter.pageY - 300;
  });

  setInterval(createDots, 1000 / 30);
};

但是,实际上不起作用,我看不到虚线动画(我检查了 chrome,ff)。如何解决这个问题?!

演示:https://jsfiddle.net/5pzvh8ko/

【问题讨论】:

  • 检查了您的控制台...? jsfiddle.net/5pzvh8ko/7
  • @cswl: dotted 有效,但在鼠标悬停画布中无效。

标签: javascript jquery css html html5-canvas


【解决方案1】:

你可以通过给你的canvas标签一个ID来更好地尝试它,然后使用“document.getElementById()”来访问你的canvas标签,我认为这是一个更好的方法,甚至更简单。这是一个例子: 带有 id 的画布标签:

<canvas ID="canvas_id" style="position: absolute; width: 100%; z-index:3; display: block;" height="359" width="2111">

然后使用 JavaScript 以这种方式访问​​它:

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

这可能不是您所需要的,但您至少可以尝试一下。

【讨论】:

    猜你喜欢
    • 2013-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    • 1970-01-01
    • 2012-04-13
    相关资源
    最近更新 更多