albumDrawBox.vue 7.6 KB

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