|
@@ -4,6 +4,7 @@ import { toRefs } from 'vue'
|
|
|
import { reactive } from 'vue'
|
|
|
import { inject, onUnmounted, onMounted, type ComponentInternalInstance, ref} from 'vue'
|
|
|
import { encode, decode} from 'js-base64'
|
|
|
+import hand from '@/assets/3-4.png'
|
|
|
|
|
|
/**
|
|
|
* 它从 `inject` 函数返回 `StaticImg` 服务
|
|
@@ -224,7 +225,6 @@ export const useAdaptationIpadAndPhone = () => {
|
|
|
}
|
|
|
|
|
|
export const useQueryParmas = async () => {
|
|
|
- console.log(1);
|
|
|
let state = reactive({})
|
|
|
|
|
|
state = await new Promise( (resolve) => {
|
|
@@ -283,25 +283,31 @@ export const useDeviceDire = (cb: (dire: 'H' | 'V') => void) => {
|
|
|
export const useMagnifier = async (
|
|
|
canvas: HTMLCanvasElement,
|
|
|
canvasId: string,
|
|
|
- { ansUrl, quesUrl, instance }: { ansUrl: string, quesUrl: string, instance: ComponentInternalInstance}
|
|
|
+ { ansUrl, quesUrl, instance, rate }
|
|
|
+ :
|
|
|
+ { ansUrl: string, quesUrl: string, instance: ComponentInternalInstance, rate: number}
|
|
|
) => {
|
|
|
-
|
|
|
+
|
|
|
+ const adaptatio = useAdaptationIpadAndPhone()
|
|
|
+
|
|
|
let ctx: UniApp.CanvasContext = uni.createCanvasContext(canvasId)
|
|
|
|
|
|
+ const deviceWidthRate = uni.getSystemInfoSync().windowWidth / 375
|
|
|
+
|
|
|
const draw = async () => {
|
|
|
- ctx.drawImage(quesUrl, 0, 0, 225, 386)
|
|
|
+ ctx.drawImage(quesUrl, 0, 0, 225 * deviceWidthRate, 386 * deviceWidthRate)
|
|
|
ctx.save()
|
|
|
- ctx.drawImage(ansUrl, 225, 0, 75, 386)
|
|
|
+ ctx.drawImage(ansUrl, 225 * deviceWidthRate , 0, 75 * deviceWidthRate, 386 * deviceWidthRate)
|
|
|
ctx.save()
|
|
|
ctx.draw()
|
|
|
ctx.restore();
|
|
|
// 创建 Image 对象并加载图片
|
|
|
-
|
|
|
+
|
|
|
// 初始化放大镜的参数
|
|
|
- const magnifierSize = 150; // 放大镜尺寸
|
|
|
- const magnification = 1.5; // 放大倍数
|
|
|
- const lineWidth = 4 // 放大镜边框宽度
|
|
|
-
|
|
|
+ const magnifierSize = 150 * deviceWidthRate ; // 放大镜尺寸
|
|
|
+ const magnification = 1.5 ; // 放大倍数
|
|
|
+ const lineWidth = 4 * deviceWidthRate// 放大镜边框宽度
|
|
|
+
|
|
|
await new Promise( resolve => {
|
|
|
useSchedulerOnce(() => {
|
|
|
resolve(true)
|
|
@@ -314,15 +320,15 @@ export const useMagnifier = async (
|
|
|
canvasId: canvasId,
|
|
|
fileType: 'png',
|
|
|
quality: 1,
|
|
|
- destWidth: 300,
|
|
|
- destHeight: 386,
|
|
|
+ destWidth: 300 * deviceWidthRate,
|
|
|
+ destHeight: 386 * deviceWidthRate,
|
|
|
success: (res) => resolve( res.tempFilePath),
|
|
|
fail: (error) => {
|
|
|
console.error('canvasToTempFilePath failed: ', error);
|
|
|
}
|
|
|
}, instance);
|
|
|
}) as string
|
|
|
-
|
|
|
+
|
|
|
function initMagnifier () {
|
|
|
// 计算放大镜区域的位置和尺寸
|
|
|
const startX = 0;
|
|
@@ -358,22 +364,23 @@ export const useMagnifier = async (
|
|
|
);
|
|
|
ctx.draw()
|
|
|
ctx.restore();
|
|
|
- console.log('初始化放大镜');
|
|
|
-
|
|
|
}
|
|
|
|
|
|
initMagnifier();
|
|
|
|
|
|
+ // moveMagnifier({ touches: [{pageX:132 + i * 5, pageY: 340 + i * 12 }] })
|
|
|
+
|
|
|
function moveMagnifier (event: TouchEvent) {
|
|
|
- const mouseX = event.touches[0].pageX - canvas.getBoundingClientRect().left;
|
|
|
- const mouseY = event.touches[0].pageY - canvas.getBoundingClientRect().top;
|
|
|
+
|
|
|
+ const mouseX = (event.touches[0].pageX - canvas.getBoundingClientRect().left) * adaptatio * deviceWidthRate
|
|
|
+ const mouseY = (event.touches[0].pageY - canvas.getBoundingClientRect().top ) * adaptatio * deviceWidthRate
|
|
|
|
|
|
// 清空 Canvas
|
|
|
ctx.clearRect(0, 0, canvas.clientWidth, canvas.clientHeight);
|
|
|
|
|
|
// 计算放大镜区域的位置和尺寸
|
|
|
- const startX = mouseX - magnifierSize / 2;
|
|
|
- const startY = mouseY - magnifierSize / 2;
|
|
|
+ const startX = (mouseX - magnifierSize / 2)
|
|
|
+ const startY = (mouseY - magnifierSize / 2)
|
|
|
const width = magnifierSize;
|
|
|
const height = magnifierSize;
|
|
|
|
|
@@ -401,6 +408,48 @@ export const useMagnifier = async (
|
|
|
ctx.restore();
|
|
|
}
|
|
|
|
|
|
+ const move = () => {
|
|
|
+
|
|
|
+ let x = canvas.getBoundingClientRect().left + magnifierSize / 2
|
|
|
+ let y = canvas.getBoundingClientRect().top + magnifierSize / 2
|
|
|
+
|
|
|
+ // 设置目标位置
|
|
|
+ const targetX = 170;
|
|
|
+ const targetY = 460;
|
|
|
+
|
|
|
+ // 设置动画参数
|
|
|
+ const animationDuration = 1000; // 1秒
|
|
|
+ let startTime: number = 0;
|
|
|
+
|
|
|
+ // 动画函数
|
|
|
+ function animate() {
|
|
|
+
|
|
|
+ if (!startTime) startTime = new Date().getTime();
|
|
|
+ const elapsedTime = new Date().getTime() - startTime;
|
|
|
+
|
|
|
+ if (elapsedTime < animationDuration) {
|
|
|
+
|
|
|
+ // 计算当前位置
|
|
|
+ const progress = elapsedTime / animationDuration;
|
|
|
+ x = interpolate(x, targetX, progress);
|
|
|
+ y = interpolate(y, targetY, progress);
|
|
|
+ moveMagnifier({touches: [{pageX: x, pageY: y}]})
|
|
|
+
|
|
|
+ // 继续下一帧绘制
|
|
|
+ requestAnimationFrame(animate);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 线性插值函数
|
|
|
+ function interpolate(start: number, end: number, progress: number) {
|
|
|
+ return start + (end - start) * progress;
|
|
|
+ }
|
|
|
+
|
|
|
+ requestAnimationFrame(animate);
|
|
|
+ }
|
|
|
+
|
|
|
+ // move()
|
|
|
+
|
|
|
// 监听鼠标移动事件,实现放大镜效果
|
|
|
canvas.addEventListener("touchmove", moveMagnifier );
|
|
|
}
|
|
@@ -414,7 +463,7 @@ export const useMagnifier = async (
|
|
|
onUnmounted(() => {
|
|
|
canvas.removeEventListener('touchmove', () => {})
|
|
|
})
|
|
|
-
|
|
|
+
|
|
|
return {
|
|
|
draw, clear
|
|
|
}
|