问答详情
源自:2-8 大鱼随鼠标移动

鱼太大了,而且鱼总不能停在鼠标停的地方,只有鼠标在浏览器左上角鱼才能和鼠标重合,鼠标离左上角越远鱼就离鼠标越远

main.js里面和鼠标相关的:

  can1.addEventListener('mousemove', onMouseMove, false);

function onMouseMove(e)

{

  if(e.offsetX || e.layerX)

  {

    mx = e.offsetX == undefined ? e.layerX : e.offsetX;

    my = e.offsetY == undefined ? e.layerY : e.offsetY;

  }

}

mom.js里面:

momObj.prototype.draw = function()

{


this.x = lerpDistance(mx, this.x, 0.98);

this.y = lerpDistance(my, this.y, 0.98);


var deltaY = my - this.y;

var deltaX = mx - this.x;

var beta = Math.atan2(deltaY, deltaX);


this.angle = lerpAngle(beta, this.angle, 0.6);


ctx1.save();

ctx1.translate(this.x, this.y);

ctx1.rotate(this.angle);

ctx1.drawImage(this.bigEye, -this.bigEye.width * 0.5, -this.bigEye.height * 0.5);

ctx1.drawImage(this.bigBody, -this.bigBody.width * 0.5, -this.bigBody.height * 0.5);

ctx1.drawImage(this.bigTail,-this.bigTail.width * 0.5 + 30, -this.bigTail.height * 0.5);

ctx1.restore();

}


提问者:呼啦啦巴扎嘿 2016-07-30 22:12

个回答

  • 慕仙8447233
    2016-10-19 14:59:39

    我也是这样  你解决了没啊

  • hk凯
    2016-08-01 13:56:03

    很大是什么鬼?我看了一下代码和我的就是这里不一样

    var beta = Math.atan2(deltaY,deltaX) + Math.PI;