|
|
@@ -1,10 +1,10 @@
|
|
|
-import React, { useEffect, useRef, useState } from 'react'
|
|
|
+import React, { useRef, useState } from 'react'
|
|
|
import styles from './index.module.scss'
|
|
|
|
|
|
-type TCProps = { className?: string; children?: React.ReactNode }
|
|
|
+type TCProps = { className?: string; children?: React.ReactNode; zoom?: number }
|
|
|
function TouchContainer(props: TCProps) {
|
|
|
// 缩放比例 和 偏移量
|
|
|
- const [zoom, setZoom] = useState(1)
|
|
|
+ const [zoom, setZoom] = useState(props.zoom || 1)
|
|
|
const [offsetX, setOffsetX] = useState(0)
|
|
|
const [offsetY, setOffsetY] = useState(0)
|
|
|
const touchDataRef = useRef({
|
|
|
@@ -13,11 +13,12 @@ function TouchContainer(props: TCProps) {
|
|
|
startOffsetX: 0, // 元素起始偏移 X
|
|
|
startOffsetY: 0, // 元素起始偏移 Y
|
|
|
initialDistance: 0, // 两指缩放的初始距离
|
|
|
- initialZoom: 1, // 初始缩放比例
|
|
|
+ initialZoom: 2, // 初始缩放比例
|
|
|
midX: 0, // 两指缩放的中点 X 坐标
|
|
|
midY: 0 // 两指缩放的中点 Y 坐标
|
|
|
})
|
|
|
|
|
|
+
|
|
|
// useEffect(() => {
|
|
|
// const touchContainer = document.getElementById('touch-container')
|
|
|
// if (!touchContainer) return
|
|
|
@@ -55,8 +56,8 @@ function TouchContainer(props: TCProps) {
|
|
|
const handleTouchMove = (e: TouchEvent | React.TouchEvent) => {
|
|
|
console.log('touch move')
|
|
|
const touch = e.touches[0]
|
|
|
- const dx = touch.clientY - touchDataRef.current.startY
|
|
|
- const dy = -touch.clientX + touchDataRef.current.startX
|
|
|
+ const dx = touch.clientX - touchDataRef.current.startX
|
|
|
+ const dy = touch.clientY - touchDataRef.current.startY
|
|
|
if (e.touches.length === 1) {
|
|
|
setOffsetX(prev => {
|
|
|
const newX = touchDataRef.current.startOffsetX - dx * 0.5
|
|
|
@@ -72,18 +73,18 @@ function TouchContainer(props: TCProps) {
|
|
|
// 计算当前两指之间的距离
|
|
|
const distance = Math.hypot(touch2.clientX - touch.clientX, touch2.clientY - touch.clientY)
|
|
|
// 根据距离调整缩放比例
|
|
|
- const newZoom = Math.max(0.8, Math.min(3, distance / touchDataRef.current.initialDistance * touchDataRef.current.initialZoom))
|
|
|
+ const newZoom = Math.max(0.8, Math.min(4, distance / touchDataRef.current.initialDistance * touchDataRef.current.initialZoom))
|
|
|
setZoom(newZoom)
|
|
|
- setOffsetX(prev => {
|
|
|
- const deltaMidX = (touch2.clientX + touch.clientX) / 2 - touchDataRef.current.midX
|
|
|
- const newX = touchDataRef.current.startOffsetX + deltaMidX - dx * 0.5
|
|
|
- return newX
|
|
|
- })
|
|
|
- setOffsetY(prev => {
|
|
|
- const deltaMidY = (touch2.clientY + touch.clientY) / 2 - touchDataRef.current.midY
|
|
|
- const newY = touchDataRef.current.startOffsetY + deltaMidY - dy * 0.5
|
|
|
- return newY
|
|
|
- })
|
|
|
+ // setOffsetX(prev => {
|
|
|
+ // const deltaMidX = (touch2.clientX + touch.clientX) / 2 - touchDataRef.current.midX
|
|
|
+ // const newX = touchDataRef.current.startOffsetX + deltaMidX - dx * 0.5
|
|
|
+ // return -newX
|
|
|
+ // })
|
|
|
+ // setOffsetY(prev => {
|
|
|
+ // const deltaMidY = (touch2.clientY + touch.clientY) / 2 - touchDataRef.current.midY
|
|
|
+ // const newY = touchDataRef.current.startOffsetY + deltaMidY - dy * 0.5
|
|
|
+ // return -newY
|
|
|
+ // })
|
|
|
}
|
|
|
}
|
|
|
|