albumDrawBox.vue 7.9 KB

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