albumDrawBox.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <template>
  2. <photo-consumer
  3. class="carousel-item"
  4. :src="watermark || base_img_url2 + (photo.resFilename && !name ? photo.resFilename : photo.filename) + resize"
  5. >
  6. <img v-if="Math.abs(current - index) < 3" crossorigin="anonymous" @load="drawWatermark($event, name)" loading="lazy" :src="watermark || (base_img_url2 + (photo.resFilename && !name ? photo.resFilename : photo.filename) + resize)" style="width: 100%;" />
  7. <canvas ref="canvasRef" style="position: absolute;"></canvas>
  8. <!-- <div class="tag-text" v-if="showTagBox" >
  9. <span v-html="photo.growText"></span>
  10. <button class="close-button" @click="hideTagBox">✖</button>
  11. </div> -->
  12. <div class="tag-box right" :class="{'leftTop': 'leftTop'}">{{ index+1 }}/{{ length }}</div>
  13. <!-- <div class="center-mark">mark</div>-->
  14. </photo-consumer>
  15. </template>
  16. <script setup>
  17. import { ref, onMounted, onBeforeUnmount, defineProps } from "vue";
  18. import { base_img_url2 } from "@/api/config";
  19. import {imageCache,loadImage} from "./cacheImg.js"
  20. import {dateFormat} from "@/utils/date_util.js"
  21. import {pointToFormat} from "@/utils/util.js"
  22. import {drawTextInRect, drawBorderImageInRect, drawImageInRect, drawRectInRect, drawHorizontalTextList, drawTargetRectangles} from "./utils"
  23. const resize = "?x-oss-process=image/resize,p_50/format,webp/quality,q_50";
  24. const canvasRef = ref(null);
  25. const watermark = ref(null)
  26. const baseMapBig = ref(false)
  27. const props = defineProps({
  28. farmId:{
  29. type: [Number,String],
  30. required: true
  31. },
  32. photo:{
  33. type: Object,
  34. required: true
  35. },
  36. index:{
  37. type: Number,
  38. required: true
  39. },
  40. length:{
  41. type: Number,
  42. required: true
  43. },
  44. current:{
  45. type: Number,
  46. required: true
  47. },
  48. name:{
  49. type: String,
  50. default: ""
  51. }
  52. })
  53. let img = null;
  54. let ctx = null;
  55. let data = {year:props.photo.uploadDate.substring(0,4),
  56. monthDay:dateFormat(new Date(props.photo.uploadDate),'mm/dd'),
  57. address:props.photo.district.replaceAll("\"","") + props.photo.gardenName,
  58. tempImg:imageCache.get("temp"),temp:"10°C-20°C",wendu:"适宜",
  59. feiniao:imageCache.get("feiniao"),
  60. baseMap:imageCache.get("base_map_"+props.photo.treeId),
  61. fusheImg:imageCache.get("fushe"),fushe:"光照优",
  62. shiduImg:imageCache.get("shidu"),shidu:"湿度适宜",
  63. text:"病害风险,及时喷药",
  64. shotCode:props.photo.shotCode,
  65. treeCode:props.photo.treeCode,
  66. pingzhong:props.photo.pingzhong,
  67. uploadDate:props.photo.uploadDate,
  68. }
  69. async function drawWatermark(event,name) {
  70. console.log(name)
  71. if(!name){
  72. drawWatermark1(event)
  73. }else{
  74. drawTargetRectangles2(event.target,props.photo.watermarks.find(item=>item.name === name))
  75. }
  76. }
  77. function drawTargetRectangles2(img, obj) {
  78. const canvas = canvasRef.value;
  79. let scale = 3
  80. canvas.width = img.width * scale;
  81. canvas.height = img.height * scale;
  82. ctx = canvas.getContext('2d');
  83. ctx.scale(scale, scale)
  84. ctx.drawImage(img, 0, 0, img.width, img.height);
  85. drawTargetRectangles(ctx, obj, 4000,3000,img.width, img.height)
  86. watermark.value = canvas.toDataURL();
  87. }
  88. async function drawWatermark1(event) {
  89. img = event.target
  90. await loadImage(props.photo.baseMap,"base_map_"+props.photo.treeId)
  91. data.baseMap = imageCache.get("base_map_"+props.photo.treeId)
  92. if(!watermark.value){
  93. let param = {farmId:props.farmId, date: props.photo.uploadDate}
  94. let weather = null
  95. VE_API.image.findSuitabilityByPoint(param).then((res)=>{
  96. if(res.code === 0){
  97. weather = res.data
  98. drawWatermark2(img,weather)
  99. }else{
  100. drawWatermark2(img,null)
  101. }
  102. })
  103. }
  104. }
  105. function drawWatermark2(img,weather) {
  106. const canvas = canvasRef.value;
  107. let scale = 3
  108. canvas.width = img.width * scale;
  109. canvas.height = img.height * scale;
  110. ctx = canvas.getContext('2d');
  111. ctx.scale(scale, scale)
  112. ctx.drawImage(img, 0, 0, img.width, img.height);
  113. drawBottom(img.width, img.height, weather)
  114. watermark.value = canvas.toDataURL();
  115. }
  116. // console.log(data)
  117. const drawBottom = (imgWidth, imgHeight, weather) => {
  118. if (weather) {
  119. data["temp"] = weather.tempMin + "°C" + "-" + weather.tempMax + "°C" + " " + weather.tempSuitability
  120. data["fushe"] = "光照"+weather.vindexSuitability
  121. data["shidu"] = "湿度"+weather.humiditySuitability
  122. }
  123. // 设置文本样式
  124. ctx.font = "8px Arial";
  125. ctx.textAlign = "left"; // 设置为左对齐
  126. let imgRect = { x: 0, y: 0, width: imgWidth, height: imgHeight}
  127. // 绘制头部黑色半透明遮罩
  128. let topRect = drawRectInRect(ctx,imgRect, 0, 0, 30, 10,"rgba(0, 0, 0, 0.6)")
  129. // 绘制黑色半透明遮罩
  130. let bottomRect = drawRectInRect(ctx,imgRect, 0, 5/6 * 100, 100, 1/6 * 100,"rgba(0, 0, 0, 0.6)")
  131. // drawHorizontalTextList(ctx, topRect, '#ffffff90',[data.treeCode, "蓬径:5m", "高度:3m", "高产树",data.pingzhong],
  132. drawHorizontalTextList(ctx, topRect, '#ffffff90',[data.treeCode],
  133. 40, 0,
  134. 65 , 0,"|",0,
  135. 30,"#ffffff50",1,2)
  136. ctx.fillStyle = "white"; // 文本颜色为白色
  137. // 绘制温度
  138. let startXPercent = 1;
  139. drawImageInRect(ctx, bottomRect, data.tempImg, startXPercent, 5+10, 5, 30)
  140. drawTextInRect(ctx, bottomRect,`${data.temp}`,startXPercent + 4, 28+10, 20)
  141. // 绘制湿度
  142. drawImageInRect(ctx, bottomRect, data.shiduImg, startXPercent+26, 7 + 10, 4, 30)
  143. drawTextInRect(ctx, bottomRect,`${data.shidu}`,startXPercent + 31, 28 + 10, 20)
  144. // 绘制辐射
  145. drawImageInRect(ctx, bottomRect, data.fusheImg, startXPercent+26 + 18, 7 + 10, 5, 30)
  146. drawTextInRect(ctx, bottomRect,`${data.fushe}`,startXPercent+31 + 18, 28 + 10, 20)
  147. //绘制日期信息
  148. ctx.fillStyle = "#FFFFFF";
  149. drawTextInRect(ctx, bottomRect,`${formatDate(new Date(data.uploadDate))}`,startXPercent +1.7, 75, 24)
  150. //绘制位置信息
  151. ctx.fillStyle = "#FFFFFF90";
  152. drawTextInRect(ctx, bottomRect,`${data.treeCode}_S3_SCS3-3_D0P0G1`,startXPercent +13, 75, 18)
  153. if(data.baseMap){
  154. drawBorderImageInRect(ctx, imgRect, data.baseMap, 2/3*100, 2/3*100,
  155. 1/3*100, 1/3*100, 5, 5)
  156. }
  157. }
  158. const showTagBox = ref(true); // 控制 tag-box 的显示状态
  159. const hideTagBox = (event) => {
  160. event.stopPropagation();
  161. showTagBox.value = false; // 隐藏 tag-box
  162. };
  163. const formatDate = (date) => {
  164. const year = date.getFullYear();
  165. const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始,需要加1
  166. const day = String(date.getDate()).padStart(2, '0');
  167. return `${(year+"").substring(2)}${month}${day}`;
  168. };
  169. </script>
  170. <style lang="scss" scoped>
  171. .canvas-container {
  172. width: 100%;
  173. height: 100%;
  174. display: flex;
  175. justify-content: center;
  176. }
  177. .carousel-item {
  178. min-width: 100%;
  179. flex-shrink: 0;
  180. width: 100%;
  181. pointer-events: auto;
  182. position: relative;
  183. .tag-box {
  184. position: absolute;
  185. bottom: 30%;
  186. left: 50%;
  187. transform: translate(-50%, 50%); // 确保在高二分之一的位置水平居中
  188. height: 18px;
  189. padding: 0 6px;
  190. background: rgba(108, 108, 108, 0.67);
  191. border-radius: 10px;
  192. display: flex;
  193. align-items: center;
  194. color: #FFFFFF;
  195. font-size: 12px;
  196. &.right {
  197. left: auto;
  198. right: 10px;
  199. }
  200. &.leftTop {
  201. height: 25px;
  202. line-height: 26px;
  203. padding: 0 8px;
  204. border-radius: 16px;
  205. background: rgba(0, 0, 0, 0.6);
  206. bottom: auto;
  207. top: 6px;
  208. }
  209. }
  210. .tag-text {
  211. position: absolute;
  212. bottom: 31%;
  213. left: 50%;
  214. width: 80%;
  215. transform: translate(-50%, 50%); // 确保在高二分之一的位置水平居中
  216. height: 24px;
  217. padding: 10px 0px 10px 0px;
  218. background: rgba(0, 0, 0, 0.67);
  219. border-radius: 6px;
  220. display: flex;
  221. align-items: center;
  222. justify-content: center;
  223. text-align: center;
  224. color: #FFFFFF;
  225. font-size: 12px;
  226. }
  227. .center-mark {
  228. position: absolute;
  229. bottom: 10px;
  230. left: 50%;
  231. transform: translateX(-50%);
  232. color: #36402c;
  233. font-size: rpx(24);
  234. font-weight: bold;
  235. padding: rpx(14) rpx(30);
  236. background: linear-gradient(
  237. 90deg,
  238. rgba(255, 255, 255, 0) 0%,
  239. rgba(255, 255, 255, 0.6) 24%,
  240. rgba(255, 255, 255, 0.6) 76%,
  241. rgba(255, 255, 255, 0) 100%
  242. );
  243. }
  244. }
  245. .carousel-item img {
  246. width: 100%;
  247. display: block;
  248. border-radius: 12px;
  249. }
  250. canvas {
  251. position: absolute;
  252. }
  253. .close-button {
  254. background: transparent;
  255. border: none;
  256. color: #FFFFFF;
  257. cursor: pointer;
  258. font-size: 10px; // 可以根据需求调整大小
  259. position: absolute;
  260. top: -1px;
  261. right: -9px; // 调整为合适的间距
  262. transform: translateY(-50%);
  263. }
  264. .floating-img {
  265. position: absolute;
  266. bottom: 0;
  267. right: 0;
  268. width: auto !important;
  269. height: 25% !important;
  270. }
  271. .floating-img-big {
  272. position: fixed !important;
  273. z-index: 99999 !important;
  274. top: 50% !important;
  275. left: 50% !important;
  276. width: auto !important;
  277. height: 100% !important;
  278. transform: translate(-50%, -50%) !important;
  279. }
  280. </style>