gpt4 book ai didi

JavaScript 旋转

转载 作者:行者123 更新时间:2023-11-30 18:19:18 26 4
gpt4 key购买 nike

我需要先旋转图像,然后再将其加载到 Canvas 中。据我所知,我无法使用 canvas.rotate() 旋转它,因为它会旋转整个场景。

有没有好的JS方法来旋转图片? [不依赖于浏览器的方式]

最佳答案

不完全是,你可以保存场景,旋转图像然后恢复场景:

var canvas = document.getElementById('canvas');
canvas.width = 600;
canvas.height = 400;
var ctx = canvas.getContext('2d');
var image = new Image();
image.src = 'https://www.google.ro/images/srpr/logo3w.png';
drawRotatedImage(image, 275, 95, 25);

function drawRotatedImage(image, x, y, angle) {
ctx.save();
ctx.translate(x, y);
ctx.rotate(angle * (Math.PI/180));
ctx.drawImage(image, -(image.width/2), -(image.height/2));
ctx.restore();
}

JSFiddle Example

关于JavaScript 旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12472639/

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