gpt4 book ai didi

javascript - 通过范围 slider 旋转图像

转载 作者:行者123 更新时间:2023-11-28 17:02:22 24 4
gpt4 key购买 nike

大家好,如何通过范围 slider 使图像旋转?

我构建了该函数,使其范围可以在 0 到 360 之间并显示值范围,它工作正常,但如何应用它来旋转图像? 裁剪图像脚本文档是 here

我用裁剪脚本更新了代码,请帮助旋转图像并输出到 div

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Cropper.js</title>
<!-- <link rel="stylesheet" href="dist/cropper.css"> -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/0.8.1/cropper.css" />

<style>
.container {
max-width: 640px;
margin: 20px auto;
}

img {
max-width: 100%;
}
</style>
</head>
<body>

<div class="container">
<h1>Cropper with a range of aspect ratio</h1>

<div>
<img id="image" src="https://fengyuanchen.github.io/cropperjs/images/picture.jpg" alt="Picture">
</div>
<button onclick="cropper.getCroppedCanvas()">Save</button>
</div>

<!-- <script src="dist/cropper.js"></script> -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/0.8.1/cropper.js"></script>
<script>
window.addEventListener('DOMContentLoaded', function () {
var image = document.querySelector('#image');
var minAspectRatio = 1.0;
var maxAspectRatio = 1.0;
var cropper = new Cropper(image, {
ready: function () {
var cropper = this.cropper;
var containerData = cropper.getContainerData();
var cropBoxData = cropper.getCropBoxData();
var aspectRatio = cropBoxData.width / cropBoxData.height;
var newCropBoxWidth;

if (aspectRatio < minAspectRatio || aspectRatio > maxAspectRatio) {
newCropBoxWidth = cropBoxData.height * ((minAspectRatio + maxAspectRatio) / 2);

cropper.setCropBoxData({
left: (containerData.width - newCropBoxWidth) / 2,
width: newCropBoxWidth
});
}
},
cropmove: function () {
var cropper = this.cropper;
var cropBoxData = cropper.getCropBoxData();
var aspectRatio = cropBoxData.width / cropBoxData.height;

if (aspectRatio < minAspectRatio) {
cropper.setCropBoxData({
width: cropBoxData.height * minAspectRatio
});
} else if (aspectRatio > maxAspectRatio) {
cropper.setCropBoxData({
width: cropBoxData.height * maxAspectRatio
});
}
}
});
});
</script>

<script>
function updateTextInput(val) {
document.getElementById('textInput').value=val;
}

</script>

<input type="range" name="rangeInput" min="0" max="360" onchange="updateTextInput(this.value);">
<input type="text" id="textInput" value="">
<!-- FULL DOCUMENTATION ON https://github.com/fengyuanchen/cropperjs -->


</body>
</html>

最佳答案

您可以使用一些 JavaScript 来设置 slider 值更改时图像的旋转。既然你有 jQuery:

$(function(){
let slider = $('input[type=range]'),
image = $('#image');

slider.on('change mousemove', function(){
image.css('transform', 'rotate(' + $(this).val() + 'deg)');
});

});

旁注:这种类型的事件分配 - 在 javascript 中查找元素,而不是向输入添加 onchange 属性 - 更加灵活和可维护。 p>

这是一个示例:https://codepen.io/benjamin-hull/pen/ewxboE

一些值得注意的事情:

我添加了“mousemove”事件监听器以及“change”事件监听器,以便用户在移动 slider 时获得实时反馈。这可能是一个问题,因为 mousemove 可以产生数百个事件。您可能需要查看该事件的“去抖动”以将其限制为合理的值。

在我的示例中,我将 slider 设置为最小 -180、最大 180 和默认值 0。这允许用户左右旋转。

您可能还希望旋转来缩放图像,因此它始终填充框架。

关于javascript - 通过范围 slider 旋转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56980317/

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