html5图片绕中心旋转,HTML5从中心点旋转图像

好吧,这是怎么我设法让它起作用:

包含动画循环的main.js

window.onload = function(){

/*variables*/

var canvas = document.getElementById('canvas'),

context = canvas.getContext('2d'),

mouse = utils.captureMouse(canvas),

ship = new Ship();

ship.x = canvas.width/2;

ship.y = canvas.with/2;

/*animation loop*/

(function drawFrame(){

window.requestAnimationFrame(drawFrame, canvas);

//clear rect tyhjentää koko canvasin

context.clearRect(0, 0, canvas.width + 1, canvas.height + 1);

var dx = mouse.x - alus.x,

dy = mouse.y - alus.y;

ship.rotation = Math.atan2(dy, dx);

ship.draw(context);

})();

}

Ship.js

function Ship(){

this.x = 0;//x-sjainti

this.y = 0;//y-sijainti

this.rotation = 0;

this.img = new Image();

this.img.src = "resources/img/ship2.png";

this.imgW = this.img.width;

this.imgH = this.img.height;

}

Ship.prototype.draw = function(context){

context.save();

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

context.rotate(this.rotation);

context.drawImage(this.img, this.imgW/2 * -1, this.imgH/2 * -1);

context.restore();

}

其要点是IMGW和IMGH。如drawImage-method所示,图像的绘制位置设置为对象向后尺寸的一半。尝试后,这似乎已经集中了旋转点。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值