-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #201 from GuoXiCheng/dev-c
Dev
- Loading branch information
Showing
11 changed files
with
1,192 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@import "tailwindcss/base"; | ||
@import "tailwindcss/components"; | ||
@import "tailwindcss/utilities"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<template> | ||
<canvas id="myCanvas" class="cursor-crosshair"></canvas> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
import { onMounted } from 'vue' | ||
import { AccessibilityNode, AccessibilityWindow } from './types'; | ||
const props = defineProps<{ | ||
rawData: AccessibilityWindow | null; | ||
imgSrc: string; | ||
}>(); | ||
onMounted(async () => { | ||
const data = props.rawData as AccessibilityWindow; | ||
if (!data) return; | ||
const canvas = document.getElementById('myCanvas') as HTMLCanvasElement; | ||
const ctx = canvas.getContext('2d') as CanvasRenderingContext2D; | ||
let renderableWidth: number, renderableHeight: number; | ||
// 加载背景图 | ||
const img = new Image(); | ||
img.src = props.imgSrc; // 在此替换为实际的图片URL | ||
img.onload = function () { | ||
const parentWidth = canvas.parentElement!.clientWidth; | ||
const parentHeight = canvas.parentElement!.clientHeight; | ||
const parentAspect = parentWidth / parentHeight; | ||
const imgAspect = img.width / img.height; | ||
if (imgAspect < parentAspect) { | ||
// 纵向填充 | ||
renderableHeight = parentHeight; | ||
renderableWidth = img.width * (renderableHeight / img.height); | ||
} else if (imgAspect > parentAspect) { | ||
// 横向填充 | ||
renderableWidth = parentWidth; | ||
renderableHeight = img.height * (renderableWidth / img.width); | ||
} else { | ||
// 完全匹配 | ||
renderableWidth = parentWidth; | ||
renderableHeight = parentHeight; | ||
} | ||
canvas.width = renderableWidth; | ||
canvas.height = renderableHeight; | ||
ctx.drawImage(img, 0, 0, renderableWidth, renderableHeight); | ||
}; | ||
canvas.addEventListener('click', (event) => { | ||
const { offsetX, offsetY } = event; | ||
let closestNode = null; | ||
let minDistance = Infinity; | ||
data.nodes.forEach(node => { | ||
const rateW = canvas.width / data.screenWidth; | ||
const rateH = canvas.height / data.screenHeight; | ||
const rateLeft = node.left * rateW; | ||
const rateRight = node.right * rateW; | ||
const rateTop = node.top * rateH; | ||
const rateBottom = node.bottom * rateH; | ||
const centerX = (rateLeft + rateRight) / 2; | ||
const centerY = (rateTop + rateBottom) / 2; | ||
const distance = Math.sqrt(Math.pow(offsetX - centerX, 2) + Math.pow(offsetY - centerY, 2)); | ||
if (distance < minDistance && offsetX >= rateLeft && offsetX <= rateRight && offsetY >= rateTop && offsetY <= rateBottom) { | ||
minDistance = distance; | ||
closestNode = { | ||
...node, | ||
left: rateLeft, | ||
right: rateRight, | ||
top: rateTop, | ||
bottom: rateBottom, | ||
}; | ||
} | ||
}); | ||
if (closestNode !== null) { | ||
closestNode = closestNode as AccessibilityNode; | ||
// 重新绘制背景图 | ||
ctx.clearRect(0, 0, canvas.width, canvas.height); | ||
ctx.drawImage(img, 0, 0, renderableWidth, renderableHeight); | ||
// 绘制标记矩形 | ||
ctx.strokeStyle = 'red'; | ||
ctx.lineWidth = 2; | ||
ctx.strokeRect(closestNode.left, closestNode.top, closestNode.right - closestNode.left, closestNode.bottom - closestNode.top); | ||
} | ||
}); | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.