将鼠标移动添加到画布动画中

时间:2018-09-24 14:02:06

标签: javascript animation html5-canvas mouseevent

我在canvas html中制作了一个动画,但是现在我想根据鼠标的位置使球的起源原点在画布周围移动。我想添加鼠标事件功能,但是我似乎无法弄清楚逻辑并添加到代码中,任何帮助将不胜感激!

function runParticles () {
  var canvas = document.createElement("canvas");
  c = canvas.getContext("2d");
  var particles = {};
  var particleIndex = 0;
  var particleNum = 15;

  // set canvas size
  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;

  // add canvas to body
  document.body.appendChild(canvas);

  // style the canvas
  c.fillStyle = "black";
  c.fillRect(0, 0, canvas.width, canvas.height);

  function Particle() {
  this.x = canvas.width / 2;
  this.y = canvas.height / 2;
  this.vx = Math.random() * 10 - 5;
  this.vy = Math.random() * 10 - 5;
  this.gravity = 0.3;
  particleIndex++;
  particles[particleIndex] = this;
  this.id = particleIndex;
  this.life = 0;


  this.maxLife = Math.random() * 30 + 60;


  this.color = "hsla(" + parseInt(Math.random() * 360, 10) + ",90%,60%,0.5";
  }

  Particle.prototype.draw = function() {
  this.x += this.vx;
  this.y += this.vy;


  if (Math.random() < 0.1) {
      this.vx = Math.random() * 10 - 5;
      this.vy = Math.random() * 10 - 5;
  }

  this.life++;
  if (this.life >= this.maxLife) {
      delete particles[this.id];
  }

  c.fillStyle = this.color;
  //c.fillRect(this.x, this.y, 5, 10);
  c.beginPath();
  c.arc(this.x, this.y, 2.5, degToRad(0), degToRad(360));
  c.fill();
  };

  setInterval(function() {
  //normal setting before drawing over canvas w/ black background
  c.globalCompositeOperation = "source-over";
  c.fillStyle = "rgba(0,0,0,0.5)";
  c.fillRect(0, 0, canvas.width, canvas.height);
  for (var i = 0; i < particleNum; i++) {
      new Particle();
  }

  // c.globalCompositeOperation = "darken";

  for (var i in particles) {
      particles[i].draw();
  }
  }, 30);
}

function degToRad(deg) {
            var radians = (deg * Math.PI / 180) - Math.PI / 2;
            return radians;
        }
<!DOCTYPE html5>
<html>

<head>
  <title>disturbed</title>

  <script src="toto.js" type="text/javascript"></script>
  <script>
    window.onload = () => runParticles();
  </script>
</head>

<body>
</body>

</html>

1 个答案:

答案 0 :(得分:2)

  1. 我添加了一个检测鼠标位置的功能:
function oMousePos(canvas, evt) {
  var ClientRect = canvas.getBoundingClientRect();
    return { //objeto
    x: Math.round(evt.clientX - ClientRect.left),
    y: Math.round(evt.clientY - ClientRect.top)
  }
}
  1. 我已经声明了一个变量m(鼠标)。
var m = {x:canvas.width/2,y:canvas.height/2};

并且我将粒子的原点从this.x = canvas.width / 2; this.y = canvas.height / 2;更改为this.x = m.x; this.y = m.y;

这是鼠标不移到画布上的位置

  1. 我添加了一个事件“ mousemove”。当鼠标移到画布上时,位置会发生变化。
canvas.addEventListener("mousemove", (evt)=>{
  m = oMousePos(canvas, evt);
})

我还添加了一个事件“ mouseleave”。当鼠标离开画布时,鼠标会回到中间。

canvas.addEventListener("mouseleave", (evt)=>{
  m = {x:canvas.width/2,y:canvas.height/2};
})
  1. 我还更改了setInterval的requestAnimationFrame(效率更高)。里面的代码就是您的代码。

function runParticles () {
  var canvas = document.createElement("canvas");
  c = canvas.getContext("2d");
  var particles = {};
  var particleIndex = 0;
  var particleNum = 8;

  // set canvas size
  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;
  
  var m = {x:canvas.width/2,y:canvas.height/2};///////

  // add canvas to body
  document.body.appendChild(canvas);

  // style the canvas
  c.fillStyle = "black";
  c.fillRect(0, 0, canvas.width, canvas.height);

  function Particle() {
  this.x = m.x;//////////
  this.y = m.y;//////////
  this.vx = Math.random() * 10 - 5;
  this.vy = Math.random() * 10 - 5;
  this.gravity = 0.3;
  particleIndex++;
  particles[particleIndex] = this;
  this.id = particleIndex;
  this.life = 0;


  this.maxLife = Math.random() * 30 + 60;


  this.color = "hsla(" + parseInt(Math.random() * 360, 10) + ",90%,60%,0.5";
  }

  Particle.prototype.draw = function() {
  this.x += this.vx;
  this.y += this.vy;


  if (Math.random() < 0.1) {
      this.vx = Math.random() * 10 - 5;
      this.vy = Math.random() * 10 - 5;
  }

  this.life++;
  if (this.life >= this.maxLife) {
      delete particles[this.id];
  }

  c.fillStyle = this.color;
  //c.fillRect(this.x, this.y, 5, 10);
  c.beginPath();
  c.arc(this.x, this.y, 2.5, degToRad(0), degToRad(360));
  c.fill();
  };
  
function Draw() {
  window.requestAnimationFrame(Draw);
  c.globalCompositeOperation = "source-over";
  c.fillStyle = "rgba(0,0,0,0.5)";
  c.fillRect(0, 0, canvas.width, canvas.height);
  for (var i = 0; i < particleNum; i++) {
      new Particle();
  }

  // c.globalCompositeOperation = "darken";

  for (var i in particles) {
      particles[i].draw();
  } 
  }
  
  Draw();
  
  
///////////////////
canvas.addEventListener("mousemove", (evt)=>{
  m = oMousePos(canvas, evt);
})
canvas.addEventListener("mouseleave", (evt)=>{
  m = {x:canvas.width/2,y:canvas.height/2};
})
///////////////////
   
}

function degToRad(deg) {
            var radians = (deg * Math.PI / 180) - Math.PI / 2;
            return radians;
        }


runParticles();




function oMousePos(canvas, evt) {
  var ClientRect = canvas.getBoundingClientRect();
	return { //objeto
	x: Math.round(evt.clientX - ClientRect.left),
	y: Math.round(evt.clientY - ClientRect.top)
  }
}
body,canvas{margin:0;padding:0;}

我希望这会有所帮助。