StorePoster.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <template>
  2. <div v-if="posterImageStatus" class="poster-first">
  3. <div class="poster-pop" v-show="!canvasStatus && posterImage">
  4. <img
  5. src="@assets/images/poster-close.png"
  6. class="close"
  7. @click="posterImageClose"
  8. />
  9. <img
  10. :src="posterImage"
  11. ref="conf0"
  12. alt="tp"
  13. class="poster-image"
  14. id="scream"
  15. />
  16. <div class="keep">长按图片可以保存到手机</div>
  17. </div>
  18. <div class="mask" @touchmove.prevent @click="posterImageClose"></div>
  19. <div class="canvasBox">
  20. <canvas ref="myCanvas" width="240" height="412"></canvas>
  21. </div>
  22. </div>
  23. </template>
  24. <script>
  25. export default {
  26. name: "StorePoster",
  27. props: {
  28. posterImageStatus: Boolean,
  29. posterData: Object
  30. },
  31. data: function() {
  32. return {
  33. canvasStatus: false,
  34. posterImage: ""
  35. };
  36. },
  37. watch: {
  38. posterImageStatus: function() {
  39. let that = this;
  40. if (that.posterImageStatus === true) {
  41. that.$nextTick(function() {
  42. that.savePosterPath();
  43. });
  44. }
  45. }
  46. },
  47. mounted: function() {},
  48. methods: {
  49. posterImageClose: function() {
  50. this.posterImageStatus = false;
  51. this.canvasStatus = false;
  52. this.$emit("setPosterImageStatus");
  53. },
  54. savePosterPath: function() {
  55. this.$dialog.loading.open();
  56. this.setHtml2Canvas();
  57. },
  58. setHtml2Canvas: function() {
  59. var c = this.$refs.myCanvas;
  60. var ctx = c.getContext("2d");
  61. var imageData = ctx.getImageData(0, 0, c.width, c.height);
  62. for (var i = 0; i < imageData.data.length; i += 4) {
  63. // 当该像素是透明的,则设置成白色
  64. if (imageData.data[i + 3] == 0) {
  65. imageData.data[i] = 255;
  66. imageData.data[i + 1] = 255;
  67. imageData.data[i + 2] = 255;
  68. imageData.data[i + 3] = 255;
  69. }
  70. }
  71. ctx.putImageData(imageData, 0, 0);
  72. var img = new Image();
  73. let imgY = 225;
  74. img.onload = function() {
  75. ctx.drawImage(img, 0, 0, 240, imgY);
  76. };
  77. img.src = this.posterData.image;
  78. ctx.font = "14px PingFang-SC-Medium";
  79. ctx.fillStyle = "#282828";
  80. var str = this.posterData.title;
  81. var initHeight = 260;
  82. let fontWidth = ctx.measureText(str).width;
  83. this.canvasTextAutoLine(str, c, 10, initHeight, 20, 225, 2);
  84. ctx.font = "16px PingFang-SC-Heavy";
  85. ctx.fillStyle = "#DF2D0A";
  86. let textY = 0;
  87. if (fontWidth < 220) {
  88. textY = initHeight + 30;
  89. } else {
  90. textY = initHeight + 45;
  91. }
  92. ctx.textAlign = "center";
  93. ctx.fillText("¥" + this.posterData.price, 120, textY);
  94. var screamsCode = new Image();
  95. let codeY = textY + 10;
  96. screamsCode.onload = function() {
  97. ctx.drawImage(screamsCode, 10, codeY, 75, 75);
  98. };
  99. screamsCode.src = this.posterData.code;
  100. ctx.font = "12px Arial";
  101. ctx.fillStyle = "#282828";
  102. ctx.textAlign = "left";
  103. let fontY = codeY + 75 / 2;
  104. ctx.fillText("长按识别二维码 立即购买", 90, fontY);
  105. setTimeout(() => {
  106. this.posterImage = c.toDataURL();
  107. this.$dialog.loading.close();
  108. }, 500);
  109. },
  110. /**
  111. * str:要绘制的字符串
  112. * canvas:canvas对象
  113. * initX:绘制字符串起始x坐标
  114. * initY:绘制字符串起始y坐标
  115. * lineHeight:字行高,自己定义个值即可
  116. * canvasWidth:文本宽度
  117. * lines: 行数
  118. */
  119. canvasTextAutoLine(
  120. str,
  121. canvas,
  122. initX,
  123. initY,
  124. lineHeight,
  125. canvasWidth,
  126. lines
  127. ) {
  128. var ctx = canvas.getContext("2d");
  129. var lineWidth = 0;
  130. var lastSubStrIndex = 0;
  131. var beginLineHeight = lineHeight;
  132. var beginY = initY;
  133. for (let i = 0; i < str.length; i++) {
  134. lineWidth += ctx.measureText(str[i]).width;
  135. if (lineWidth > canvasWidth - initX) {
  136. //减去initX,防止边界出现的问题
  137. if (initY >= beginY + beginLineHeight * (lines - 1)) {
  138. ctx.fillText(
  139. str.substring(lastSubStrIndex, i - 1) + "...",
  140. initX,
  141. initY
  142. );
  143. return;
  144. } else {
  145. ctx.fillText(str.substring(lastSubStrIndex, i), initX, initY);
  146. initY += lineHeight;
  147. lineWidth = 0;
  148. lastSubStrIndex = i;
  149. }
  150. }
  151. if (i == str.length - 1) {
  152. ctx.fillText(str.substring(lastSubStrIndex, i + 1), initX, initY);
  153. }
  154. }
  155. }
  156. }
  157. };
  158. </script>
  159. <style scoped>
  160. .poster-first .mask {
  161. z-index: 1001 !important;
  162. }
  163. .canvasBox {
  164. width: 100%;
  165. height: 100%;
  166. margin: auto;
  167. /*display: flex;*/
  168. flex-direction: column;
  169. justify-content: center;
  170. align-items: center;
  171. display: none;
  172. }
  173. .poster-first {
  174. overscroll-behavior: contain;
  175. }
  176. .poster-pop {
  177. width: 4.8rem;
  178. position: fixed;
  179. left: 50%;
  180. transform: translateY(-50%);
  181. -webkit-transform: translateY(-50%);
  182. -o-transform: translateY(-50%);
  183. -ms-transform: translateY(-50%);
  184. -moz-transform: translateY(-50%);
  185. z-index: 9999;
  186. top: 50%;
  187. margin-left: -2.4rem;
  188. }
  189. .poster-pop .canvas {
  190. background-color: #ffffff;
  191. width: 100%;
  192. }
  193. .poster-pop .poster-image {
  194. width: 100%;
  195. display: block;
  196. }
  197. .poster-pop .canvas .image {
  198. width: 100%;
  199. height: 4.5rem;
  200. display: block;
  201. }
  202. .poster-pop .canvas .text {
  203. text-align: center;
  204. margin-top: 0.32rem;
  205. }
  206. .poster-pop .canvas .text.black {
  207. padding: 0 0.1rem;
  208. color: #000;
  209. }
  210. .poster-pop .canvas .text.rad {
  211. color: #ff0000;
  212. }
  213. .poster-pop .canvas .code {
  214. padding: 0 0.1rem 0.2rem 0.1rem;
  215. }
  216. .poster-pop .canvas .code .code-img {
  217. width: 1.7rem;
  218. height: 1.7rem;
  219. }
  220. .poster-pop .canvas .code .code-img img {
  221. width: 100%;
  222. height: 100%;
  223. }
  224. .poster-pop .canvas .code .code-text {
  225. width: 2.8rem;
  226. font-size: 0.24rem;
  227. }
  228. .poster-pop .close {
  229. width: 0.46rem;
  230. height: 0.75rem;
  231. position: fixed;
  232. right: 0;
  233. top: -0.75rem;
  234. display: block;
  235. }
  236. .keep {
  237. color: #fff;
  238. text-align: center;
  239. font-size: 0.25rem;
  240. margin-top: 0.1rem;
  241. }
  242. .mask {
  243. position: fixed;
  244. top: 0;
  245. left: 0;
  246. right: 0;
  247. bottom: 0;
  248. background-color: rgba(0, 0, 0, 0.6);
  249. z-index: 9;
  250. }
  251. img {
  252. border: none;
  253. }
  254. </style>