gpt4 book ai didi

javascript - 在 HTML5 Canvas 上旋转图像

转载 作者:行者123 更新时间:2023-11-28 12:21:58 25 4
gpt4 key购买 nike

我有一张图片需要旋转一个 Angular 然后将它绘制到 Canvas 上的某个点。我目前有这个:

var image = roach.image;
image.style.msTransform = 'rotate(' + roach.heading + 'deg)';
this.gameContext.drawImage(image, roach.position.x, roach.position.y);

我如何编辑它以使其工作,其中 roach.heading 是我想要将其旋转到度数的 Angular 。

最佳答案

试试这个:

var image = roach.image,
ctx = this.gameContext,
widthHalf = Math.floor( image.width / 2 ),
heightHalf = Math.floor( image.height / 2 );

ctx.save();

ctx.translate( roach.position.x, roach.position.y );
ctx.translate( widthHalf, heightHalf );
ctx.rotate( ( Math.PI / 180 ) * roach.heading );
ctx.drawImage( image, -widthHalf, -heightHalf );
ctx.restore();

jsfiddle:http://jsfiddle.net/PwzEc/

关于javascript - 在 HTML5 Canvas 上旋转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13196223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com