This commit is contained in:
2025-09-10 07:28:38 +08:00
parent f187588540
commit d50e5329a0
17 changed files with 443 additions and 726 deletions

View File

@@ -0,0 +1,21 @@
body { margin: 0; padding: 0; height: 100vh; display: flex; overflow: hidden; }
.background-container { position: relative; flex: 1; height: 100vh; background-image: url('../images/background.png'); background-position: center; background-repeat: no-repeat; background-size: contain; z-index: 1; }
.control-panel { width: 200px; background-color: #fff; border-left: 1px solid #ccc; padding: 20px; height: 100vh; box-sizing: border-box; z-index: 2; overflow-y: auto; }
.control-section { margin-bottom: 30px; }
.control-section h3 { margin-top: 0; padding-bottom: 10px; border-bottom: 1px solid #eee; }
.image-selector { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-top: 15px; }
.image-btn { width: 70px; height: 70px; border: 2px solid #ddd; border-radius: 4px; cursor: pointer; background-size: contain; background-repeat: no-repeat; background-position: center; transition: all 0.2s; }
.image-btn:hover { border-color: #4CAF50; transform: scale(1.05); }
.image-btn:active { transform: scale(0.98); }
.arrow-container { position: absolute; pointer-events: auto; }
.arrow-image { width: 155px; height: 173px; background-size: contain; background-repeat: no-repeat; cursor: move; }
.save-btn { width: 100%; padding: 10px; background-color: #4CAF50; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; margin-top: 20px; }
.save-btn:hover { background-color: #45a049; }
.arrow-text { position: absolute; top: '100%'; left: '50%'; transform: translateX(-50%); margin-top: '-5px'; font-weight: bold; color: red; font-size: 16px; white-space: nowrap; text-align: center; width: 100%; background-color: transparent; padding: 3px 8px; border-radius: 4px; }
#contextMenu { position: absolute; display: none; background: white; border: 1px solid #ccc; padding: 5px; z-index: 9999; }
#deleteArrow { padding: 3px 10px; cursor: pointer; }
.menu-separator { height: 1px; background-color: #ccc; margin: 5px 0; }
.menu-item { padding: 3px 10px; cursor: pointer; }
.menu-item:hover { background-color: #f0f0f0; }
.check-mark { display: inline-block; width: 16px; text-align: center; }
.center-dot { position: absolute; width: 8px; height: 8px; background: red; border-radius: 50%; transform: translate(-50%, -50%); pointer-events: none; z-index: 5; }

View File

@@ -1,140 +0,0 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>双箭头填充动画</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
font-family: Arial, sans-serif;
}
.container {
text-align: center;
}
button {
margin-top: 20px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
}
button:hover {
background-color: #45a049;
}
button:disabled {
background-color: #cccccc;
cursor: not-allowed;
}
</style>
</head>
<body>
<div class="container">
<h1>双箭头填充动画演示</h1>
<!-- 扩展SVG尺寸以容纳两个相对的箭头 -->
<svg xmlns="http://www.w3.org/2000/svg" width="500px" height="61px" viewBox="-0.5 -0.5 500 61">
<!-- 定义裁剪路径,用于限制填充区域为箭头形状 -->
<defs>
<!-- 从左到右箭头的裁剪路径 -->
<clipPath id="arrowClipPathLeft">
<path d="M 0 8.1 L 174 18 L 164 0 L 240 30 L 164 60 L 174 42 L 0 51.9 L 0 30 Z" />
</clipPath>
<!-- 从右到左箭头的裁剪路径 -->
<clipPath id="arrowClipPathRight">
<path d="M 240 8.1 L 66 18 L 76 0 L 0 30 L 76 60 L 66 42 L 240 51.9 L 240 30 Z" />
</clipPath>
</defs>
<!-- 左侧箭头:从左到右 -->
<!-- 背景箭头轮廓 - 保持不变 -->
<path id="outlineArrowLeft"
d="M 0 8.1 L 174 18 L 164 0 L 240 30 L 164 60 L 174 42 L 0 51.9 L 0 30 Z"
fill="white"
stroke="black"
stroke-width="2"/>
<!-- 填充层 - 使用裁剪路径限制填充范围 -->
<rect id="fillAnimationLeft"
x="0" y="0" width="0" height="61"
fill="red"
clip-path="url(#arrowClipPathLeft)"/>
<!-- 右侧箭头:从右到左 (通过平移和水平翻转实现) -->
<!-- 背景箭头轮廓 - 保持不变 -->
<g transform="translate(500, 0) scale(-1, 1)">
<path id="outlineArrowRight"
d="M 0 8.1 L 174 18 L 164 0 L 240 30 L 164 60 L 174 42 L 0 51.9 L 0 30 Z"
fill="white"
stroke="black"
stroke-width="2"/>
<!-- 填充层 - 使用裁剪路径限制填充范围 -->
<rect id="fillAnimationRight"
x="0" y="0" width="0" height="61"
fill="red"
clip-path="url(#arrowClipPathLeft)"/>
</g>
</svg>
<button id="startButton">开始填充</button>
</div>
<script>
// 获取DOM元素
const fillAnimationLeft = document.getElementById('fillAnimationLeft');
const fillAnimationRight = document.getElementById('fillAnimationRight');
const startButton = document.getElementById('startButton');
let animationId = null;
let isAnimating = false;
// 开始填充动画
function startFillAnimation() {
if (isAnimating) return;
isAnimating = true;
startButton.disabled = true;
startButton.textContent = "填充中...";
const duration = 2000; // 动画持续时间(毫秒)
const startTime = performance.now();
const startWidth = 0;
const endWidth = 241; // 箭头的总宽度
function animate(currentTime) {
const elapsedTime = currentTime - startTime;
const progress = Math.min(elapsedTime / duration, 1);
// 计算当前的填充宽度从0逐渐变为241
const currentWidth = startWidth + (endWidth - startWidth) * progress;
// 同时更新两个箭头的填充宽度
fillAnimationLeft.setAttribute('width', currentWidth);
fillAnimationRight.setAttribute('width', currentWidth);
if (progress < 1) {
animationId = requestAnimationFrame(animate);
} else {
// 动画完成
isAnimating = false;
startButton.disabled = false;
startButton.textContent = "重新填充";
}
}
animationId = requestAnimationFrame(animate);
}
// 绑定按钮点击事件
startButton.addEventListener('click', startFillAnimation);
</script>
</body>
</html>

View File

@@ -1,92 +0,0 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>军事攻击箭头(点击开始)</title>
<style>
/* 基础布局 */
body{margin:0;padding:20px;background:#f5f1e6;font-family:"SimSun",serif}
.map-container{
width:896px; /* 与图片同宽 */
height:1152px; /* 与图片同高 */
margin:0 auto;
position:relative;
/* 正式使用时把下面url换成你的图片路径现在先用背景色占位 */
background:#e8dfca url('D:/dsWork/dsProject/dsLightRag/XingJun/c9fc2122-9324-4e2b-9bae-ced575575eca-1.png') center/contain no-repeat;
border:2px solid #8b7355;
overflow:auto;
}
/* 控制栏 */
.animation-controls{position:absolute;top:10px;left:10px;z-index:100}
.animation-controls button{margin-right:5px;padding:5px 10px}
/* SVG箭头样式 */
svg{position:absolute;top:0;left:0;width:100%;height:100%}
.attack-arrow{
fill:none;
stroke:#b70000;
stroke-width:14;
stroke-linecap:round;
stroke-linejoin:round;
marker-end:url(#arrowhead)
}
/* 地名/部队标注 */
.label{position:absolute;font-size:14px;font-weight:bold;color:#000;
background:rgba(255,255,255,.8);padding:2px 5px;border-radius:3px}
</style>
</head>
<body>
<div class="map-container">
<!-- 控制按钮 -->
<div class="animation-controls">
<button onclick="startAnimation()">开始</button>
<button onclick="pauseAnimation()">暂停</button>
<button onclick="resetAnimation()">重置</button>
</div>
<!-- 地图层SVG -->
<svg>
<!-- 行军路径(透明) -->
<path id="animationPath" d="M100,300 Q300,200 500,300 T900,300" fill="none"/>
<!-- 真正的箭头 -->
<g id="arrowGroup">
<path id="movingArrow"
d="m216.91503,274.91109c0.26021,-11.6278 -0.1665,-23.27113 2.13988,-34.87848c56.67197,3.81831 112.67554,10.06601 168.39255,15.68451c2.99113,-5.70158 1.58328,-10.4311 4.18213,-15.70261c35.79493,11.0927 71.2671,22.34337 105.28547,34.12978c-32.63733,13.36985 -70.07352,24.98065 -106.82583,36.87022c-1.19012,-5.5368 -2.38046,-11.07356 -3.5706,-16.61037c-54.1675,5.3566 -111.45142,10.8948 -165.96914,15.79176c-3.70747,-11.29953 -3.5985,-23.60505 -3.63446,-35.28482z"
class="attack-arrow">
<!-- 默认不执行begin="indefinite" -->
<animateMotion id="arrowAnimation" dur="10s" repeatCount="indefinite" begin="indefinite">
<mpath href="#animationPath"/>
</animateMotion>
</path>
</g>
</svg>
<!-- 一些标注 -->
<div class="label" style="top:480px;left:80px">9纵</div>
<div class="label" style="top:380px;left:280px">73军</div>
<div class="label" style="top:230px;left:680px">莱芜</div>
</div>
<script>
/* 控制函数 */
function startAnimation(){
const anim = document.getElementById('arrowAnimation');
anim.beginElement(); // 开始/继续
}
function pauseAnimation(){
const anim = document.getElementById('arrowAnimation');
anim.endElement(); // 立即暂停SVG 1.1简单办法)
}
function resetAnimation(){
const anim = document.getElementById('arrowAnimation');
anim.endElement(); // 先停
/* 把箭头瞬移到起点100,300 */
document.getElementById('arrowGroup').setAttribute('transform','translate(100,300)');
}
</script>
</body>
</html>

View File

Before

Width:  |  Height:  |  Size: 6.9 KiB

After

Width:  |  Height:  |  Size: 6.9 KiB

View File

Before

Width:  |  Height:  |  Size: 4.6 KiB

After

Width:  |  Height:  |  Size: 4.6 KiB

View File

Before

Width:  |  Height:  |  Size: 6.9 KiB

After

Width:  |  Height:  |  Size: 6.9 KiB

View File

Before

Width:  |  Height:  |  Size: 4.6 KiB

After

Width:  |  Height:  |  Size: 4.6 KiB

View File

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View File

Before

Width:  |  Height:  |  Size: 6.8 KiB

After

Width:  |  Height:  |  Size: 6.8 KiB

View File

Before

Width:  |  Height:  |  Size: 198 KiB

After

Width:  |  Height:  |  Size: 198 KiB

View File

Before

Width:  |  Height:  |  Size: 886 KiB

After

Width:  |  Height:  |  Size: 886 KiB

View File

Before

Width:  |  Height:  |  Size: 5.8 KiB

After

Width:  |  Height:  |  Size: 5.8 KiB

View File

Before

Width:  |  Height:  |  Size: 29 KiB

After

Width:  |  Height:  |  Size: 29 KiB

View File

@@ -0,0 +1,407 @@
// 添加常量定义
const ARROW_WIDTH = 155;
const ARROW_HEIGHT = 173;
const STOP_DISTANCE = 50;
document.addEventListener('DOMContentLoaded', function() {
const backgroundContainer = document.querySelector('.background-container');
const imageButtons = document.querySelectorAll('.image-btn');
const markCenterBtn = document.getElementById('markCenterBtn');
const startAnimationBtn = document.getElementById('startAnimationBtn');
const resetBtn = document.getElementById('resetBtn');
const contextMenu = document.getElementById('contextMenu');
const deleteArrow = document.getElementById('deleteArrow');
let selectedArrow = null;
let centerDot = null;
// 添加重置按钮事件监听器
resetBtn.addEventListener('click', function() {
location.reload();
});
// 添加开始按钮事件监听器
startAnimationBtn.addEventListener('click', startAnimation);
// 动画控制函数
function startAnimation() {
if (!centerDot) {
alert('请先确定中心点');
return;
}
const centerRect = centerDot.getBoundingClientRect();
const containerRect = backgroundContainer.getBoundingClientRect();
const centerX = centerRect.left - containerRect.left;
const centerY = centerRect.top - containerRect.top;
const arrows = document.querySelectorAll('.arrow-container');
if (arrows.length === 0) {
alert('没有可移动的箭头');
return;
}
arrows.forEach(arrow => {
animateArrow(arrow, centerX, centerY);
});
}
// 添加偏移量输入框事件处理
const globalStopDistanceInput = document.getElementById('global-stop-distance');
function updateStopDistanceInput() {
if (selectedArrow) {
globalStopDistanceInput.value = selectedArrow.dataset.stopDistance || 20;
globalStopDistanceInput.disabled = false;
} else {
globalStopDistanceInput.value = 20;
globalStopDistanceInput.disabled = true;
}
}
globalStopDistanceInput.addEventListener('input', function() {
if (selectedArrow) {
selectedArrow.dataset.stopDistance = this.value;
console.log(`[设置] 箭头停止距离: ${this.value}px`);
saveAllElements();
}
});
function selectArrow(arrow) {
document.querySelectorAll('.arrow-container').forEach(el => {
el.style.outline = 'none';
});
selectedArrow = arrow;
if (selectedArrow) {
selectedArrow.style.outline = '2px solid blue';
}
updateStopDistanceInput();
}
// 点击空白处取消选择
document.addEventListener('click', function() {
selectedArrow = null;
document.querySelectorAll('.arrow-container').forEach(el => {
el.style.outline = 'none';
});
updateStopDistanceInput();
contextMenu.style.display = 'none';
});
// 单个箭头动画函数
function animateArrow(arrow, targetX, targetY) {
const arrowWidth = ARROW_WIDTH;
const arrowHeight = ARROW_HEIGHT;
const centerOffsetX = arrowWidth / 2;
const centerOffsetY = arrowHeight / 2;
const stopDistance = parseInt(arrow.dataset.stopDistance) || STOP_DISTANCE;
const rect = arrow.getBoundingClientRect();
const containerRect = backgroundContainer.getBoundingClientRect();
let currentX = rect.left - containerRect.left + centerOffsetX;
let currentY = rect.top - containerRect.top + centerOffsetY;
const offsetRange = 20;
const randomOffsetX = (Math.random() - 0.5) * offsetRange;
const randomOffsetY = (Math.random() - 0.5) * offsetRange;
const finalTargetX = targetX + randomOffsetX;
const finalTargetY = targetY + randomOffsetY;
const dxTotal = finalTargetX - currentX;
const dyTotal = finalTargetY - currentY;
const distanceTotal = Math.sqrt(dxTotal * dxTotal + dyTotal * dyTotal);
let adjustedTargetX, adjustedTargetY;
if (distanceTotal > stopDistance) {
const ratio = (distanceTotal - stopDistance) / distanceTotal;
adjustedTargetX = currentX + dxTotal * ratio;
adjustedTargetY = currentY + dyTotal * ratio;
} else {
adjustedTargetX = currentX;
adjustedTargetY = currentY;
}
function updatePosition() {
const dx = adjustedTargetX - currentX;
const dy = adjustedTargetY - currentY;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 2) {
arrow.style.left = (adjustedTargetX - centerOffsetX) + 'px';
arrow.style.top = (adjustedTargetY - centerOffsetY) + 'px';
return;
}
const step = Math.max(distance * 0.05, 1);
const moveX = (dx / distance) * step;
const moveY = (dy / distance) * step;
currentX += moveX;
currentY += moveY;
arrow.style.left = (currentX - centerOffsetX) + 'px';
arrow.style.top = (currentY - centerOffsetY) + 'px';
requestAnimationFrame(updatePosition);
}
updatePosition();
}
// 加载保存的元素
loadAllElements();
// 绑定箭头按钮事件
imageButtons.forEach(button => {
button.addEventListener('click', () => {
const imagePath = button.getAttribute('data-image');
addImageToContainer(imagePath);
});
});
// 右键菜单事件
deleteArrow.addEventListener('click', function() {
if (selectedArrow) {
selectedArrow.remove();
saveAllElements();
contextMenu.style.display = 'none';
selectedArrow = null;
}
});
// 为匹配角菜单项添加事件监听器
document.querySelectorAll('.menu-item').forEach(item => {
item.addEventListener('click', function() {
if (selectedArrow) {
const referencePoint = this.getAttribute('data-point');
selectedArrow.dataset.referencePoint = referencePoint;
saveAllElements();
contextMenu.style.display = 'none';
selectedArrow = null;
}
});
});
// 点击空白处关闭菜单
document.addEventListener('click', function() {
contextMenu.style.display = 'none';
selectedArrow = null;
});
// 标记中心点功能
markCenterBtn.addEventListener('click', function() {
if (centerDot) {
centerDot.remove();
centerDot = null;
}
function handleMapClick(e) {
const rect = backgroundContainer.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
centerDot = document.createElement('div');
centerDot.className = 'center-dot';
centerDot.style.left = x + 'px';
centerDot.style.top = y + 'px';
backgroundContainer.appendChild(centerDot);
backgroundContainer.removeEventListener('click', handleMapClick);
console.log('[中心点] 已标记中心点:', { left: centerDot.style.left, top: centerDot.style.top });
saveAllElements();
}
backgroundContainer.addEventListener('click', handleMapClick);
});
// 添加箭头函数
function addImageToContainer(imagePath, position = null, textContent = '') {
const container = document.createElement('div');
container.className = 'arrow-container';
container.dataset.imagePath = imagePath;
const newImage = document.createElement('div');
newImage.className = 'arrow-image';
newImage.style.backgroundImage = `url('${imagePath}')`;
container.appendChild(newImage);
// 添加文字元素
if (textContent) {
const textElement = document.createElement('div');
textElement.className = 'arrow-text';
textElement.textContent = textContent;
container.dataset.text = textContent;
container.appendChild(textElement);
}
// 设置位置
const arrowWidth = ARROW_WIDTH;
const arrowHeight = ARROW_HEIGHT;
const centerOffsetX = arrowWidth / 2;
const centerOffsetY = arrowHeight / 2;
if (position) {
container.style.left = parseFloat(position.left) + 'px';
container.style.top = parseFloat(position.top) + 'px';
if (position.referencePoint) {
container.dataset.referencePoint = position.referencePoint;
}
if (position.stopDistance) {
container.dataset.stopDistance = position.stopDistance;
}
} else {
const x = Math.random() * (backgroundContainer.offsetWidth - arrowWidth) + centerOffsetX;
const y = Math.random() * (backgroundContainer.offsetHeight - arrowHeight) + centerOffsetY;
container.style.left = (x - centerOffsetX) + 'px';
container.style.top = (y - centerOffsetY) + 'px';
}
// 双击编辑文字
container.addEventListener('dblclick', (e) => {
e.stopPropagation();
const currentText = container.dataset.text || '';
const textContent = prompt('请输入文字:', currentText);
if (textContent !== null) {
if (textContent.trim() === '') {
container.querySelector('.arrow-text')?.remove();
delete container.dataset.text;
} else {
let textElement = container.querySelector('.arrow-text');
if (!textElement) {
textElement = document.createElement('div');
textElement.className = 'arrow-text';
container.appendChild(textElement);
}
textElement.textContent = textContent;
container.dataset.text = textContent;
saveAllElements();
}
}
});
// 右键菜单支持
container.addEventListener('contextmenu', function(e) {
e.preventDefault();
selectedArrow = this;
contextMenu.style.left = `${e.clientX}px`;
contextMenu.style.top = `${e.clientY}px`;
contextMenu.style.display = 'block';
const currentPoint = selectedArrow.dataset.referencePoint || 'top-left';
document.querySelectorAll('.menu-item').forEach(item => {
const checkMark = item.querySelector('.check-mark');
if (!checkMark) return;
checkMark.textContent = item.getAttribute('data-point') === currentPoint ? '✓' : '';
});
});
// 拖拽功能
let isDragging = false;
let offsetX, offsetY;
newImage.addEventListener('mousedown', startDrag);
function startDrag(e) {
e.preventDefault();
isDragging = true;
selectedArrow = container;
container.style.zIndex = '10';
const rect = container.getBoundingClientRect();
offsetX = e.clientX - rect.left;
offsetY = e.clientY - rect.top;
document.addEventListener('mousemove', drag);
document.addEventListener('mouseup', stopDrag);
}
function drag(e) {
if (!isDragging) return;
const containerRect = backgroundContainer.getBoundingClientRect();
const x = e.clientX - containerRect.left - offsetX;
const y = e.clientY - containerRect.top - offsetY;
container.style.left = x + 'px';
container.style.top = y + 'px';
}
function stopDrag() {
if (isDragging) {
isDragging = false;
container.style.zIndex = '1';
saveAllElements();
document.removeEventListener('mousemove', drag);
document.removeEventListener('mouseup', stopDrag);
}
}
backgroundContainer.appendChild(container);
saveAllElements();
return container;
}
// 保存所有元素状态
function saveAllElements() {
const elements = [];
// 保存中心点
if (centerDot) {
localStorage.setItem('centerPoint', JSON.stringify({
left: centerDot.style.left,
top: centerDot.style.top
}));
} else {
localStorage.removeItem('centerPoint');
}
// 保存箭头
document.querySelectorAll('.arrow-container').forEach(arrow => {
elements.push({
type: 'arrow',
imagePath: arrow.dataset.imagePath,
left: arrow.style.left,
top: arrow.style.top,
text: arrow.dataset.text || '',
referencePoint: arrow.dataset.referencePoint || 'top-left',
stopDistance: arrow.dataset.stopDistance || 20
});
});
localStorage.setItem('savedElements', JSON.stringify(elements));
console.log('[保存] 已保存元素:', elements);
}
// 加载所有元素状态
function loadAllElements() {
// 加载中心点
const savedCenter = localStorage.getItem('centerPoint');
if (savedCenter) {
const center = JSON.parse(savedCenter);
centerDot = document.createElement('div');
centerDot.className = 'center-dot';
centerDot.style.left = center.left;
centerDot.style.top = center.top;
backgroundContainer.appendChild(centerDot);
}
// 加载箭头
const savedElements = localStorage.getItem('savedElements');
if (savedElements) {
const elements = JSON.parse(savedElements);
elements.forEach(element => {
if (element.type === 'arrow') {
// Fix image path by adding 'images/' prefix if missing
if (!element.imagePath.startsWith('images/')) {
element.imagePath = `images/${element.imagePath}`;
}
addImageToContainer(element.imagePath, element, element.text);
}
});
console.log('[加载] 已加载元素:', elements);
}
}
// 阻止右键菜单冒泡
contextMenu.addEventListener('click', function(e) {
e.stopPropagation();
});
});

View File

@@ -4,44 +4,28 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>箭头动画</title>
<style>
body { margin: 0; padding: 0; height: 100vh; display: flex; overflow: hidden; }
.background-container { position: relative; flex: 1; height: 100vh; background-image: url('background.png'); background-position: center; background-repeat: no-repeat; background-size: contain; z-index: 1; }
.control-panel { width: 200px; background-color: #fff; border-left: 1px solid #ccc; padding: 20px; height: 100vh; box-sizing: border-box; z-index: 2; overflow-y: auto; }
.control-section { margin-bottom: 30px; }
.control-section h3 { margin-top: 0; padding-bottom: 10px; border-bottom: 1px solid #eee; }
.image-selector { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-top: 15px; }
.image-btn { width: 70px; height: 70px; border: 2px solid #ddd; border-radius: 4px; cursor: pointer; background-size: contain; background-repeat: no-repeat; background-position: center; transition: all 0.2s; }
.image-btn:hover { border-color: #4CAF50; transform: scale(1.05); }
.image-btn:active { transform: scale(0.98); }
.arrow-container { position: absolute; pointer-events: auto; }
.arrow-image { width: 155px; height: 173px; background-size: contain; background-repeat: no-repeat; cursor: move; }
.save-btn { width: 100%; padding: 10px; background-color: #4CAF50; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; margin-top: 20px; }
.save-btn:hover { background-color: #45a049; }
.arrow-text { position: absolute; top: '100%'; left: '50%'; transform: translateX(-50%); margin-top: '-5px'; font-weight: bold; color: red; font-size: 16px; white-space: nowrap; text-align: center; width: 100%; background-color: transparent; padding: 3px 8px; border-radius: 4px; }
#contextMenu { position: absolute; display: none; background: white; border: 1px solid #ccc; padding: 5px; z-index: 9999; }
#deleteArrow { padding: 3px 10px; cursor: pointer; }
.menu-separator { height: 1px; background-color: #ccc; margin: 5px 0; }
.menu-item { padding: 3px 10px; cursor: pointer; }
.menu-item:hover { background-color: #f0f0f0; }
.check-mark { display: inline-block; width: 16px; text-align: center; }
.center-dot { position: absolute; width: 8px; height: 8px; background: red; border-radius: 50%; transform: translate(-50%, -50%); pointer-events: none; z-index: 5; }
</style>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="background-container"></div>
<div class="control-panel">
<div class="control-section">
<div class="control-section">
<h3>增加箭头</h3>
<div class="image-selector">
<div class="image-btn" data-image="./1.png" style="background-image: url('./1.png');"></div>
<div class="image-btn" data-image="./2.png" style="background-image: url('./2.png');"></div>
<div class="image-btn" data-image="./3.png" style="background-image: url('./3.png');"></div>
<div class="image-btn" data-image="./4.png" style="background-image: url('./4.png');"></div>
<div class="image-btn" data-image="./5.png" style="background-image: url('./5.png');"></div>
<div class="image-btn" data-image="./6.png" style="background-image: url('./6.png');"></div>
<div class="image-btn" data-image="images/1.png" style="background-image: url('images/1.png');"></div>
<div class="image-btn" data-image="images/2.png" style="background-image: url('images/2.png');"></div>
<div class="image-btn" data-image="images/3.png" style="background-image: url('images/3.png');"></div>
<div class="image-btn" data-image="images/4.png" style="background-image: url('images/4.png');"></div>
<div class="image-btn" data-image="images/5.png" style="background-image: url('images/5.png');"></div>
<div class="image-btn" data-image="images/6.png" style="background-image: url('images/6.png');"></div>
</div>
<!-- 添加偏移量控制区域 -->
<div class="offset-control" style="margin-top: 15px; padding: 10px; border: 1px solid #eee; border-radius: 4px;">
<label for="global-stop-distance" style="display: block; margin-bottom: 5px;">箭头停止距离 (px):</label>
<input type="number" id="global-stop-distance" min="0" max="100" value="20" style="width: 100%; padding: 8px;">
<p style="font-size: 12px; color: #666; margin: 5px 0 0 0;">* 选择箭头后设置生效</p>
</div>
<!-- 移除保存位置按钮 -->
<button class="save-btn" id="markCenterBtn" style="margin-top: 10px;">标记中心点</button>
<button class="save-btn" id="startAnimationBtn" style="margin-top: 10px;">开始</button>
<button class="save-btn" id="resetBtn" style="margin-top: 10px;">重置</button>
@@ -54,472 +38,9 @@
<div class="menu-item" data-point="top-right"><span class="check-mark"></span>右上角</div>
<div class="menu-item" data-point="bottom-left"><span class="check-mark"></span>左下角</div>
<div class="menu-item" data-point="bottom-right"><span class="check-mark"></span>右下角</div>
<!-- 添加偏移量输入框 -->
<div class="menu-separator"></div>
<div class="menu-item input-item">
<label for="stop-distance">偏移量:</label>
<input type="number" id="stop-distance" min="0" max="100" value="20" style="width:60px;margin-left:5px;">
</div>
</div>
<script>
// 添加常量定义
const ARROW_WIDTH = 155;
const ARROW_HEIGHT = 173;
const STOP_DISTANCE = 50;
// 修复核心整理所有JS代码到单个script标签内
document.addEventListener('DOMContentLoaded', function() {
const backgroundContainer = document.querySelector('.background-container');
const imageButtons = document.querySelectorAll('.image-btn');
const saveBtn = document.getElementById('savePositionsBtn');
const markCenterBtn = document.getElementById('markCenterBtn');
const startAnimationBtn = document.getElementById('startAnimationBtn');
const resetBtn = document.getElementById('resetBtn');
const contextMenu = document.getElementById('contextMenu');
const deleteArrow = document.getElementById('deleteArrow');
let selectedArrow = null;
let centerDot = null;
// 添加重置按钮事件监听器
resetBtn.addEventListener('click', function() {
location.reload();
});
// 添加开始按钮事件监听器
startAnimationBtn.addEventListener('click', startAnimation);
// 动画控制函数 - 确保在DOMContentLoaded回调内部定义
function startAnimation() {
// 检查是否已设置中心点
if (!centerDot) {
alert('请先确定中心点');
return;
}
// 获取中心点坐标修复使用getBoundingClientRect确保坐标正确
const centerRect = centerDot.getBoundingClientRect();
const containerRect = backgroundContainer.getBoundingClientRect();
const centerX = centerRect.left - containerRect.left;
const centerY = centerRect.top - containerRect.top;
// 获取所有箭头
const arrows = document.querySelectorAll('.arrow-container');
if (arrows.length === 0) {
alert('没有可移动的箭头');
return;
}
// 为每个箭头添加动画
arrows.forEach(arrow => {
animateArrow(arrow, centerX, centerY);
});
}
// 单个箭头动画函数
function animateArrow(arrow, targetX, targetY) {
// 获取箭头图片尺寸固定为155px×173px
const arrowWidth = 155;
const arrowHeight = 173;
// 计算中心点偏移量(宽度和高度的一半)
const centerOffsetX = arrowWidth / 2;
const centerOffsetY = arrowHeight / 2;
// 定义停止距离(箭头中心点与目标中心点的距离)
const stopDistance = 50;
// 获取箭头当前位置(相对于背景容器)
const rect = arrow.getBoundingClientRect();
const containerRect = backgroundContainer.getBoundingClientRect();
// 当前位置需要加上偏移量,获取实际中心点位置
let currentX = rect.left - containerRect.left + centerOffsetX;
let currentY = rect.top - containerRect.top + centerOffsetY;
// 添加随机偏移避免重叠
const offsetRange = 20;
const randomOffsetX = (Math.random() - 0.5) * offsetRange;
const randomOffsetY = (Math.random() - 0.5) * offsetRange;
const finalTargetX = targetX + randomOffsetX;
const finalTargetY = targetY + randomOffsetY;
// 计算方向向量和距离
const dxTotal = finalTargetX - currentX;
const dyTotal = finalTargetY - currentY;
const distanceTotal = Math.sqrt(dxTotal * dxTotal + dyTotal * dyTotal);
// 计算调整后的目标位置保持stopDistance距离
let adjustedTargetX, adjustedTargetY;
if (distanceTotal > stopDistance) {
// 如果距离大于停止距离,计算新目标位置
const ratio = (distanceTotal - stopDistance) / distanceTotal;
adjustedTargetX = currentX + dxTotal * ratio;
adjustedTargetY = currentY + dyTotal * ratio;
} else {
// 如果已在停止距离内,保持当前位置
adjustedTargetX = currentX;
adjustedTargetY = currentY;
}
function updatePosition() {
const dx = adjustedTargetX - currentX;
const dy = adjustedTargetY - currentY;
const distance = Math.sqrt(dx * dx + dy * dy);
// 当距离小于2px时停止动画确保到达调整后的目标位置
if (distance < 2) {
arrow.style.left = (adjustedTargetX - centerOffsetX) + 'px';
arrow.style.top = (adjustedTargetY - centerOffsetY) + 'px';
return;
}
const step = Math.max(distance * 0.05, 1);
const moveX = (dx / distance) * step;
const moveY = (dy / distance) * step;
currentX += moveX;
currentY += moveY;
arrow.style.left = (currentX - centerOffsetX) + 'px';
arrow.style.top = (currentY - centerOffsetY) + 'px';
requestAnimationFrame(updatePosition);
}
updatePosition();
}
// 加载保存的元素
loadAllElements();
// 绑定箭头按钮事件
imageButtons.forEach(button => {
button.addEventListener('click', () => {
const imagePath = button.getAttribute('data-image');
addImageToContainer(imagePath);
});
});
// 保存按钮事件
// 删除保存按钮事件监听器
// saveBtn.addEventListener('click', saveAllElements);
// 右键菜单事件
deleteArrow.addEventListener('click', function() {
if (selectedArrow) {
selectedArrow.remove();
saveAllElements();
contextMenu.style.display = 'none';
selectedArrow = null;
}
});
// 为匹配角菜单项添加事件监听器
document.querySelectorAll('.menu-item').forEach(item => {
item.addEventListener('click', function() {
if (selectedArrow) {
const referencePoint = this.getAttribute('data-point');
selectedArrow.dataset.referencePoint = referencePoint;
saveAllElements(); // 保存更改
contextMenu.style.display = 'none';
selectedArrow = null;
}
});
});
// 加载保存的元素
// 点击空白处关闭菜单
document.addEventListener('click', function() {
contextMenu.style.display = 'none';
selectedArrow = null;
});
// 标记中心点功能
markCenterBtn.addEventListener('click', function() {
// 移除旧点
if (centerDot) {
centerDot.remove();
centerDot = null;
}
// 创建新点
function handleMapClick(e) {
const rect = backgroundContainer.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
centerDot = document.createElement('div');
centerDot.className = 'center-dot';
centerDot.style.left = x + 'px';
centerDot.style.top = y + 'px';
backgroundContainer.appendChild(centerDot);
backgroundContainer.removeEventListener('click', handleMapClick);
// 修复中心点保存 - 添加详细日志
console.log('[中心点] 已标记中心点:', { left: centerDot.style.left, top: centerDot.style.top });
saveAllElements(); // 确保保存中心点
}
backgroundContainer.addEventListener('click', handleMapClick);
});
// 添加箭头函数
function addImageToContainer(imagePath, position = null, textContent = '') {
const container = document.createElement('div');
container.className = 'arrow-container';
container.dataset.imagePath = imagePath;
const newImage = document.createElement('div');
newImage.className = 'arrow-image';
newImage.style.backgroundImage = `url('${imagePath}')`;
container.appendChild(newImage);
// 添加文字元素
if (textContent) {
const textElement = document.createElement('div');
textElement.className = 'arrow-text';
textElement.textContent = textContent;
container.dataset.text = textContent;
container.appendChild(textElement);
}
// 设置位置 - 使用中心点作为参照
const arrowWidth = 155;
const arrowHeight = 173;
const centerOffsetX = arrowWidth / 2;
const centerOffsetY = arrowHeight / 2;
if (position) {
// 修复:直接使用传递的左上角位置,不再减去偏移量
container.style.left = parseFloat(position.left) + 'px';
container.style.top = parseFloat(position.top) + 'px';
} else {
// 随机位置也使用中心点作为参照
const x = Math.random() * (backgroundContainer.offsetWidth - arrowWidth) + centerOffsetX;
const y = Math.random() * (backgroundContainer.offsetHeight - arrowHeight) + centerOffsetY;
container.style.left = (x - centerOffsetX) + 'px';
container.style.top = (y - centerOffsetY) + 'px';
}
// 双击编辑文字
container.addEventListener('dblclick', (e) => {
e.stopPropagation();
const currentText = container.dataset.text || '';
const textContent = prompt('请输入文字:', currentText);
if (textContent !== null) {
if (textContent.trim() === '') {
container.querySelector('.arrow-text')?.remove();
delete container.dataset.text;
} else {
let textElement = container.querySelector('.arrow-text');
if (!textElement) {
textElement = document.createElement('div');
textElement.className = 'arrow-text';
container.appendChild(textElement);
}
textElement.textContent = textContent;
container.dataset.text = textContent;
saveAllElements(); // 文字编辑后自动保存
}
}
});
// 右键菜单支持
container.addEventListener('contextmenu', function(e) {
e.preventDefault();
selectedArrow = this;
contextMenu.style.left = `${e.clientX}px`;
contextMenu.style.top = `${e.clientY}px`;
contextMenu.style.display = 'block';
// 显示当前选中的匹配角对号
const currentPoint = selectedArrow.dataset.referencePoint || 'top-left';
document.querySelectorAll('.menu-item').forEach(item => {
const checkMark = item.querySelector('.check-mark');
if (!checkMark) return; // Add null check
if (item.dataset.point === currentPoint) {
checkMark.textContent = '✓';
} else {
checkMark.textContent = '';
}
});
// 添加偏移量保存逻辑
const stopDistanceInput = document.getElementById('stop-distance');
// 设置输入框当前值
stopDistanceInput.value = selectedArrow.dataset.stopDistance || 20;
// 点击空白处保存偏移量
contextMenu.addEventListener('click', function(e) {
e.stopPropagation(); // 阻止事件冒泡到document
});
// 修改输入框事件处理,添加失去焦点时保存
// Single declaration at the top
//const stopDistanceInput = document.getElementById('stop-distance');
// Later in your code (no redeclaration)
stopDistanceInput.addEventListener('blur', function() {
if (selectedArrow) {
selectedArrow.dataset.stopDistance = this.value;
console.log(`[设置] 停止半径: ${this.value}px`);
saveAllElements(); // 立即保存
}
});
// 修改原有点击保存逻辑,仅在点击菜单外部时触发
function saveStopDistance(e) {
if (!contextMenu.contains(e.target)) {
if (selectedArrow) {
selectedArrow.dataset.stopDistance = stopDistanceInput.value;
console.log(`[设置] 停止半径: ${stopDistanceInput.value}px`);
}
document.removeEventListener('click', saveStopDistance);
contextMenu.style.display = 'none';
}
}
});
// 拖拽功能
makeElementDraggable(container);
backgroundContainer.appendChild(container);
saveAllElements(); // 添加后自动保存
return container;
}
// 拖拽实现
function makeElementDraggable(element) {
let isDragging = false, offsetX, offsetY;
element.addEventListener('mousedown', (e) => {
if (e.button === 2) return; // 忽略右键
isDragging = true;
const rect = element.getBoundingClientRect();
offsetX = e.clientX - rect.left;
offsetY = e.clientY - rect.top;
element.style.zIndex = '10';
});
document.addEventListener('mousemove', (e) => {
if (!isDragging) return;
const x = e.clientX - offsetX;
const y = e.clientY - offsetY;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
// 实时保存并控制台输出
saveAllElements();
console.log(`箭头移动并保存: 位置(${x}, ${y})`);
});
document.addEventListener('mouseup', () => {
if (isDragging) {
isDragging = false;
element.style.zIndex = '2';
saveAllElements(); // 拖拽结束后自动保存
}
});
}
// 保存/加载功能
function saveAllElements() {
const elements = [];
const arrowWidth = 155;
const arrowHeight = 173;
const centerOffsetX = arrowWidth / 2;
const centerOffsetY = arrowHeight / 2;
document.querySelectorAll('.arrow-container').forEach(arrow => {
// 保存中心点坐标而非左上角坐标
const left = parseFloat(arrow.style.left) || 0;
const top = parseFloat(arrow.style.top) || 0;
elements.push({
type: 'arrow',
imagePath: arrow.dataset.imagePath,
left: left + centerOffsetX,
top: top + centerOffsetY,
text: arrow.dataset.text || '',
referencePoint: arrow.dataset.referencePoint || 'top-left', // 默认左上角
stopDistance: arrow.dataset.stopDistance || 20 // 添加此行保存偏移量
});
});
// 保存中心点
if (centerDot) {
elements.push({
type: 'center',
left: centerDot.style.left,
top: centerDot.style.top
});
}
localStorage.setItem('savedElements', JSON.stringify(elements));
// 添加详细保存信息
const centerElement = elements.find(el => el.type === 'center');
if (centerElement) {
console.log(`[${new Date().toLocaleTimeString()}] 中心点已保存:`, centerElement);
} else {
console.log(`[${new Date().toLocaleTimeString()}] 未检测到中心点,未保存`);
}
console.log(`[${new Date().toLocaleTimeString()}] 自动保存完成: 共${elements.length}个元素`);
console.log('保存内容:', elements);
}
function loadAllElements() {
const savedElements = JSON.parse(localStorage.getItem('savedElements') || '[]');
const arrowWidth = 155;
const arrowHeight = 173;
const centerOffsetX = arrowWidth / 2;
const centerOffsetY = arrowHeight / 2;
savedElements.forEach(item => {
if (item.type === 'arrow') {
// 加载时使用中心点坐标减去偏移量得到左上角位置
addImageToContainer(item.imagePath, {
left: item.left - centerOffsetX,
top: item.top - centerOffsetY
}, item.text);
// 添加停止距离加载
const arrows = backgroundContainer.querySelectorAll('.arrow-container');
const lastArrow = arrows[arrows.length - 1];
lastArrow.dataset.stopDistance = item.stopDistance || 20;
} else if (item.type === 'center') {
// 创建中心点
centerDot = document.createElement('div');
centerDot.className = 'center-dot';
centerDot.style.left = item.left;
centerDot.style.top = item.top;
backgroundContainer.appendChild(centerDot);
console.log('[加载] 已恢复中心点:', item);
centerDotExists = true; // 添加变量声明
saveAllElements(); // 添加此行
}
});
}
}); // DOMContentLoaded回调结束
// 确保这里没有残留的startAnimation函数定义
function animateArrow(arrow) {
// 获取自定义停止半径
const stopDistance = parseFloat(arrow.dataset.stopDistance) || 20;
const centerX = parseFloat(centerDot.style.left) || 0;
const centerY = parseFloat(centerDot.style.top) || 0;
// 获取箭头当前位置和参照点
let currentX = parseFloat(arrow.style.left);
let currentY = parseFloat(arrow.style.top);
const referencePoint = arrow.dataset.referencePoint || 'top-left';
// 根据参照点计算实际位置
if (referencePoint === 'top-right') currentX += 155;
if (referencePoint === 'bottom-left') currentY += 173;
if (referencePoint === 'bottom-right') { currentX += 155; currentY += 173; }
// 计算距离
const dx = centerX - currentX;
const dy = centerY - currentY;
const distance = Math.sqrt(dx * dx + dy * dy);
// 判断是否到达停止距离
if (distance > stopDistance) {
// 移动逻辑...
} else {
return true; // 已停止
}
}
</script>
<script src="js/script.js"></script>
</body>
</html>