hch-poster.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. square3() {
  82. let data = this.posterData
  83. let system = this.system
  84. let posterMain = {
  85. r: data.square3.r * system.scale,
  86. w: data.square3.w * system.scale,
  87. h: data.square3.h * system.scale,
  88. x: (system.w - data.square3.w * system.scale) / 2,
  89. y: this.poster.y + data.poster.p + data.square3.mt * system.scale,
  90. color: '#E96737'
  91. }
  92. return posterMain
  93. },
  94. title() {
  95. let data = this.posterData
  96. let system = this.system
  97. let posterTitle = data.title
  98. posterTitle.x = this.square3.x
  99. posterTitle.y = this.square3.y + 30 * system.scale
  100. return posterTitle
  101. },
  102. num() {
  103. let data = this.posterData
  104. let system = this.system
  105. let posterTitle = data.num
  106. posterTitle.x = this.square3.x
  107. posterTitle.y = this.square3.y + 100 * system.scale
  108. return posterTitle
  109. },
  110. txt() {
  111. let data = this.posterData
  112. let system = this.system
  113. let posterTitle = data.txt
  114. posterTitle.x = this.square3.x
  115. posterTitle.y = this.square3.y + 150 * system.scale
  116. return posterTitle
  117. },
  118. },
  119. created() {
  120. // 获取设备信息
  121. this.system = getSystem()
  122. },
  123. methods: {
  124. /**
  125. * @description: 展示图片
  126. * @param {type}
  127. * @return {type}
  128. * @author: hch
  129. */
  130. posterShow() {
  131. this.canvasShow = true
  132. this.creatPoster()
  133. },
  134. /**
  135. * @description: 生成图片
  136. * @author: hch
  137. */
  138. async creatPoster() {
  139. uni.showLoading({
  140. title: '生成图片中...'
  141. })
  142. const ctx = uni.createCanvasContext('myCanvas', this)
  143. this.ctx = ctx
  144. ctx.clearRect(0, 0, this.system.w, this.system.h) //清空之前的图片
  145. ctx.draw() //清空之前的图片
  146. // 根据设备屏幕大小和距离屏幕上下左右距离,及圆角绘制背景
  147. let poster = this.poster
  148. let square3 = this.square3
  149. let title = this.title
  150. let num = this.num
  151. let txt = this.txt
  152. await drawSquarePic(ctx, poster.x, poster.y, poster.w, poster.h, poster.r, poster.url)
  153. await drawSquarePic(ctx, square3.x, square3.y, square3.w, square3.h, square3.r, square3.url, square3
  154. .color)
  155. // 绘制标题 textY 绘制文本的y位置
  156. // console.log('creatPoster -> title.x', title.x)
  157. let textY = drawTextReturnH(
  158. this.ctx,
  159. title.text,
  160. title.x,
  161. title.y,
  162. this.system.w,
  163. title.fontSize,
  164. title.color,
  165. title.lineHeight,
  166. title.align
  167. )
  168. let numY = drawTextReturnH(
  169. this.ctx,
  170. num.text,
  171. num.x,
  172. num.y,
  173. this.system.w,
  174. num.fontSize,
  175. num.color,
  176. num.lineHeight,
  177. num.align
  178. )
  179. let txtY = drawTextReturnH(
  180. this.ctx,
  181. txt.text,
  182. txt.x,
  183. txt.y,
  184. this.system.w,
  185. txt.fontSize,
  186. txt.color,
  187. txt.lineHeight,
  188. txt.align
  189. )
  190. uni.hideLoading()
  191. },
  192. /**
  193. * @description: 保存到系统相册
  194. * @param {type}
  195. * @return {type}
  196. * @author: hch
  197. */
  198. handleSaveCanvasImage() {
  199. uni.showLoading({
  200. title: '保存中...'
  201. })
  202. let _this = this
  203. // 把画布转化成临时文件
  204. // #ifndef MP-ALIPAY
  205. // 支付宝小程序外,其他都是用这个方法 canvasToTempFilePath
  206. uni.canvasToTempFilePath({
  207. x: this.poster.x,
  208. y: this.poster.y,
  209. width: this.poster.w, // 画布的宽
  210. height: this.poster.h, // 画布的高
  211. destWidth: this.poster.w * 5,
  212. destHeight: this.poster.h * 5,
  213. canvasId: 'myCanvas',
  214. success(res) {
  215. //保存图片至相册
  216. // #ifndef H5
  217. // 除了h5以外的其他端
  218. uni.saveImageToPhotosAlbum({
  219. filePath: res.tempFilePath,
  220. success(res) {
  221. uni.hideLoading()
  222. uni.showToast({
  223. title: '图片保存成功',
  224. duration: 2000,
  225. icon: 'none'
  226. })
  227. _this.handleCanvasCancel()
  228. _this.handleCanvasConfirm()
  229. },
  230. fail() {
  231. uni.showToast({
  232. title: '保存失败,稍后再试',
  233. duration: 2000,
  234. icon: 'none'
  235. })
  236. uni.hideLoading()
  237. }
  238. })
  239. // #endif
  240. // #ifdef H5
  241. // h5的时候
  242. uni.showToast({
  243. title: '请长按保存',
  244. duration: 3000,
  245. icon: 'none'
  246. })
  247. _this.handleCanvasCancel()
  248. _this.$emit('previewImage', res.tempFilePath)
  249. // #endif
  250. },
  251. fail(res) {
  252. // console.log('fail -> res', res)
  253. uni.showToast({
  254. title: '保存失败,稍后再试',
  255. duration: 2000,
  256. icon: 'none'
  257. })
  258. uni.hideLoading()
  259. }
  260. },
  261. this
  262. )
  263. // #endif
  264. // #ifdef MP-ALIPAY
  265. // 支付宝小程序条件下 toTempFilePath
  266. this.ctx.toTempFilePath({
  267. x: this.poster.x,
  268. y: this.poster.y,
  269. width: this.poster.w, // 画布的宽
  270. height: this.poster.h, // 画布的高
  271. destWidth: this.poster.w * 5,
  272. destHeight: this.poster.h * 5,
  273. success(res) {
  274. //保存图片至相册
  275. my.saveImage({
  276. url: res.apFilePath,
  277. showActionSheet: true,
  278. success(res) {
  279. uni.hideLoading()
  280. uni.showToast({
  281. title: '图片保存成功',
  282. duration: 2000,
  283. icon: 'none'
  284. })
  285. _this.handleCanvasCancel()
  286. },
  287. fail() {
  288. uni.showToast({
  289. title: '保存失败,稍后再试',
  290. duration: 2000,
  291. icon: 'none'
  292. })
  293. uni.hideLoading()
  294. }
  295. })
  296. },
  297. fail(res) {
  298. // console.log('fail -> res', res)
  299. uni.showToast({
  300. title: '保存失败,稍后再试',
  301. duration: 2000,
  302. icon: 'none'
  303. })
  304. uni.hideLoading()
  305. }
  306. },
  307. this
  308. )
  309. // #endif
  310. },
  311. /**
  312. * @description: 取消图片
  313. * @param {type}
  314. * @return {type}
  315. * @author: hch
  316. */
  317. handleCanvasCancel() {
  318. this.canvasShow = false
  319. this.$emit('cancel', true)
  320. },
  321. handleCanvasConfirm() {
  322. this.$emit('canvasConfirm')
  323. }
  324. }
  325. }
  326. </script>
  327. <style lang="scss">
  328. .content {
  329. height: 100%;
  330. text-align: center;
  331. }
  332. .canvas-content {
  333. position: absolute;
  334. top: 0;
  335. .canvas-mask {
  336. position: fixed;
  337. top: 0;
  338. right: 0;
  339. bottom: 0;
  340. left: 0;
  341. z-index: 200;
  342. width: 100%;
  343. height: 100%;
  344. background: rgba(0, 0, 0, 0.5);
  345. }
  346. .canvas {
  347. z-index: 200;
  348. }
  349. .button-wrapper {
  350. position: fixed;
  351. bottom: 80rpx;
  352. z-index: 200;
  353. display: flex;
  354. width: 100%;
  355. height: 72rpx;
  356. justify-content: space-around;
  357. }
  358. .save-btn {
  359. z-index: 16;
  360. width: 40%;
  361. height: 100%;
  362. font-size: 30rpx;
  363. line-height: 72rpx;
  364. color: #fff;
  365. text-align: center;
  366. background: $uni-btn-color;
  367. border-radius: 45rpx;
  368. border-radius: 36rpx;
  369. }
  370. .cancel-btn {
  371. color: $uni-btn-color;
  372. background: #fff;
  373. }
  374. .canvas-close-btn {
  375. position: fixed;
  376. top: 30rpx;
  377. right: 0;
  378. z-index: 12;
  379. width: 60rpx;
  380. height: 60rpx;
  381. padding: 20rpx;
  382. }
  383. }
  384. </style>