Skip to content

Commit

Permalink
feat: dodule scale
Browse files Browse the repository at this point in the history
  • Loading branch information
yanquanfahei committed May 6, 2023
1 parent 30781c8 commit 16fdf5f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
dist
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"simple-git-hooks": "^2.8.1",
"stylelint": "^15.3.0",
"typescript": "^4.9.4",
"vconsole": "^3.15.0",
"vite": "4.0.4",
"vue-tsc": "^1.0.24"
},
Expand Down
20 changes: 20 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 20 additions & 5 deletions src/components/ImageBox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
willChange: 'transform'
}"
>
<view @touchstart="handleTouchStart">
<view
@touchstart="handleTouchStart"
>
<img
:style="{
width: currentWidth + 'px',
Expand All @@ -33,12 +35,13 @@ import { reactive, ref } from 'vue'
import getSuitableImageSize from './utils/getSuitableImageSize'
import getMultipleTouchPosition from './utils/getMultipleTouchPosition'
import useAnimationPosition from './hooks/useAnimationPosition'
import { minStartTouchOffset } from './variables'
import { minStartTouchOffset, scaleBuffer } from './variables'
import { computePositionEdge, getReachType } from './utils/edgeHandle'
import getPositionOnMoveOrScale from './utils/getPositionOnMoveOrScale'
import { limitScale } from './utils/limitTarget'
import useScrollPosition from './hooks/useScrollPosition'
import useContinuousTap from './hooks/useContinuousTap'
import useDebounceCallback from './hooks/useDebounceCallback'
const props = defineProps({
src: {
Expand Down Expand Up @@ -140,8 +143,8 @@ function onScale (current, clientX, clientY) {
}
}
const handleMove = (nextClientX, nextClientY, currentTouchLength) => {
const { touched, maskTouched, CX, CY, lastCX, lastCY, lastX, lastY, scale, reach, x, y, width, height } = state
const handleMove = useDebounceCallback((nextClientX, nextClientY, currentTouchLength) => {
const { touched, maskTouched, CX, CY, lastCX, lastCY, lastX, lastY, scale, reach, x, y, width, height, naturalWidth, touchLength } = state
if (touched || maskTouched) {
// 单指最小缩放下,以初始移动距离来判断意图
if (currentTouchLength === 0 && initialTouchRef.value === 0) {
Expand Down Expand Up @@ -178,13 +181,25 @@ const handleMove = (nextClientX, nextClientY, currentTouchLength) => {
state.reach = 'x'
return
}
// 目标倍数
const toScale = limitScale(
scale + ((currentTouchLength - touchLength) / 100 / 2) * scale,
naturalWidth / width,
scaleBuffer
)
// 导出变量
emit('expose', { scale: toScale })
Object.assign(state, {
touchLength: currentTouchLength,
reach: currentReach,
scale: toScale,
...getPositionOnMoveOrScale(x, y, width, height, scale, scale, nextClientX, nextClientY, offsetX, offsetY)
})
}
}
}, { maxWait: 8 })
function onTouchmove (e) {
e.preventDefault()
Expand Down
4 changes: 2 additions & 2 deletions src/components/ImagePreview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ function handleReachMove (reachPosition, clientX, clientY, nextScale) {
}
function handleReachUp (clientX, clientY) {
const offsetClientX = clientX - (state.lastCX ?? clientX)
const offsetClientY = clientY - (state.lastCY ?? clientY)
const offsetClientX = clientX - (state.lastCX === null || state.lastCX === undefined ? clientX : state.lastCX)
const offsetClientY = clientY - (state.lastCY === null || state.lastCY === undefined ? clientY : state.lastCY)
let willClose = false
// 下一张
Expand Down
4 changes: 4 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { createSSRApp } from 'vue'
import App from './App.vue'
import './styles.css'
import vconsole from 'vconsole'

new vconsole()

export function createApp () {
const app = createSSRApp(App)
return {
Expand Down

0 comments on commit 16fdf5f

Please sign in to comment.