StorePoster.vue 7.0 KB

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