albumDrawBox.vue 8.0 KB

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