[英]Function to rotate an image various degrees
I am working on coding a research experiment where one of several preloaded images can be displayed rotated at a multiple of 45 degrees. 我正在编写一个研究实验的代码,其中可以以45度的倍数旋转显示几个预加载图像中的一个。 Is there a function for doing this so I could have say
是否有执行此操作的功能,所以我可以说
var img45 = function(img, deg) { ...
I need to be able to pass the pre-rotated images as variables to a different function. 我需要能够将预旋转的图像作为变量传递给其他函数。
I can't use CSS' transform since the same image needs to be available at several different angles and I can't just transform the canvas display because I need the pre-rotated images as variables. 我不能使用CSS变换,因为同一图像需要在几个不同的角度可用,并且我不能仅变换画布显示,因为我需要预先旋转的图像作为变量。
Rotate() works but does not let me assign the picture to a new variable, it instead modifies the original image, which is not what I want (I need the original to be unmodified). Rotate()有效,但是不允许我将图片分配给新变量,而是修改了原始图像,这不是我想要的(我需要不修改原始图像)。
var img45 = function(img_id, angle) {
$('#'+img_id).rotate(angle);
}
Is this what you need? 这是您需要的吗? It's jQuery, so you need to include jQuery's library.
它是jQuery,因此您需要包括jQuery的库。
Something like this? 像这样吗
// Function to rotate element. var rotate = function(elem, deg) { elem.style.transform = 'rotate(' + deg + ')'; } // Test rotating boxes var boxes = document.querySelectorAll('.box'); for (var i = 1; i <= boxes.length; ++i) { rotate(boxes[i], (5 * i) + 'deg'); }
.box { width: 128px; height: 128px; border: 1px solid #ddd; margin: 10px; }
<div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.