feat: 增加缩放

This commit is contained in:
huanghao1412 2024-07-15 19:45:04 +08:00
parent 1115443b1a
commit a79116d63d
4 changed files with 122 additions and 3 deletions

4
.env
View File

@ -3,9 +3,9 @@ VITE_DEV_PORT = '8080'
# development path
# VITE_DEV_PATH = 'http://192.168.0.34:11887'
VITE_DEV_PATH = 'http://114.115.222.135:9015/'
VITE_DEV_PATH = 'http://192.168.0.34:8102/'
# VITE_DEV_PATH = 'http://192.168.0.120:3001'
VITE_DEV_TOKEN = '2a6c435a-1b8f-43b6-938c-ab6b73f96801'
VITE_DEV_TOKEN = 'dd23c593-c823-4eb3-86d0-54ec77c99880'
# production path
VITE_PRO_PATH = 'http://192.168.0.235:8177'

View File

@ -8,6 +8,8 @@ import dataJson from './data.json'
export const includes = []
export const option = {
canScroll: true,
canDrag: true,
dataset: dataJson,
mapRegion: {
adcode: 'china',

View File

@ -2,6 +2,16 @@
<!-- Echarts 全局设置 -->
<global-setting :optionData="optionData"></global-setting>
<CollapseItem name="地图" :expanded="true">
<SettingItemBox name="允许缩放">
<n-space>
<n-switch v-model:value="optionData.canScroll" size="small"/>
</n-space>
</SettingItemBox>
<SettingItemBox name="允许拖拽">
<n-space>
<n-switch v-model:value="optionData.canDrag" size="small"/>
</n-space>
</SettingItemBox>
<SettingItemBox name="地图区域">
<SettingItem name="默认中国">
<n-select

View File

@ -1,5 +1,5 @@
<template>
<div>
<div ref="box" :style="{scale}" style="position: absolute">
<div class="back-icon" v-if="(enter && levelHistory.length !== 0) || (enter && !isPreview())" @click="backLevel">
<n-icon :color="backColor" :size="backSize * 1.1">
<ArrowBackIcon />
@ -306,6 +306,113 @@ watch(() => customData.value.dataMap, () => {
immediate: true
})
let scale = ref(1)
const handleWheel = (event: any) => {
//
event.preventDefault();
if(!option.value.canScroll) return
//
if (event.deltaY < 0) {
scale.value = Math.min(3, scale.value + 0.1)
}
else {
scale.value = Math.max(0.1, scale.value - 0.1)
}
}
// .go-preview-scale
function findParentWithClass(element: any, className: string) {
//
let parent = element.parentElement;
//
while (parent && !parent.classList.contains(className)) {
parent = parent.parentElement;
}
// null
return parent;
}
const getScaleXY = (v: string) => {
// scale
const regex = /scale\((\d*\.?\d+),\s*(\d*\.?\d+)\)/;
// 使
const matches = v.match(regex);
if (matches) {
const scaleX = parseFloat(matches[1]);
const scaleY = parseFloat(matches[2]);
return [scaleX, scaleY]
} else {
return [1, 1]
}
}
let offsetX:number, offsetY:number
let left:number, top:number
let isDragging = ref(false)
const box = ref()
const handleMousedown = (event: any) => {
if(box.value) {
let el = box.value
isDragging.value = true;
offsetX = event.clientX
offsetY = event.clientY
left = el.style.left.replace('px', '') * 1 || 0
top = el.style.top.replace('px', '') * 1 || 0
//
document.addEventListener('mousemove', handleMousemove)
document.addEventListener('mouseup', handleMouseup)
}
}
//
const handleMousemove = (event: any) => {
if(!option.value.canDrag) return
if (!isDragging.value) return;
if(box.value){
let parent = findParentWithClass(box.value, 'go-preview-scale')
let [scaleX, scaleY] = getScaleXY(parent.style.transform)
let el = box.value
//
const x = left + (event.clientX - offsetX) / scaleX
const y = top + (event.clientY - offsetY) / scaleY
//
el.style.left = `${x}px`;
el.style.top = `${y}px`;
}
}
//
const handleMouseup = (event: any) => {
if(!option.value.canDrag) return
isDragging.value = false;
//
document.removeEventListener('mousemove', handleMousemove);
document.removeEventListener('mouseup', handleMouseup);
}
onMounted(() => {
nextTick(() => {
if(vChartRef.value) {
vChartRef.value.$el.addEventListener('wheel', handleWheel, { passive: false })
}
if(box.value) {
box.value.addEventListener('mousedown', handleMousedown)
}
})
})
onUnmounted(() => {
vChartRef?.value?.$el?.removeEventListener('wheel', handleWheel, { passive: false })
box?.value?.removeEventListener('mousedown', handleMousedown)
})
// // dataset
// watch(
// () => props.chartConfig.option.dataset,