albumDrawBox.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <template>
  2. <photo-consumer
  3. class="carousel-item"
  4. :src="watermark || base_img_url2 + 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} from "./cacheImg.js"
  20. import {dateFormat} from "@/utils/date_util.js"
  21. const resize = "?x-oss-process=image/resize,p_30/format,webp/quality,q_40";
  22. const canvasRef = ref(null);
  23. const watermark = ref(null)
  24. const props = defineProps({
  25. farmId:{
  26. type: [Number,String],
  27. required: true
  28. },
  29. photo:{
  30. type: Object,
  31. required: true
  32. },
  33. index:{
  34. type: Number,
  35. required: true
  36. },
  37. length:{
  38. type: Number,
  39. required: true
  40. },
  41. current:{
  42. type: Number,
  43. required: true
  44. }
  45. })
  46. let img = null;
  47. function drawWatermark(event) {
  48. img = event.target
  49. if(!watermark.value){
  50. let param = {farmId:props.farmId, date: props.photo.uploadDate}
  51. let weather = null
  52. VE_API.miniimage.getByFarmIdAndDate(param).then((res)=>{
  53. if(res.code === 0){
  54. weather = res.data
  55. drawWatermark2(img,weather)
  56. }else{
  57. drawWatermark2(img,null)
  58. }
  59. })
  60. }
  61. }
  62. function drawWatermark2(img,weather) {
  63. const canvas = canvasRef.value;
  64. let scale = 3
  65. canvas.width = img.width * scale;
  66. canvas.height = img.height * scale;
  67. const ctx = canvas.getContext('2d');
  68. ctx.scale(scale, scale)
  69. ctx.drawImage(img, 0, 0, img.width, img.height);
  70. drawBottom(ctx, img.width, img.height, weather)
  71. watermark.value = canvas.toDataURL();
  72. }
  73. let data = {year:props.photo.uploadDate.substring(0,4),
  74. monthDay:dateFormat(new Date(props.photo.uploadDate),'mm/dd'),
  75. address:props.photo.district.replaceAll("\"","") + props.photo.gardenName,
  76. tempImg:imageCache.get("temp"),temp:"20°C",
  77. feiniao:imageCache.get("feiniao"),
  78. fusheImg:imageCache.get("fushe"),fushe:"光照优",
  79. shiduImg:imageCache.get("shidu"),shidu:"湿度适宜",
  80. shotCode:props.photo.shotCode
  81. }
  82. // console.log(data)
  83. const drawBottom = (ctx, imgWidth, imgHeight, weather) => {
  84. if(weather){
  85. data["temp"] = weather.tempMin+"°C"
  86. let fushe = "光照优"
  87. if(weather.uvIndex > 5){
  88. fushe = "辐射过高"
  89. }else if(weather.uvIndex < 1){
  90. fushe = "光照差"
  91. }
  92. data["fushe"] = fushe
  93. let shidu = "湿度适宜"
  94. if(weather.humidity < 10){
  95. shidu = "干旱缺水"
  96. }
  97. data["shidu"] = shidu
  98. }
  99. // 设置遮罩的高度为imgHeight的1/6
  100. const maskHeight = imgHeight / 4;
  101. // 绘制黑色半透明遮罩
  102. ctx.fillStyle = "rgba(0, 0, 0, 0.5)"; // 黑色,50%透明度
  103. ctx.fillRect(0, imgHeight - maskHeight, imgWidth, maskHeight);
  104. // 设置文本样式
  105. ctx.fillStyle = "white"; // 文本颜色为白色
  106. ctx.font = "8px Arial";
  107. ctx.textAlign = "left"; // 设置为左对齐
  108. // 从遮罩的底部开始绘制文本
  109. let currentY = imgHeight - maskHeight + 10; // 字段的起始Y坐标
  110. let currentX = 10; // 字段的起始X坐标
  111. // 绘制年份
  112. const field1 = `${data.year}`;
  113. ctx.fillText(field1, currentX, currentY+10);
  114. currentY += 5;
  115. // 计算下划线的起始和结束坐标
  116. const underlineY = currentY + 10; // 下划线Y坐标稍微低于文本
  117. const underlineWidth = ctx.measureText(field1).width; // 下划线的宽度与文本一致
  118. ctx.strokeStyle = "white"; // 下划线颜色为白色
  119. ctx.lineWidth = 1; // 下划线的宽度
  120. ctx.beginPath();
  121. ctx.moveTo(currentX, underlineY); // 下划线起始点
  122. ctx.lineTo(currentX + underlineWidth, underlineY); // 下划线结束点
  123. ctx.stroke(); // 绘制下划线
  124. // 绘制日期
  125. currentY += 20;
  126. currentX += -3
  127. ctx.fillStyle = "white"; // 文本颜色为白色
  128. ctx.font = "20px Arial";
  129. ctx.textAlign = "left"; // 设置为左对齐
  130. const field2 = `${data.monthDay}`;
  131. ctx.fillText(field2, currentX, currentY + 10);
  132. // 绘制一个圆点
  133. currentX += 60
  134. ctx.beginPath();
  135. ctx.arc(currentX, imgHeight - maskHeight + (maskHeight / 2 + 1), 2, 0, Math.PI * 2); // 绘制圆形
  136. ctx.fill(); // 填充圆形
  137. // 绘制地址
  138. currentY += -10;
  139. currentX += 10
  140. ctx.font = "8px Arial";
  141. const field3 = `${data.address}`;
  142. ctx.fillText(field3, currentX, currentY );
  143. // 绘制温度
  144. currentY += 8;
  145. currentX += -6
  146. ctx.drawImage(data.tempImg, currentX + 2, currentY + 1, 13, 13);
  147. const field4 = `${data.temp}`;
  148. currentX += 15
  149. currentY += 12
  150. ctx.fillText(field4, currentX, currentY);
  151. // 绘制湿度
  152. currentX += 20
  153. currentY += -12
  154. ctx.drawImage(data.shiduImg, currentX +2, currentY + 1, 13, 13);
  155. currentX += 16
  156. currentY += 12
  157. ctx.font = "8px Arial";
  158. const field5 = `${data.shidu}`;
  159. ctx.fillText(field5, currentX, currentY);
  160. // 绘制辐射
  161. currentX += 32
  162. currentY += -12
  163. ctx.drawImage(data.fusheImg, currentX + 2, currentY + 1, 13, 13);
  164. currentX += 16
  165. currentY += 12
  166. ctx.font = "8px Arial";
  167. const field6 = `${data.fushe}`;
  168. ctx.fillText(field6, currentX, currentY);
  169. // 绘制文本信息
  170. currentX =imgWidth - 40;
  171. currentY =imgHeight - 40;
  172. ctx.drawImage(data.feiniao, currentX, currentY, 25, 25);
  173. ctx.fillText(data.shotCode, currentX - 10, currentY + 35);
  174. }
  175. //绘制文字
  176. const drawText = (ctx, textObject) => {
  177. const { x, y, text, color } = textObject;
  178. ctx.fillStyle = `rgb(${color[0]}, ${color[1]}, ${color[2]})`;
  179. ctx.font = 'normal 12px sans-serif';
  180. const textHeight = parseInt(ctx.font, 10); // 获取字体大小计算文本的实际高度
  181. ctx.fillText(text, x, y + textHeight); // 将y调整为y + textHeight // 在指定位置(左上角)绘制文本
  182. };
  183. const showTagBox = ref(true); // 控制 tag-box 的显示状态
  184. const hideTagBox = (event) => {
  185. event.stopPropagation();
  186. showTagBox.value = false; // 隐藏 tag-box
  187. };
  188. </script>
  189. <style lang="scss" scoped>
  190. .canvas-container {
  191. width: 100%;
  192. height: 100%;
  193. display: flex;
  194. justify-content: center;
  195. }
  196. .carousel-item {
  197. min-width: 100%;
  198. flex-shrink: 0;
  199. width: 100%;
  200. pointer-events: auto;
  201. position: relative;
  202. .tag-box {
  203. position: absolute;
  204. bottom: 30%;
  205. left: 50%;
  206. transform: translate(-50%, 50%); // 确保在高二分之一的位置水平居中
  207. height: 18px;
  208. padding: 0 6px;
  209. background: rgba(108, 108, 108, 0.67);
  210. border-radius: 10px;
  211. display: flex;
  212. align-items: center;
  213. color: #FFFFFF;
  214. font-size: 12px;
  215. &.right {
  216. left: auto;
  217. right: 10px;
  218. }
  219. &.leftTop {
  220. height: 25px;
  221. line-height: 26px;
  222. padding: 0 8px;
  223. border-radius: 16px;
  224. background: rgba(0, 0, 0, 0.6);
  225. bottom: auto;
  226. top: 6px;
  227. }
  228. }
  229. .tag-text {
  230. position: absolute;
  231. bottom: 31%;
  232. left: 50%;
  233. width: 80%;
  234. transform: translate(-50%, 50%); // 确保在高二分之一的位置水平居中
  235. height: 24px;
  236. padding: 10px 0px 10px 0px;
  237. background: rgba(0, 0, 0, 0.67);
  238. border-radius: 6px;
  239. display: flex;
  240. align-items: center;
  241. justify-content: center;
  242. text-align: center;
  243. color: #FFFFFF;
  244. font-size: 12px;
  245. }
  246. .center-mark {
  247. position: absolute;
  248. bottom: 10px;
  249. left: 50%;
  250. transform: translateX(-50%);
  251. color: #36402c;
  252. font-size: rpx(24);
  253. font-weight: bold;
  254. padding: rpx(14) rpx(30);
  255. background: linear-gradient(
  256. 90deg,
  257. rgba(255, 255, 255, 0) 0%,
  258. rgba(255, 255, 255, 0.6) 24%,
  259. rgba(255, 255, 255, 0.6) 76%,
  260. rgba(255, 255, 255, 0) 100%
  261. );
  262. }
  263. }
  264. .carousel-item img {
  265. width: 100%;
  266. display: block;
  267. }
  268. canvas {
  269. position: absolute;
  270. }
  271. .close-button {
  272. background: transparent;
  273. border: none;
  274. color: #FFFFFF;
  275. cursor: pointer;
  276. font-size: 10px; // 可以根据需求调整大小
  277. position: absolute;
  278. top: -1px;
  279. right: -9px; // 调整为合适的间距
  280. transform: translateY(-50%);
  281. }
  282. </style>