This commit is contained in:
renyt 2025-05-08 17:30:13 +08:00
parent 1709abef7e
commit 9fc632a79d

View File

@ -1,9 +1,8 @@
<template>
<view>
<!-- 定义canvas画布 // canvas200x200 -->
<canvas
@touchstart="handleCanvasTouch"
canvas-id="canvas" id="canvas" style="border: 1px solid red;height: 600px;width: 100vw;"></canvas>
<canvas @touchstart="handleCanvasTouch" @touchmove="handleCanvasTouchMove" @touchend="handleCanvasTouchEnd"
canvas-id="canvas" id="canvas" style="border: 1px solid red;height: 600px;width: 1000px;"></canvas>
<!-- 添加一个按钮用于触发绘图 -->
<button @click="handleDraw">点击绘制圆形</button>
@ -16,42 +15,127 @@
onMounted,
getCurrentInstance
} from 'vue'
//
const translateX = ref(0);
const translateY = ref(0);
const scale = ref(1);
const canvasWidth = ref(375) // Canvas
const canvasHeight = ref(400) // Canvas
const instance = getCurrentInstance()
//
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() {
//
const imgUrl = "https://assets.sx25.troyrc.com/sx25/images/events/XBDT.jpg"
const ctx = uni.createCanvasContext('canvas', instance) // canvas
ctx.translate(translateX.value, translateY.value );
ctx.scale(scale.value, scale.value);
const res = await uni.getImageInfo({
src: imgUrl,
})
ctx.drawImage(res.path, 0, 0);
ctx.beginPath();
ctx.moveTo(50, 50); //
ctx.lineTo(150, 50); //
ctx.lineTo(200, 100); //
ctx.lineTo(100, 100); //
ctx.closePath(); //
ctx.stroke(); //
ctx.drawImage(res.path, 0, 0,canvasWidth.value ,canvasHeight.value);
//
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.draw()
}
// canvas
function handleCanvasTouch(event) {
//
const x = event.touches[0].x;
const y = event.touches[0].y;
console.log(x,y,'12');
// canvas
function handleCanvasTouchMove(event) {
if (event.touches.length === 1) {
//
translateX.value += event.touches[0].x - event.touches[0].startX;
translateY.value += event.touches[0].y - event.touches[0].startY;
} else if (event.touches.length === 2) {
//
const distance1 = Math.sqrt(
(event.touches[0].x - event.touches[1].x) ** 2 +
(event.touches[0].y - event.touches[1].y) ** 2
);
const distance2 = Math.sqrt(
(event.touches[0].startX - event.touches[1].startX) ** 2 +
(event.touches[0].startY - event.touches[1].startY) ** 2
);
scale.value *= distance1 / distance2;
}
draw(); //
}
// canvas
function handleCanvasTouchEnd(event) {
//
event.touches.forEach((touch) => {
touch.startX = touch.x;
touch.startY = touch.y;
});
}
// canvas
function handleCanvasTouch(event) {
//
const x = event.touches[0].x;
const y = event.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('用户点击了“取消”按钮');
}
},
});
}
});
}
//
const handleDraw = () => {
draw(); //
@ -61,6 +145,25 @@
draw()
// Canvas
})
//
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>
<style scoped>