How can I save a rotated image using JavaScript or jQuery after rotating it by clicking on a rotate button? The code provided below shows the functionality for rotating an image and saving the rotated version. Feel free to provide your solution.
Code for rotating the image:
var angle = 0,
img = document.getElementById('id');
document.getElementById('rotatedown').onclick = function() {
angle = (angle + 270) % 360;
img.className = "rotate" + angle;
imgRotate = document.getElementById('id').src;
$('.saverotatedown').css('display', 'block')
$('.saverotateright').css('display', 'none')
$('.saverotateup').css('display', 'none')
$('.saverotateleft').css('display', 'none')
let save = document.querySelector(".saverotatedown")
save.addEventListener('click', () => {
let a = document.createElement('a');
a.href = imgRotate;
a.download = 'imaged.png';
document.body.appendChild(a);
a.click();
});
console.log(img.className, imgRotate);
}
<title>Image upload</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="https://skillrary.com/uploads/images/fav-sr-64x64-logo.png" type="image/x-icon">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/0.8.1/cropper.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" crossorigin="anonymous" rel="preconnect" defer/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/0.8.1/cropper.min.js"></script>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
<img src="https://skillrary.com/uploads/images/f-sr-logo-195-50.png" id="id" />
<button id="rotatedown" class="rotateImgDiv"><i class="fa fa-undo" aria-hidden="true"></i>
Rotate</button>
<button class="btn saverotatedown" style="display: none">Save down</button>