share-code.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <!--
  2. * @Description: 生成图片组件
  3. * @Version: 1.0.0
  4. * @Autor: hch
  5. * @Date: 2020-08-07 14:48:41
  6. * @LastEditors: Please set LastEditors
  7. * @LastEditTime: 2021-07-31 18:11:35
  8. * 保存图片按钮和关闭按钮 在html代码中写出来 绑定点击方法然后透明 再用canvas 覆盖
  9. -->
  10. <template>
  11. <view class="canvas-content" v-show="canvasShow" :style="'width:' + system.w + 'px; height:' + system.h + 'px;'">
  12. <!-- 遮罩层 -->
  13. <view class="canvas-mask"></view>
  14. <!-- 图片 -->
  15. <!-- :width="system.w" :height="system.h" 支付宝必须要这样设置宽高才有效果 -->
  16. <canvas class="canvas" canvas-id="myCanvas" id="myCanvas"
  17. :style="'width:' + system.w + 'px; height:' + system.h + 'px;'" :width="system.w"
  18. :height="system.h"></canvas>
  19. <view class="button-wrapper">
  20. <!-- 保存图片按钮 -->
  21. <!-- #ifndef MP-QQ -->
  22. <!-- cover-view 标签qq小程序有问题 -->
  23. <cover-view class="save-btn" @tap="handleSaveCanvasImage">保存</cover-view>
  24. <cover-view class="save-btn cancel-btn" @tap="handleCanvasCancel">取消</cover-view>
  25. <!-- #endif -->
  26. <!-- #ifdef MP-QQ -->
  27. <view class="save-btn" @tap="handleSaveCanvasImage">保存</view>
  28. <view class="save-btn cancel-btn" @tap="handleCanvasCancel">取消</view>
  29. <!-- #endif -->
  30. </view>
  31. </view>
  32. </template>
  33. <script>
  34. import {
  35. drawSquarePic,
  36. drawTextReturnH,
  37. getSystem
  38. } from './utils'
  39. export default {
  40. data() {
  41. return {
  42. system: {},
  43. canvasShow: false
  44. }
  45. },
  46. props: {
  47. posterData: {
  48. type: Object,
  49. default: () => {
  50. return {}
  51. }
  52. }
  53. },
  54. computed: {
  55. /**
  56. * @description: 计算图片背景数据
  57. * @param {*}
  58. * @return {*}
  59. * @author: hch
  60. */
  61. poster() {
  62. let data = this.posterData
  63. let system = this.system
  64. let posterBg = {
  65. url: data.poster.url,
  66. r: data.poster.r * system.scale,
  67. w: data.poster.w * system.scale,
  68. h: data.poster.h * system.scale,
  69. x: (system.w - data.poster.w * system.scale) / 2,
  70. y: (system.h - data.poster.h * system.scale) / 2,
  71. p: data.poster.p * system.scale
  72. }
  73. return posterBg
  74. },
  75. /**
  76. * @description: 计算图片头部主图
  77. * @param {*}
  78. * @return {*}
  79. * @author: hch
  80. */
  81. mainImg() {
  82. let data = this.posterData
  83. let system = this.system
  84. let posterMain = {
  85. url: data.mainImg.url,
  86. r: data.mainImg.r * system.scale,
  87. w: data.mainImg.w * system.scale,
  88. h: data.mainImg.h * system.scale,
  89. x: (system.w - data.mainImg.w * system.scale) / 2,
  90. y: this.poster.y + data.poster.p + data.mainImg.mt * system.scale
  91. }
  92. return posterMain
  93. },
  94. /**
  95. * @description: 计算图片标题
  96. * @param {*}
  97. * @return {*}
  98. * @author: hch
  99. */
  100. title() {
  101. let data = this.posterData
  102. let system = this.system
  103. let posterTitle = data.title
  104. posterTitle.x = this.mainImg.x
  105. posterTitle.y = this.mainImg.y + this.mainImg.h + data.title.mt * system.scale
  106. return posterTitle
  107. },
  108. /**
  109. * @description: 计算小程序码
  110. * @param {*}
  111. * @return {*}
  112. * @author: hch
  113. */
  114. codeImg() {
  115. let data = this.posterData
  116. let system = this.system
  117. let posterCode = {
  118. url: data.codeImg.url,
  119. r: data.codeImg.r * system.scale,
  120. w: data.codeImg.w * system.scale,
  121. h: data.codeImg.h * system.scale,
  122. x: (system.w - data.codeImg.w * system.scale) / 2,
  123. y: data.codeImg.mt * system.scale //y需要加上绘图后文本的y
  124. }
  125. return posterCode
  126. }
  127. },
  128. created() {
  129. // 获取设备信息
  130. this.system = getSystem()
  131. },
  132. methods: {
  133. /**
  134. * @description: 展示图片
  135. * @param {type}
  136. * @return {type}
  137. * @author: hch
  138. */
  139. posterShow() {
  140. this.canvasShow = true
  141. this.creatPoster()
  142. },
  143. /**
  144. * @description: 生成图片
  145. * @author: hch
  146. */
  147. async creatPoster() {
  148. uni.showLoading({
  149. title: '生成海报中...'
  150. })
  151. const ctx = uni.createCanvasContext('myCanvas', this)
  152. this.ctx = ctx
  153. ctx.clearRect(0, 0, this.system.w, this.system.h) //清空之前的图片
  154. ctx.draw() //清空之前的图片
  155. // 根据设备屏幕大小和距离屏幕上下左右距离,及圆角绘制背景
  156. let poster = this.poster
  157. let mainImg = this.mainImg
  158. let codeImg = this.codeImg
  159. let title = this.title
  160. await drawSquarePic(ctx, poster.x, poster.y, poster.w, poster.h, poster.r, poster.url)
  161. await drawSquarePic(ctx, mainImg.x, mainImg.y, mainImg.w, mainImg.h, mainImg.r, mainImg.url)
  162. // 绘制标题 textY 绘制文本的y位置
  163. // console.log('creatPoster -> title.x', title.x)
  164. let textY = drawTextReturnH(
  165. ctx,
  166. title.text,
  167. title.x,
  168. title.y,
  169. title.w,
  170. title.fontSize,
  171. title.color,
  172. title.lineHeight,
  173. title.align
  174. )
  175. // 绘制小程序码
  176. await drawSquarePic(
  177. ctx,
  178. codeImg.x,
  179. codeImg.y + textY,
  180. codeImg.w,
  181. codeImg.h,
  182. codeImg.r,
  183. codeImg.url
  184. )
  185. // 小程序的名称
  186. // 长按/扫描识别查看商品
  187. let y = 0
  188. this.posterData.tips.forEach((element, i) => {
  189. if (i == 0) {
  190. y = codeImg.y + textY + element.mt + codeImg.h
  191. } else {
  192. y += element.mt
  193. }
  194. y = drawTextReturnH(
  195. ctx,
  196. element.text,
  197. title.x,
  198. y,
  199. mainImg.w,
  200. element.fontSize,
  201. element.color,
  202. element.lineHeight,
  203. element.align
  204. )
  205. })
  206. uni.hideLoading()
  207. },
  208. /**
  209. * @description: 保存到系统相册
  210. * @param {type}
  211. * @return {type}
  212. * @author: hch
  213. */
  214. handleSaveCanvasImage() {
  215. uni.showLoading({
  216. title: '保存中...'
  217. })
  218. let _this = this
  219. // 把画布转化成临时文件
  220. // #ifndef MP-ALIPAY
  221. // 支付宝小程序外,其他都是用这个方法 canvasToTempFilePath
  222. uni.canvasToTempFilePath({
  223. x: this.poster.x,
  224. y: this.poster.y,
  225. width: this.poster.w, // 画布的宽
  226. height: this.poster.h, // 画布的高
  227. destWidth: this.poster.w * 5,
  228. destHeight: this.poster.h * 5,
  229. canvasId: 'myCanvas',
  230. success(res) {
  231. //保存图片至相册
  232. // #ifndef H5
  233. // 除了h5以外的其他端
  234. uni.saveImageToPhotosAlbum({
  235. filePath: res.tempFilePath,
  236. success(res) {
  237. uni.hideLoading()
  238. uni.showToast({
  239. title: '海报保存成功,可以去分享啦~',
  240. duration: 2000,
  241. icon: 'none'
  242. })
  243. _this.handleCanvasCancel()
  244. },
  245. fail() {
  246. uni.showToast({
  247. title: '保存失败,稍后再试',
  248. duration: 2000,
  249. icon: 'none'
  250. })
  251. uni.hideLoading()
  252. }
  253. })
  254. // #endif
  255. // #ifdef H5
  256. // h5的时候
  257. uni.showToast({
  258. title: '请长按保存',
  259. duration: 3000,
  260. icon: 'none'
  261. })
  262. _this.handleCanvasCancel()
  263. _this.$emit('previewImage', res.tempFilePath)
  264. // #endif
  265. },
  266. fail(res) {
  267. // console.log('fail -> res', res)
  268. uni.showToast({
  269. title: '保存失败,稍后再试',
  270. duration: 2000,
  271. icon: 'none'
  272. })
  273. uni.hideLoading()
  274. }
  275. },
  276. this
  277. )
  278. // #endif
  279. // #ifdef MP-ALIPAY
  280. // 支付宝小程序条件下 toTempFilePath
  281. this.ctx.toTempFilePath({
  282. x: this.poster.x,
  283. y: this.poster.y,
  284. width: this.poster.w, // 画布的宽
  285. height: this.poster.h, // 画布的高
  286. destWidth: this.poster.w * 5,
  287. destHeight: this.poster.h * 5,
  288. success(res) {
  289. //保存图片至相册
  290. my.saveImage({
  291. url: res.apFilePath,
  292. showActionSheet: true,
  293. success(res) {
  294. uni.hideLoading()
  295. uni.showToast({
  296. title: '图片保存成功,可以去分享啦~',
  297. duration: 2000,
  298. icon: 'none'
  299. })
  300. _this.handleCanvasCancel()
  301. },
  302. fail() {
  303. uni.showToast({
  304. title: '保存失败,稍后再试',
  305. duration: 2000,
  306. icon: 'none'
  307. })
  308. uni.hideLoading()
  309. }
  310. })
  311. },
  312. fail(res) {
  313. // console.log('fail -> res', res)
  314. uni.showToast({
  315. title: '保存失败,稍后再试',
  316. duration: 2000,
  317. icon: 'none'
  318. })
  319. uni.hideLoading()
  320. }
  321. },
  322. this
  323. )
  324. // #endif
  325. },
  326. /**
  327. * @description: 取消图片
  328. * @param {type}
  329. * @return {type}
  330. * @author: hch
  331. */
  332. handleCanvasCancel() {
  333. this.canvasShow = false
  334. this.$emit('cancel', true)
  335. },
  336. }
  337. }
  338. </script>
  339. <style lang="scss">
  340. .content {
  341. height: 100%;
  342. text-align: center;
  343. }
  344. .canvas-content {
  345. position: absolute;
  346. top: 0;
  347. .canvas-mask {
  348. position: fixed;
  349. top: 0;
  350. right: 0;
  351. bottom: 0;
  352. left: 0;
  353. z-index: 9;
  354. width: 100%;
  355. height: 100%;
  356. background: rgba(0, 0, 0, 0.5);
  357. }
  358. .canvas {
  359. z-index: 10;
  360. }
  361. .button-wrapper {
  362. position: fixed;
  363. bottom: 80rpx;
  364. z-index: 16;
  365. display: flex;
  366. width: 100%;
  367. height: 72rpx;
  368. justify-content: space-around;
  369. }
  370. .save-btn {
  371. z-index: 16;
  372. width: 40%;
  373. height: 100%;
  374. font-size: 30rpx;
  375. line-height: 72rpx;
  376. color: #fff;
  377. text-align: center;
  378. background: $uni-btn-color;
  379. border-radius: 45rpx;
  380. border-radius: 36rpx;
  381. }
  382. .cancel-btn {
  383. color: $uni-btn-color;
  384. background: #fff;
  385. }
  386. .canvas-close-btn {
  387. position: fixed;
  388. top: 30rpx;
  389. right: 0;
  390. z-index: 12;
  391. width: 60rpx;
  392. height: 60rpx;
  393. padding: 20rpx;
  394. }
  395. }
  396. </style>