魚(yú)移到左上角,不會(huì)跟著鼠標(biāo)動(dòng),求解答,代碼如下:
momObj.prototype.init = function()
{
this.x = canWidth * 0.5;
this.y = canHeight * 0.5;
this.angle = 0;
this.bigEye.src = "./src/bigEye0.png" ;
this.bigBody.src = "./src/bigSwim0.png" ;
this.bigTail.src = ?"./src/bigTail0.png" ;
}
momObj.prototype.draw = function()
{
//lerp x,y
this.x = lerpDistance(mx, this.x, 0.98); ? ?//讓大魚(yú)的坐標(biāo)一直趨向于鼠標(biāo)的坐標(biāo)
this.y = lerpDistance(my, this.y, 0.98);
//delta angle(角度差)
//Math.atan2(x,y)
var deltaY = my - this.y;
var deltaX = mx - this.x;
var beta = Math.atan2(deltaY ,deltaX);//返回值是[-PI,PI]
this.angle = lerpAngle(beta, this.angle, 0.6);?
ctx1.save();
ctx1.translate(this.x,this.y); ?//大魚(yú)的相對(duì)原點(diǎn)
ctx1.rotate(this.angle); ? ?//旋轉(zhuǎn)畫(huà)布
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();
}
2018-08-08
什么問(wèn)題?
2016-09-21
main.js
//初始化部分
mom = new momObj();
mom.init();
mx = canWidth * 0.5;
my = canHeight * 0.5;
//循環(huán)部分
ctx1.clearRect(0,0,canWidth,canHeight); //ctx1每一幀的都要清除掉,不然會(huì)被覆蓋,使畫(huà)的圖形變粗
mom.draw();
function onMouseMove(e) ??
{
if (e.offSetX || e.layerX) ??
{
mx = e.offSetX == undefined ? e.offSetX : e.layerX;
my = e.offSetY == undefined ? e.offSetY : e.layerY;
//console.log(mx);
}
}