This commit is contained in:
renyt 2025-05-08 15:19:32 +08:00
parent 8c8677aeda
commit 1709abef7e
2 changed files with 166 additions and 21 deletions

View File

@ -1,7 +1,9 @@
<template> <template>
<view> <view>
<!-- 定义canvas画布 // canvas200x200 --> <!-- 定义canvas画布 // canvas200x200 -->
<canvas canvas-id="canvas" id="canvas" style="width: 200px; height: 200px;border: 1px solid red;" :width="canvasWidth" :height="canvasHeight"></canvas> <canvas
@touchstart="handleCanvasTouch"
canvas-id="canvas" id="canvas" style="border: 1px solid red;height: 600px;width: 100vw;"></canvas>
<!-- 添加一个按钮用于触发绘图 --> <!-- 添加一个按钮用于触发绘图 -->
<button @click="handleDraw">点击绘制圆形</button> <button @click="handleDraw">点击绘制圆形</button>
@ -9,36 +11,54 @@
</template> </template>
<script setup> <script setup>
import { ref, onMounted ,getCurrentInstance } from 'vue' import {
ref,
onMounted,
getCurrentInstance
} from 'vue'
const canvasWidth = ref(200) // Canvas const canvasWidth = ref(375) // Canvas
const canvasHeight = ref(200) // Canvas const canvasHeight = ref(400) // Canvas
const instance = getCurrentInstance() const instance = getCurrentInstance()
//
function draw() {
const ctx = uni.createCanvasContext('canvas',instance) // canvas
console.log(ctx,'12');
//
ctx.beginPath() //
ctx.arc(100, 100, 80, 0, Math.PI * 2) // (100, 100)80
ctx.setFillStyle('rgba(0, 0, 0, 1)') //
ctx.fill() //
//
ctx.beginPath() // async function draw() {
ctx.arc(100, 100, 75, 0, Math.PI * 2) // (100, 100)60 const imgUrl = "https://assets.sx25.troyrc.com/sx25/images/events/XBDT.jpg"
ctx.setFillStyle('white') // const ctx = uni.createCanvasContext('canvas', instance) // canvas
ctx.fill() // 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.draw() ctx.draw()
} }
// canvas
function handleCanvasTouch(event) {
//
const x = event.touches[0].x;
const y = event.touches[0].y;
console.log(x,y,'12');
}
// //
const handleDraw = () => { const handleDraw = () => {
draw(); // draw(); //
} }
onMounted(() => { onMounted(() => {
// draw() draw()
// Canvas // Canvas
}) })
</script> </script>

125
pages/index/indexclick.vue Normal file
View File

@ -0,0 +1,125 @@
<template>
<view>
<!-- 定义canvas画布 // canvas200x200 -->
<canvas @touchstart="handleCanvasTouch" canvas-id="canvas" id="canvas"
style="border: 1px solid red;height: 600px;width: 100vw;"></canvas>
<!-- 添加一个按钮用于触发绘图 -->
<button @click="handleDraw">点击绘制圆形</button>
</view>
</template>
<script setup>
import {
ref,
onMounted,
getCurrentInstance
} from 'vue'
const canvasWidth = ref(375) // Canvas
const canvasHeight = ref(400) // Canvas
const instance = getCurrentInstance()
//
const blocks = ref([{
x: 50,
y: 50,
width: 100,
height: 100,
color: 'red',
message: '点击了红色方块'
},
{
x: 200,
y: 50,
width: 100,
height: 100,
color: 'blue',
message: '点击了蓝色方块'
},
{
x: 50,
y: 200,
width: 100,
height: 100,
color: 'green',
message: '点击了绿色方块'
},
{
x: 200,
y: 200,
width: 100,
height: 100,
color: 'yellow',
message: '点击了黄色方块'
},
{
x: 350,
y: 50,
width: 100,
height: 100,
color: 'purple',
message: '点击了紫色方块'
},
])
async function draw() {
const imgUrl = "https://assets.sx25.troyrc.com/sx25/images/events/XBDT.jpg"
const ctx = uni.createCanvasContext('canvas', instance) // canvas
const res = await uni.getImageInfo({
src: imgUrl,
})
console.log(res, 'res');
ctx.drawImage(res.path, 0, 0, canvasWidth.value, canvasHeight.value);
// 5
//
blocks.value.forEach((block) => {
ctx.beginPath();
ctx.fillStyle = block.color;
ctx.rect(block.x, block.y, block.width, block.height);
ctx.fill();
});
ctx.draw()
}
// canvas
function handleCanvasTouch(event) {
//
const x = event.touches[0].x;
const y = event.touches[0].y;
// 5
//
blocks.value.forEach((block) => {
if (x >= block.x && x <= block.x + block.width && y >= block.y && y <= block.y + block.height) {
uni.showModal({
title: '提示',
content: block.message,
showCancel: true, //
success: (res) => {
if (res.confirm) {
console.log('用户点击了“确定”按钮');
} else if (res.cancel) {
console.log('用户点击了“取消”按钮');
}
},
});
}
});
}
//
const handleDraw = () => {
draw(); //
}
onMounted(() => {
draw()
// Canvas
})
</script>
<style scoped>
button {
margin-top: 10px;
}
</style>