Skip to content

Commit

Permalink
更新移动端画布缩放
Browse files Browse the repository at this point in the history
  • Loading branch information
more-strive committed Jul 2, 2024
1 parent 92f3cbe commit f3744d7
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 55 deletions.
140 changes: 140 additions & 0 deletions src/app/fabricTouch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { Point, Canvas } from 'fabric'
import { debounce } from 'lodash-es'
import { Disposable } from '@/utils/lifecycle'
import { useFabricStore } from '@/store'

export const MIN_ZOOM = 0.03
export const MAX_ZOOM = 5


export class FabricTouch extends Disposable {
isTwoTouch = false
isDragging = false
startDistance = 1
startX = 0
startY = 0
startScale = 1
lastPan?: Point

constructor(private readonly canvas: Canvas) {
super()
this.initTouchEvent()
}

initTouchEvent() {
const canvas = this.canvas?.upperCanvasEl
if (canvas) {
canvas.addEventListener('touchstart', this.touchStartHandle, { passive: false })
canvas.addEventListener('touchmove', this.touchMoveHandle, { passive: false })
canvas.addEventListener('touchend', this.touchEndHandle, { passive: false })
}
}

removeTouchEvent() {
const canvas = this.canvas?.upperCanvasEl
if (canvas) {
canvas.removeEventListener('touchstart', this.touchStartHandle)
canvas.removeEventListener('touchmove', this.touchMoveHandle)
canvas.removeEventListener('touchend', this.touchEndHandle)
}
}

touchStartHandle = (e: TouchEvent) => {
e.preventDefault()
const canvas = this.canvas
if (!canvas) return
const touches = e.touches

// brushMouseMixin.updateIsDisableDraw(touches.length >= 2)

if (touches.length === 2) {
canvas.isDrawingMode = true
this.isTwoTouch = true
const touch1 = touches[0]
const touch2 = touches[1]
this.startDistance = Math.hypot(
touch2.pageX - touch1.pageX,
touch2.pageY - touch1.pageY
)

this.startX = (touch1.pageX + touch2.pageX) / 2
this.startY = (touch1.pageY + touch2.pageY) / 2
this.startScale = canvas.getZoom()
}
}
touchMoveHandle = (e: TouchEvent) => {
e.preventDefault()

const canvas = this.canvas
if (!canvas) return
const touches = e.touches

if (touches.length === 2) {
const touch1 = touches[0]
const touch2 = touches[1]

const currentDistance = Math.hypot(
touch2.pageX - touch1.pageX,
touch2.pageY - touch1.pageY
)

const x = (touch1.pageX + touch2.pageX) / 2
const y = (touch1.pageY + touch2.pageY) / 2

// Calculate zoom
let zoom = this.startScale * (currentDistance / this.startDistance)
zoom = Math.max(MIN_ZOOM, Math.min(MAX_ZOOM, zoom))
// if (!useBoardStore.getState().isObjectCaching) {
// fabric.Object.prototype.set({
// objectCaching: true
// })
// }
canvas.zoomToPoint(new Point(this.startX, this.startY), zoom)
// paintBoard.evnet?.zoomEvent.updateZoomPercentage(true, zoom)

// Calculate drag distance
const currentPan = new Point(x - this.startX, y - this.startY)
const fabricStore = useFabricStore()
fabricStore.setZoom(zoom)
// move canvas
if (!this.isDragging) {
this.isDragging = true
this.lastPan = currentPan
} else if (this.lastPan) {
// if (!useBoardStore.getState().isObjectCaching) {
// fabric.Object.prototype.set({
// objectCaching: true
// })
// }
canvas.relativePan(
new Point(
currentPan.x - this.lastPan.x,
currentPan.y - this.lastPan.y
)
)
this.lastPan = currentPan
this.saveTransform()
}
}
}
touchEndHandle = (e: TouchEvent) => {
this.isDragging = false
this.canvas.isDrawingMode = false
if (this.isTwoTouch && e.touches.length === 0) {
this.isTwoTouch = false
}
}

saveTransform = debounce(() => {
const transform = this.canvas?.viewportTransform
if (transform) {
// useFileStore.getState().updateTransform(transform)
// if (!useBoardStore.getState().isObjectCaching) {
// fabric.Object.prototype.set({
// objectCaching: false
// })
// }
this.canvas?.requestRenderAll()
}
}, 500)
}
6 changes: 3 additions & 3 deletions src/store/modules/fabric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ export const useFabricStore = defineStore({
// setMouseFrom(data: { x?: number; y?: number; pressure?: number }) {
// Object.assign(this.mouseFrom, data)
// },
// setMouseTo(data: { x?: number; y?: number; pressure?: number }) {
// Object.assign(this.mouseTo, data)
// },
setZoom(val: number) {
this.zoom = val
},
setCanvasPercentage(val: number) {
this.scalePercentage = val
},
Expand Down
2 changes: 2 additions & 0 deletions src/views/Canvas/useCanvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { FabricGuide } from '@/app/fabricGuide'
import { HoverBorders } from '@/app/hoverBorders'
import { WheelScroll } from '@/app/wheelScroll'
import { FabricRuler } from '@/app/fabricRuler'
import { FabricTouch } from '@/app/fabricTouch'
import { isMobile } from '@/utils/common'
import { FabricCanvas } from '@/app/fabricCanvas'
import { Keybinding } from '@/app/keybinding'
Expand Down Expand Up @@ -108,6 +109,7 @@ const initCanvas = () => {
new HoverBorders(canvas)
new WheelScroll(canvas)
new FabricRuler(canvas)
new FabricTouch(canvas)
canvas.preserveObjectStacking = true
canvas.renderAll()
}
Expand Down
65 changes: 13 additions & 52 deletions src/views/Editor/CanvasFooter/index.vue
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<template>
<div>
<el-row>
<el-col :span="6">
换模板
<el-row class="content-center">
<el-col :span="6" class="center-col">
<el-button text>换模板</el-button>
</el-col>
<el-col :span="6">
换文字
<el-col :span="6" class="center-col">
<el-button text>换文字</el-button>
</el-col>
<el-col :span="6">
换图片
<el-col :span="6" class="center-col">
<el-button text>换图片</el-button>
</el-col>
<el-col :span="6">
添加
<el-col :span="6" class="center-col">
<el-button text>添加</el-button>
</el-col>
</el-row>
</div>
Expand Down Expand Up @@ -61,49 +61,10 @@ const loadFile = (files: FileList) => {
</script>

<style lang="scss" scoped>
.footer-left {
display: flex;
width: 80px;
cursor: pointer;
align-items: center;
}
.left-handle {
height: 24px;
flex: 1;
position: relative;
border-radius: $borderRadius;
&:hover{
background-color: #f1f1f1;
}
a {
color: inherit;
.content-center {
.center-col {
display: flex;
justify-content: center;
}
}
.footer-right {
display: flex;
cursor: pointer;
}
.footer-button {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.right-handle {
width: 40px;
flex: 1;
position: relative;
}
// .resize-handler {
// height: 7px;
// position: absolute;
// top: -3px;
// left: 0;
// right: 0;
// cursor: n-resize;
// z-index: 2;
// }
</style>

0 comments on commit f3744d7

Please sign in to comment.