This commit is contained in:
renyt 2025-05-08 18:26:27 +08:00
parent 5664bfdfda
commit 14bbe24343
2 changed files with 155 additions and 67 deletions

BIN
pages/index/.DS_Store vendored

Binary file not shown.

View File

@ -2,16 +2,11 @@
<view> <view>
<!-- 定义canvas画布 // canvas200x200 --> <!-- 定义canvas画布 // canvas200x200 -->
<!-- // @touchstart="handleCanvasTouch" --> <!-- // @touchstart="handleCanvasTouch" -->
<canvas <canvas @touchstart="handleTouchStart" @touchmove="handleTouchMove" @touchend="handleTouchEnd" :style="{
@touchstart="handleTouchStart"
@touchmove="handleTouchMove"
@touchend="handleTouchEnd"
:style="{
width: 1000 + 'px', width: 1000 + 'px',
height: 800 + 'px', height: 800 + 'px',
transform: `translate(${offsetX}px, ${offsetY}px) scale(${scale})` transform: `translate(${offsetX}px, ${offsetY}px) scale(${scale})`
}" }" canvas-id="canvas" id="canvas"></canvas>
canvas-id="canvas" id="canvas" ></canvas>
<!-- style="border: 1px solid red;height: 600px;width: 100vw;" --> <!-- style="border: 1px solid red;height: 600px;width: 100vw;" -->
<image src="https://assets.sx25.troyrc.com/sx25/images/events/XBDT.jpg"></image> <image src="https://assets.sx25.troyrc.com/sx25/images/events/XBDT.jpg"></image>
<!-- 添加一个按钮用于触发绘图 --> <!-- 添加一个按钮用于触发绘图 -->
@ -25,7 +20,9 @@
onMounted, onMounted,
getCurrentInstance getCurrentInstance
} from 'vue' } from 'vue'
import { onReady } from '@dcloudio/uni-app' import {
onReady
} from '@dcloudio/uni-app'
const canvasWidth = ref(375) // Canvas const canvasWidth = ref(375) // Canvas
const canvasHeight = ref(400) // Canvas const canvasHeight = ref(400) // Canvas
@ -38,7 +35,28 @@
const startY = ref(0) const startY = ref(0)
const isDragging = ref(false) const isDragging = ref(false)
const imgUrl = ref('https://assets.sx25.troyrc.com/sx25/images/events/XBDT.jpg'); // ref const imgUrl = ref('https://assets.sx25.troyrc.com/sx25/images/events/XBDT.jpg'); // ref
//
const blocks = ref([{
points: [{
x: 50,
y: 50
}, //
{
x: 150,
y: 50
}, //
{
x: 200,
y: 100
}, //
{
x: 100,
y: 100
}, //
],
message: '点击了四边形',
type: 'quadrilateral'
}]);
async function draw() { async function draw() {
const ctx = uni.createCanvasContext('canvas', instance) // canvas const ctx = uni.createCanvasContext('canvas', instance) // canvas
ctx.scale(scale.value, scale.value); ctx.scale(scale.value, scale.value);
@ -47,6 +65,25 @@
}) })
ctx.drawImage(res.path, 0, 0); ctx.drawImage(res.path, 0, 0);
//
blocks.value.forEach((block) => {
ctx.beginPath();
ctx.fillStyle = block.color;
switch (block.type) {
case 'quadrilateral': //
ctx.moveTo(block.points[0].x, block.points[0].y);
for (let i = 1; i < block.points.length; i++) {
ctx.lineTo(block.points[i].x, block.points[i].y);
}
ctx.closePath();
break;
}
ctx.stroke(); //
});
// ctx.beginPath(); // ctx.beginPath();
// ctx.moveTo(50, 50); // // ctx.moveTo(50, 50); //
// ctx.lineTo(150, 50); // // ctx.lineTo(150, 50); //
@ -90,7 +127,26 @@
// //
const handleTouchStart = (e) => { const handleTouchStart = (e) => {
console.log('handleTouchStart') const x = e.touches[0].x;
const y = e.touches[0].y;
//
blocks.value.forEach((block) => {
if (isPointInQuadrilateral(x, y, block.points)) {
uni.showModal({
title: '提示',
content: block.message,
showCancel: true, //
success: (res) => {
if (res.confirm) {
console.log('用户点击了“确定”按钮');
} else if (res.cancel) {
console.log('用户点击了“取消”按钮');
}
},
});
}
});
if (e.touches.length === 1) { if (e.touches.length === 1) {
// //
startX.value = e.touches[0].clientX - offsetX.value startX.value = e.touches[0].clientX - offsetX.value
@ -98,11 +154,23 @@
isDragging.value = true isDragging.value = true
} else if (e.touches.length === 2) { } else if (e.touches.length === 2) {
// //
const x1 = e.touches[0].clientX // const x1 = e.touches[0].clientX
const y1 = e.touches[0].clientY // const y1 = e.touches[0].clientY
const x2 = e.touches[1].clientX // const x2 = e.touches[1].clientX
const y2 = e.touches[1].clientY // const y2 = e.touches[1].clientY
lastDistance.value = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)) // lastDistance.value = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2))
// ...
const x1 = e.touches[0].clientX,
y1 = e.touches[0].clientY;
const x2 = e.touches[1].clientX,
y2 = e.touches[1].clientY;
const distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
if (lastDistance.value > 0) {
scale.value = Math.min(Math.max(scale.value * (distance / lastDistance.value), 0.5), 4);
draw(); // Canvas
}
} }
} }
@ -157,11 +225,31 @@
console.log(sres, 'sres'); console.log(sres, 'sres');
imgUrl.value = sres.tempFilePath || sres.path imgUrl.value = sres.tempFilePath || sres.path
draw() draw()
},fail:function(fres){ },
fail: function(fres) {
console.log('fres', fres) console.log('fres', fres)
} }
}) })
}) })
//
function isPointInQuadrilateral(px, py, points) {
let inside = false;
const n = points.length;
let p1 = points[0];
for (let i = 1; i <= n; i++) {
let p2 = points[i % n];
if (py > Math.min(p1.y, p2.y) && py <= Math.max(p1.y, p2.y) && px <= Math.max(p1.x, p2.x)) {
if (p1.y !== p2.y) {
let xinters = (py - p1.y) * (p2.x - p1.x) / (p2.y - p1.y) + p1.x;
if (p1.x === p2.x || px <= xinters) {
inside = !inside;
}
}
}
p1 = p2;
}
return inside;
}
</script> </script>
<style scoped> <style scoped>