VX小程序 实现区域转图片预览

方法一

1、安装插件 wxml2canvas

复制代码
npm install --save wxml2canvas

2、类型

复制代码
// 小程序页面
let data={
	list:[{
		type:'wxml',
		class:'.test_center .draw_canvas',
		limit:'.test_center',
		x:0,
		y:0
	}]
}

3、数据结构

复制代码
let testData=[
		{
			PageIndex:1,
			ImageUrl:"https://minio.23544.com:9010/mk-sys-test/origin-paper/432531489095806/2023-06-26-15-42-38/9d43c15bb1834cacb0a8900b5e45dfb2/1.jpg",
			Height:1680.0,
			Width:1200.0,
			Questions:[
				{"QuestionNum":"1-2","X":140.00000450195313,"Y":515.989620895996,"Width":149.77778,"Height":54.77778,"score":1.00,"totalScore":2.00},
				{"QuestionNum":"1111","X":113.10765335144043,"Y":756.3195006567382,"Width":799.77778,"Height":199.77778,"score":0.0,"totalScore":0.0},
				{"QuestionNum":"2222","X":100.0000025946045,"Y":822.2222792822265,"Width":799.77778,"Height":199.77778,"score":0.0,"totalScore":0.0},
				{"QuestionNum":"12","X":95.9895980078125,"Y":1260.989620895996,"Width":1022.77778,"Height":305.77778,"score":1.00,"totalScore":14.00},
			]
		},
		{
			PageIndex:1,
			ImageUrl:"https://minio.23544.com:9010/mk-sys-test/origin-paper/432531489095806/2023-06-26-15-42-38/9d43c15bb1834cacb0a8900b5e45dfb2/1.jpg",
			Height:1680.0,
			Width:1200.0,
			Questions:[
				{"QuestionNum":"1-2","X":140.00000450195313,"Y":515.989620895996,"Width":149.77778,"Height":54.77778,"score":1.00,"totalScore":2.00},
				{"QuestionNum":"1111","X":113.10765335144043,"Y":756.3195006567382,"Width":799.77778,"Height":199.77778,"score":0.0,"totalScore":0.0},
				{"QuestionNum":"2222","X":100.0000025946045,"Y":822.2222792822265,"Width":799.77778,"Height":199.77778,"score":0.0,"totalScore":0.0},
				{"QuestionNum":"12","X":95.9895980078125,"Y":1260.989620895996,"Width":1022.77778,"Height":305.77778,"score":1.00,"totalScore":14.00},
			]
		}
	]

4、页面引用

复制代码
<template>
    <canvas canvas-id="canvas1" class="test_center"></canvas>
</template>

<script setup>
import Wxml2Canvas from 'wxml2canvas';
const onclicks=()=> {
	person.drawImage = new Wxml2Canvas({
		width: 340,
		height: 210,
		element: 'canvas1',
		background: '#f0f0f0',
		finish(url) {
			console.log(7777,url)
			uni.previewImage({
				// 需要预览的图片链接列表
				urls: [url],
				// 为当前显示图片的链接/索引值
				current: url,
				// 图片指示器样式	
				indicator:'default',
				// 是否可循环预览
				loop:false
			});
		},
		error (res) {
		}
	});
	let data = {
		list: [
			{
			   type: 'image',
			   x: 0,
			   y: 0,
			   url: person.itemExam.ImageUrl,
			   style: {
				   width: person.itemExam.Width*person.scaleWidth,
				   height: person.itemExam.Height*person.scaleWidth,
				   border: '0 solid #aaaaaa',
				   boxShadow: '10 20 20 rgba(0, 0, 0, 0.4)'
			   }
		    }
		]
	}
	person.itemExam.Questions.forEach((quest,itIndex)=>{
		data.list.push({
			type: 'text',
			text: quest.score,
			x: quest.X*person.scaleWidth,
			y: quest.Y*person.scaleWidth,
			style: {
				textAlign: 'right',
				width: quest.Width*person.scaleWidth-40,
				height: quest.Height*person.scaleWidth-40,
				fontSize: 16,
				color: 'red',
				border: '0px solid red',
				padding: '6px 40px 0 0'
			}
		})
		data.list.push({
			type: 'text',
			text: '  /'+quest.totalScore,
			x: quest.X*person.scaleWidth,
			y: quest.Y*person.scaleWidth,
			style: {
				textAlign: 'right',
				width: quest.Width*person.scaleWidth-6,
				height: quest.Height*person.scaleWidth-6,
				fontSize: 16,
				color: 'blue',
				border: '1px solid red',
				padding: '6px 6px 0 0'
			}
		})
	})
	
	person.drawImage.draw(data);
}
</script>

方法二

希望我的愚见能够帮助你哦~,若有不足之处,还望指出,你们有更好的解决方法,欢迎大家在评论区下方留言支持,大家一起相互学习参考呀~

相关推荐
源码_V_saaskw9 天前
宇鹿家政服务系统小程序ThinkPHP+UniApp
微信小程序·小程序·uni-app·微信公众平台
说私域9 天前
云零售新中枢:定制化“开源AI智能名片+S2B2C商城小程序”驱动的沉浸式触点进化论
人工智能·小程序·开源·零售
伍哥的传说9 天前
React 轻量级状态管理器Zustand
前端·javascript·react.js·小程序·前端框架·ecmascript
好好的哦9 天前
抖音小程序支付错误码141211
小程序·uni-app
橘子海全栈攻城狮9 天前
【源码+文档+调试讲解】基于web的运动健康小程序的设计与实现y196
java·开发语言·小程序·notepad++
即可皕9 天前
WebSocket长连接在小程序中的实践:消息推送与断线重连机制设计
websocket·网络协议·小程序
Haibakeji9 天前
海拔案例分享-门店业绩管理小程序
大数据·小程序
鱼雀AIGC9 天前
如何仅用AI开发完整的小程序<3>—创建小程序基础框架
小程序·aigc·ai编程
素界UI设计9 天前
北斗导航深度接入小程序打车:高精度定位如何解决定位漂移难题?
小程序
郭玉齐10 天前
uniapp+vue3做小程序,获取容器高度
小程序·uni-app