Commit 1fef4e97 by 李伟康

commit 提交

1 parent 600b0f85
......@@ -58,13 +58,12 @@
</view>
</view>
</template>
<movable-area
<!-- <movable-area
:class="['canvas-box',{fill:custom}]"
scale-area
:style="{height:height}"
@touchstart="viewTouchStart"
>
<!-- :style="{aspectRatio:aspectRatio}" -->
<movable-view
class="canvas-view"
scale-min="1"
......@@ -73,10 +72,16 @@
:direction="scaleValue == 1?'none':'all'"
:scale-value="defaultScaleValue"
@scale="scale"
@change="change"
:x="viewX"
:y="viewY"
>
> -->
<zoom-pan
:class="['canvas-box',{fill:custom}]"
:disabled="drawStatus"
:scale-value="defaultScaleValue"
scale-max="5"
scale-min="1"
@scale="scale"
>
<template slot="content">
<canvas
:class="['board-canvas',{'draw-canvas':drawStatus}]"
:canvas-id="canvasId"
......@@ -101,12 +106,14 @@
<view class="skeleton-box"></view>
</template>
</template>
<!-- <image class="under-image" :src="drawImage"
</template>
</zoom-pan>
<!-- <image class="under-image" :src="drawImage"
mode="scaleToFill"
/> -->
</movable-view>
</movable-area>
<!-- </movable-view>
</movable-area> -->
<!-- <color
:colorShow="colorShow"
@colorPickTap="colorPickTapHandler"
......@@ -125,10 +132,13 @@
<script>
import color from "./color.vue"
import utils from "@/utils/utils.js";
import zoomPan from './zoom-pan.vue';
export default {
name: "wn-canvas",
components: {
color
color,
zoomPan
},
data () {
return {
......@@ -228,7 +238,9 @@ export default {
scaleValue: 1,
defaultScaleValue: 1,
viewX: 0,
viewY: 0
viewY: 0,
touchX: 0,
touchY: 0
};
},
props: {
......@@ -550,6 +562,7 @@ export default {
//开始touch
touchStart (e) {
if (!this.drawStatus) return
console.log(e)
this.drawContext.setLineWidth(this.lineWidth)
let {
x,
......@@ -890,22 +903,10 @@ export default {
}
},
/**缩放 */
scale (e) {
this.scaleValue = e.detail.scale
setTimeout(() => {
// this.viewX = this.viewX * this.scaleValue * -1
// this.viewY = this.viewY * this.scaleValue * -1
}, 1e3)
},
change(e){
// console.log(e.detail.x,e.detail.y)
},
viewTouchStart (e) {
let touch = e.changedTouches[0]
// console.log(touch.pageX,touch.pageY)
// this.viewX = -touch.pageX
// this.viewY = -touch.pageY
}
scale: utils.throttle(function (e) {
this.scaleValue = e.scale
}, 200),
}
}
</script>
......
<template>
<view
:class="['container zoom-pan-container',{disabled:disabled}]"
@touchstart="handleTouchStart"
@touchmove="handleTouchMove"
@touchend="handleTouchEnd"
@touchcancel="handleTouchEnd"
>
<view
class="content"
:style="{ transform: transformStyle ,
'transform-origin': transformOriginStyle
}"
>
<slot name="content"></slot>
</view>
</view>
</template>
<script>
import utils from "@/utils/utils.js";
export default {
props: {
disabled: Boolean,
scaleValue: {
type: Number,
default: 1
},
scaleMax: {
type: Number,
default: 10
},
scaleMin: {
type: Number,
default: 0.5
},
},
data () {
return {
startX: 0,
startY: 0,
moveX: 0,
moveY: 0,
scale: 1,
lastScale: 1,
lastTouchDistance: 0,
isScaling: false,
isDragging: false,
transformOrigin: { x: 0, y: 0 },
transformStyle: '',
transformOriginStyle: '0px 0px',
scaleFactor: 0.01,
};
},
watch: {
watchScaleValue () {
this.setScale(this.scaleValue)
}
},
computed: {
watchScaleValue () {
return this.scaleValue
}
},
methods: {
handleTouchStart (e) {
if (this.disabled) return
if (e.touches.length === 2) {
this.isScaling = true;
this.lastTouchDistance = this.getTouchDistance(e.touches);
// 初始化缩放中心为两指间的中心点
this.transformOrigin = this.getTouchCenter(e.touches);
} else if (e.touches.length === 1) {
this.isDragging = true;
[this.startX, this.startY] = [e.touches[0].pageX, e.touches[0].pageY];
this.moveX = 0
this.moveY = 0
}
},
handleTouchMove: utils.throttle(function (e) {
if (this.disabled) return
if (this.isScaling && e.touches.length === 2) {
const currentDistance = this.getTouchDistance(e.touches);
const deltaDistance = currentDistance - this.lastTouchDistance;
// 根据触点距离变化和缩放因子计算新的缩放比例
const newScale = this.lastScale + deltaDistance * this.scaleFactor;
this.setScale(newScale, this.getTouchCenter(e.touches)); // 更新缩放同时更新中心点
this.lastTouchDistance = currentDistance;
} else if (this.isDragging && e.touches.length === 1 && this.scale !== 1) {
const newX = e.touches[0].pageX;
const newY = e.touches[0].pageY;
this.moveX = (newX - this.startX) / this.scale;
this.moveY = (newY - this.startY) / this.scale;
this.updatePosition();
}
}, 16),
setScale (scale, center) {
this.scale = Math.min(Math.max(scale, this.scaleMin), this.scaleMax); // 假设最大缩放为10
this.lastScale = this.scale;
this.$emit('scale', { scale: this.scale })
// 更新transform-origin以使缩放围绕两指中心进行
if (center) this.transformOriginStyle = `${center.x}px ${center.y}px`;
this.updatePosition();
},
getTouchCenter (touches) {
const centerX = (touches[0].pageX + touches[1].pageX) / 2;
const centerY = (touches[0].pageY + touches[1].pageY) / 2;
return { x: centerX, y: centerY };
},
handleTouchEnd () {
if (this.disabled) return
this.isScaling = false;
this.isDragging = false;
},
getTouchDistance (touches) {
const dx = touches[1].pageX - touches[0].pageX;
const dy = touches[1].pageY - touches[0].pageY;
return Math.sqrt(dx * dx + dy * dy);
},
async updatePosition () {
const containerRect = await this.getQueryInfo('.container')
const contentRect = await this.getQueryInfo('.content')
const contentWidth = contentRect.width * this.scale; // 缩放后的内容宽度
const contentHeight = contentRect.height * this.scale; // 缩放后的内容高度
// 计算内容可以移动的最大范围,确保内容不会超出容器
let maxX = (containerRect.width - contentWidth) / 2;
let maxY = (containerRect.height - contentHeight) / 2;
// 限制移动范围,确保内容不移出容器
this.moveX = Math.min(Math.max(this.moveX, -maxX), maxX);
this.moveY = Math.min(Math.max(this.moveY, -maxY), maxY);
// 更新位置并确保内容不超出容器
// 注意:此处需根据实际容器大小和内容尺寸进行调整
this.transformStyle = `translate(${this.moveX}px, ${this.moveY}px) scale(${this.scale})`;
},
getQueryInfo (el) {
return new Promise((resolve, reject) => {
uni.createSelectorQuery().in(this).select(el).boundingClientRect(data => {
resolve(data)
}).exec()
})
}
},
};
</script>
<style>
.zoom-pan-container {
width: 100%;
height: 100%;
overflow: hidden;
position: relative;
}
.content {
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
}
.disabled {
pointer-events: none;
}
</style>
\ No newline at end of file
{
"dependencies": {
"dayjs": "^1.11.11",
"hammerjs": "^2.0.8",
"webpack-spritesmith": "^1.1.0"
}
}
......@@ -537,6 +537,8 @@ export default {
color: #333333;
border-radius: 16rpx;
box-sizing: border-box;
line-height: 80rpx;
padding: 0 !important;
&::placeholder {
color: #999999;
}
......
......@@ -7,10 +7,6 @@
class="content"
:style="{background:background}"
>
<view
class="title"
v-if="title"
>{{title}}</view>
<template v-if="views.length && views.length > 1">
<tabs
:list="views"
......
......@@ -251,6 +251,8 @@ export default {
color: #333333;
border-radius: 16rpx;
box-sizing: border-box;
line-height: 80rpx;
padding: 0 !important;
&::placeholder {
color: #999999;
}
......
......@@ -110,6 +110,11 @@ export default {
watchPayInfo () {
return this.$store.state.user.paymentType
},
title () {
let { id } = this,
name = this.media.find((e) => e.id == id)?.detailTitle || ""
return name;
},
},
watch: {
watchStatus: {
......@@ -174,7 +179,7 @@ export default {
index = toolList.findIndex(item => item.id == id)
if (isDistanceSignificant && index != -1) {
const nextId = this.getNextId(index, toolList, startX, halfScreenWidth)
this.$emit('update:id', nextId)
// this.$emit('update:id', nextId)
}
} catch (error) {
console.log(error)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!