console
https://jsbin.com/royekuwule/3/edit?html,css,js,console,output<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.container {
position: relative;
margin-left: 100px;
margin-top: 100px;
width: 400px;
height: 300px;
overflow: scroll;
}
.image-wrap {
position: relative;
}
</style>
</head>
<body>
<div class="container">
<div class="image-wrap">
<image class="image" src="https://img.ixintu.com/download/jpg/202010/3cae9167e60640b41fbf9d104c548049_800_525.jpg!con"></image>
</div>
</div>
<button class="btn-rotate">旋转</button>
<script>
const point = {
x: 0,
y: 0
}
const imageWrap = document.querySelector('.image-wrap')
const image = document.querySelector('.image')
image.addEventListener('load', function() {
imageWrap.style.width = image.width + 'px'
imageWrap.style.height = image.height + 'px'
})
imageWrap.addEventListener('click', function(e) {
const rect = imageWrap.getBoundingClientRect()
point.x = e.clientX - rect.left
point.y = e.clientY - rect.top
console.log(point)
if (document.querySelector('.circle')) {
return
}
const circle = document.createElement('div')
circle.classList.add('circle')
circle.style.width = '20px'
circle.style.height = '20px'
circle.style.borderRadius = '50%'
circle.style.backgroundColor = 'red'
circle.style.position = 'absolute'
circle.style.left = point.x - 10 + 'px'
circle.style.top = point.y - 10 + 'px'
imageWrap.appendChild(circle)
const handleMove = (e) => {
const rect = imageWrap.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
const img = imageWrap.querySelector('.image');
const imgWidth = img.offsetWidth;
const imgHeight = img.offsetHeight;
let limitedX, limitedY;
switch (rotationDegree) {
case 90:
limitedX = Math.max(10, Math.min(x, imgHeight - 10));
limitedY = Math.max(10, Math.min(y, imgWidth - 10));
break;
case 270:
limitedX = Math.max(10, Math.min(x, imgHeight - 10));
limitedY = Math.max(10, Math.min(y, imgWidth - 10));
break;
default:
limitedX = Math.max(10, Math.min(x, imgWidth - 10));
limitedY = Math.max(10, Math.min(y, imgHeight - 10));
}
circle.style.left = (limitedX - 10) + 'px';
circle.style.top = (limitedY - 10) + 'px';
point.x = limitedX;
point.y = limitedY;
}
circle.addEventListener('mousedown', function(e) {
e.preventDefault()
const rect = imageWrap.getBoundingClientRect()
const startX = e.clientX - rect.left
const startY = e.clientY - rect.top
document.addEventListener('mousemove', handleMove)
document.addEventListener('mouseup', function() {
document.removeEventListener('mousemove', handleMove)
document.removeEventListener('mouseup', function() {})
})
})
})
const btnRotate = document.querySelector('.btn-rotate')
let rotationDegree = 0;
btnRotate.addEventListener('click', () => {
rotationDegree = (rotationDegree + 90) % 360;
const img = imageWrap.querySelector('.image');
const circle = document.querySelector('.circle');
if (circle) {
const imgWidth = img.offsetWidth;
const imgHeight = img.offsetHeight;
console.log(imgWidth, imgHeight)
const circleX = parseFloat(circle.style.left) + 10;
const circleY = parseFloat(circle.style.top) + 10;
let newX, newY;
switch (rotationDegree) {
case 90:
newX = imgHeight - circleY;
newY = circleX;
break;
case 180:
newX = imgWidth - circleY;
newY = circleX;
break;
case 270:
newX = imgHeight - circleY;
newY = circleX;
break;
case 0:
newX = imgWidth - circleY;
newY = circleX;
break;
}
circle.style.left = (newX - 10) + 'px';
circle.style.top = (newY - 10) + 'px';
console.log(newX, newY)
}
switch (rotationDegree) {
case 90:
img.style.transform = `rotate(90deg) translateY(-100%)`;
img.style.transformOrigin = 'left top';
break;
case 180:
img.style.transform = `rotate(180deg) translate(-100%, -100%)`;
img.style.transformOrigin = 'left top';
break;
case 270:
img.style.transform = `rotate(270deg) translateX(-100%)`;
img.style.transformOrigin = 'left top';
break;
case 0:
img.style.transform = 'rotate(0deg)';
img.style.transformOrigin = 'left top';
break;
}
})
</script>
</body>
</html>