简体   繁体   English

使用数组在 JavaScript 中旋转图像

[英]Image rotation in JavaScript using array

I am writing an image rotation functionality.我正在编写图像旋转功能。 I have created an array to store degrees for transform function.我创建了一个数组来存储变换函数的度数。

However I need to know instead of increment array, to move next and previous between the rotations array.但是我需要知道而不是增量数组,在rotations数组之间移动下一个和上一个。

Switching from left and right doesn't work as expected.从左右切换无法按预期工作。

https://jsfiddle.net/andreas20/v94zLg8b/ https://jsfiddle.net/andreas20/v94zLg8b/

 rotations = ['90deg', '180deg', '270deg', '360deg'] let img = document.getElementById('img_blob'); let array_increment = 0 $('#left').click(() => { if (array_increment > 3) array_increment = 0 img.style.transform = 'rotate(-' + rotations[array_increment] + ')' array_increment++ console.log(array_increment) }) $('#right').click(() => { if (array_increment > 3) array_increment = 0 img.style.transform = 'rotate(' + rotations[array_increment] + ')' array_increment++ console.log(array_increment) })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="left">Left</button> <button id="right">Right</button><br> <img id="img_blob" src="https://i.imgur.com/vgD5ycf.jpg">

you could do rotation something like that without using array你可以在不使用数组的情况下进行类似的旋转

 let img = document.getElementById('img_blob'); let rotationValue = 0; $('#left').click(() => rotate(-1)); $('#right').click(() => rotate(1)); function rotate(direction) { rotationValue += 90 * direction; rotationValue = rotationValue % 360; img.style.transform = `rotate(${rotationValue}deg)`; // console.log(rotationValue); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <img id="img_blob" src="https://dummyimage.com/150x150/000/fff" /> <button id="left">Left</button> <button id="right">Right</button>

You had a misconception with the minus in the rotation你对旋转中的减号有误解

-90deg, -180deg, -270deg, -360deg is not the same as 90deg, 180deg, 270deg, 360deg backwards (that would be 0deg, -90deg, -180deg, -270deg , so with an offset of one) -90deg, -180deg, -270deg, -360deg90deg, 180deg, 270deg, 360deg向后不同(即0deg, -90deg, -180deg, -270deg ,所以偏移量为 1)

What I do is, I start at zero and move inside the array backwards and forwards我所做的是,我从零开始并在数组内前后移动

 rotations = ['0deg', '45deg', '90deg', '135deg', '180deg', '225deg', '270deg', '315deg'] let img = document.getElementById('img_blob'); let array_increment = 0 array_increment = 0; $('#left').click(() => { array_increment-- array_increment = (array_increment + rotations.length) % rotations.length console.log(array_increment) img.style.transform = 'rotate(' + rotations[array_increment] + ')' }) $('#right').click(() => { array_increment++ array_increment = (array_increment + rotations.length) % rotations.length console.log(array_increment) img.style.transform = 'rotate(' + rotations[array_increment] + ')' })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="left">Left</button> <button id="right">Right</button><br> <img id="img_blob" src="https://i.picsum.photos/id/190/300/300.jpg">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM