save.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. <template>
  2. <view class="canvas-box">
  3. <!-- 导航栏 -->
  4. <view class="nav-box">
  5. <view class="title-top" :style="'padding-top:' + statusBarHeight + 'rpx'">
  6. <u-icon class="title-icon" name="arrow-left" color="#ffffff" size="36" @click="getBack"></u-icon>
  7. <text>海报分享</text>
  8. </view>
  9. </view>
  10. <!-- 开发完成之前,取消 fixed;opacity: 0;-->
  11. <canvas style="width: 352px;height: 800px;position: fixed;" class="canvas" canvas-id="canvasID"></canvas>
  12. <!-- 完成海报制作后,需要把canvas移到看不见的地方,或者隐藏,把image显示出来 -->
  13. <!-- <image :src="imgUrl" mode=""></image> -->
  14. <view class="footer">
  15. <view class="download" @click="saveImage">
  16. <!-- 小于符号图标 -->
  17. <u-icon name="download" color="#ffffff" size="34"></u-icon>
  18. <text>保存到相册</text>
  19. </view>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. import wxcode from 'uniapp-qrcode';
  25. export default {
  26. data() {
  27. return {
  28. imgUrl: '',
  29. statusBarHeight: 0,
  30. bgimg: '',
  31. textX: 10, //文字x轴坐标
  32. avatar: 'https://4dscene.4dage.com/new4dkk/deng/static/img/flower_colorful@2x.png', //头像地址
  33. hello: 'https://4dscene.4dage.com/new4dkk/deng/static/img/img_label@2x.png', // hello图标
  34. mony: 'https://4dscene.4dage.com/new4dkk/deng/static/img/flower_pink@2x.png' //圆的钱图标
  35. }
  36. },
  37. async created() {
  38. wxcode.qrcode('qrcode', '1234567890123456789', 420, 420);
  39. // this.$tool.getSystemInfo().then(res => {
  40. // this.statusBarHeight = res
  41. // })
  42. },
  43. async mounted() {
  44. let list = ["知识的殿堂中闪耀属于你的光芒。阿斯顿撒大大大大大大啊啊啊啊", "愿你学业精进,成绩辉煌,"]
  45. let imgData = await uni.getImageInfo({src: this.hello})
  46. this.bgimg = imgData[1].path
  47. let ctx = uni.createCanvasContext('canvasID', this);
  48. // ctx.setFillStyle("transparent"); //设置canvas背景颜色
  49. // ctx.fillRect(0, 0, 346, 500) //设置canvas画布大小
  50. this.fillRoundRect(ctx, 0, 0, 352, 1400, 15, 'transparent'); //绘制一个圆角矩形
  51. // this.fillRoundRect(ctx, 0, 0, 346, 182, 15, '#333231'); //绘制一个圆角矩形
  52. console.log('imgUrlbgimg', this.bgimg)
  53. this.drawrectcular(ctx, this.bgimg, 86, 0, 180, 700) //绘制圆形头像
  54. this.listdrawText(ctx, list)
  55. // this.drawTextVertical(ctx, '绘制武将姓名:陆逊', x, y);
  56. // // 绘制势力汉字:吴
  57. // that.drawInfluence(ctx, that.data.hero.HERO.INFLUENCE);
  58. // // 绘制武将姓名:陆逊
  59. // that.drawName(ctx, that.data.hero.HERO.NAME);
  60. // // 绘制武将称号:江陵侯
  61. // that.drawHorner(ctx, that.data.hero.HERO.HORNER);
  62. // ctx.setFontSize(18)
  63. // ctx.setFillStyle("#ffffff")
  64. // ctx.fillText('明天依然是晴天11', 98, 65)
  65. // ctx.drawImage(this.hello, 240, 10, 86, 86) //二维码
  66. // ctx.font = '20px normal'
  67. // ctx.setFillStyle("#09CFB1")
  68. // ctx.fillText('我为“贤马”带盐', 30, 122)
  69. // ctx.font = '16px normal'
  70. // ctx.setFillStyle("#ffffff")
  71. // ctx.fillText('“闲么?上贤马做兼职”', 20, 152)
  72. // // 绘制职位标题,多余文字自动换行
  73. // ctx.setFontSize(28)
  74. // ctx.setFillStyle("#333333")
  75. // let str = '店铺实习生ZAra重庆龙湖时代'
  76. // // 字符串总长度
  77. // let _strLength = str.length
  78. // // 总结截取次数
  79. // let _strNum = Math.ceil(_strLength / 9)
  80. // // 每次开始截取字符串的索引
  81. // let _strHeight = 0
  82. // // 绘制的字体 x,y的初始位置
  83. // let _strX = 27,
  84. // _strY = 223
  85. // let strIndex = 223
  86. // // 开始截取
  87. // for (let i = 0; i < _strNum; i++) {
  88. // strIndex = _strY + i * 40
  89. // ctx.fillText(str.substr(_strHeight + i * 9, 9), _strX, _strY + i * 40)
  90. // }
  91. // strIndex += 30
  92. // ctx.setFontSize(14)
  93. // ctx.setFillStyle("#1BB99A")
  94. // let strtitle = '环境好/结算快/时间短'
  95. // ctx.fillText(strtitle, _strX, strIndex)
  96. // ctx.setFontSize(20)
  97. // ctx.setFillStyle("#333231")
  98. // ctx.fillText('16元/小时', _strX, 418)
  99. // this.drawCircular(ctx, this.mony, _strX, 429, 14, 14) //绘制圆形头像
  100. // ctx.setFontSize(12)
  101. // ctx.setFillStyle("#F7BA65")
  102. // ctx.fillText('已预付', _strX + 20, 440)
  103. // 绘制微信二维码
  104. ctx.drawImage(this.hello, 208, 370, 120, 120) //二维码
  105. ctx.draw(false, () => {
  106. // 返回canvas图片信息
  107. uni.canvasToTempFilePath({
  108. canvasId: 'canvasID',
  109. success: (res) => {
  110. this.imgUrl = res.tempFilePath
  111. // console.log(res.tempFilePath)
  112. },
  113. fail: function(err) {
  114. console.log(err)
  115. }
  116. })
  117. })
  118. },
  119. methods: {
  120. getBack() {
  121. uni.navigateBack({
  122. delta: 1
  123. });
  124. },
  125. listdrawText(ctx, list){
  126. let mylist = []
  127. list.map(ele => {
  128. if(ele && ele.length > 21){
  129. mylist.push(ele.substring(21, ele.length))
  130. mylist.push(ele.substring(0, 21))
  131. }else {
  132. mylist.push(ele)
  133. }
  134. // mylist.push(ele)
  135. })
  136. this.textX = 200 - (mylist.length/2)* 40;
  137. console.log('mylist', mylist, this.textX)
  138. mylist.map(ele => {
  139. this.drawTextVertical(ctx, ele, this.textX, 160);
  140. this.textX += 30;
  141. })
  142. },
  143. drawTextVertical(context, text, xx, y) {
  144. let x = xx || 0;
  145. var arrText = text.split('');
  146. var arrWidth = arrText.map(function (letter) {
  147. return 18;
  148. });
  149. var align = context.textAlign;
  150. var baseline = context.textBaseline;
  151. if (align == 'left') {
  152. x = x + Math.max.apply(null, arrWidth) / 2;
  153. } else if (align == 'right') {
  154. x = x - Math.max.apply(null, arrWidth) / 2;
  155. }
  156. if (baseline == 'bottom' || baseline == 'alphabetic' || baseline == 'ideographic') {
  157. y = y - arrWidth[0] / 2;
  158. } else if (baseline == 'top' || baseline == 'hanging') {
  159. y = y + arrWidth[0] / 2;
  160. }
  161. context.textAlign = 'center';
  162. context.textBaseline = 'middle';
  163. context.font = '18px normal'
  164. context.setFillStyle("#303030")
  165. // 开始逐字绘制
  166. arrText.forEach(function (letter, index) {
  167. // 确定下一个字符的纵坐标位置
  168. var letterWidth = arrWidth[index];
  169. // 是否需要旋转判断
  170. var code = letter.charCodeAt(0);
  171. console.log('arrWidth', arrWidth, align, index, letter)
  172. if (code <= 256) {
  173. context.translate(x, y);
  174. // 英文字符,旋转90°
  175. context.rotate(90 * Math.PI / 180);
  176. context.translate(-x, -y);
  177. } else if (index > 0 && text.charCodeAt(index - 1) < 256) {
  178. // y修正
  179. y = y + arrWidth[index - 1] / 2;
  180. }
  181. context.fillText(letter, x, y);
  182. // 旋转坐标系还原成初始态
  183. context.setTransform(1, 0, 0, 1, 0, 0);
  184. // 确定下一个字符的纵坐标位置
  185. var letterWidth = arrWidth[index];
  186. y = y + letterWidth;
  187. });
  188. // 水平垂直对齐方式还原
  189. context.textAlign = align;
  190. context.textBaseline = baseline;
  191. },
  192. saveImage() { //点击保存
  193. var _this = this;
  194. uni.saveImageToPhotosAlbum({
  195. filePath: _this.imgUrl,
  196. success() {
  197. uni.showModal({
  198. title: "保存成功",
  199. content: "图片已成功保存到相册,快去分享到您的圈子吧",
  200. showCancel: false
  201. })
  202. }
  203. })
  204. },
  205. // 将网络图片转为临时图片地址
  206. async getImageInfo({imgSrc}) {
  207. return new Promise((resolve, errs) => {
  208. uni.downloadFile({
  209. src: imgSrc,
  210. success: function(image) {
  211. resolve(image);
  212. },
  213. fail(err) {
  214. errs(err);
  215. }
  216. });
  217. });
  218. },
  219. // 绘制圆形头像
  220. drawCircular(ctx, url, x, y, width, height) {
  221. //画圆形头像
  222. var avatarurl_width = width;
  223. var avatarurl_heigth = height;
  224. var avatarurl_x = x;
  225. var avatarurl_y = y;
  226. ctx.save(); //先保存状态,已便于画完园再用
  227. ctx.beginPath(); //开始绘制
  228. ctx.arc(avatarurl_width / 2 + avatarurl_x, avatarurl_heigth / 2 + avatarurl_y, avatarurl_width / 2, 0, Math
  229. .PI * 2, false);
  230. ctx.setFillStyle("#FFFFFF")
  231. ctx.fill() //保证图片无bug填充
  232. ctx.clip(); //剪切
  233. ctx.drawImage(url, avatarurl_x, avatarurl_y, avatarurl_width, avatarurl_heigth); //推进去图片
  234. ctx.restore();
  235. },
  236. // 绘制矩形图片
  237. drawrectcular(ctx, url, x, y, width, height) {
  238. //画圆形头像
  239. var avatarurl_width = width;
  240. var avatarurl_heigth = height;
  241. var avatarurl_x = x;
  242. var avatarurl_y = y;
  243. ctx.save(); //先保存状态,已便于画完园再用
  244. ctx.beginPath(); //开始绘制
  245. ctx.rect(x, y, width, height);
  246. // ctx.arc(avatarurl_width / 2 + avatarurl_x, avatarurl_heigth / 2 + avatarurl_y, avatarurl_width / 2, 0, Math
  247. // .PI * 2, false);
  248. ctx.setFillStyle("#FFFFFF")
  249. ctx.fill() //保证图片无bug填充
  250. ctx.clip(); //剪切
  251. ctx.drawImage(url, avatarurl_x, avatarurl_y, avatarurl_width, avatarurl_heigth); //推进去图片
  252. ctx.restore();
  253. },
  254. // 绘制带圆角的矩形方法
  255. fillRoundRect(cxt, x, y, width, height, radius, fillColor) {
  256. //圆的直径必然要小于矩形的宽高
  257. if (2 * radius > width || 2 * radius > height) {
  258. return false;
  259. }
  260. cxt.save();
  261. cxt.translate(x, y);
  262. //绘制圆角矩形的各个边
  263. this.drawRoundRectPath(cxt, width, height, radius);
  264. cxt.fillStyle = fillColor || '#fff'; //若是给定了值就用给定的值否则给予默认值
  265. cxt.fill();
  266. cxt.restore();
  267. },
  268. drawRoundRectPath(cxt, width, height, radius) {
  269. cxt.beginPath(0);
  270. //从右下角顺时针绘制,弧度从0到1/2PI
  271. cxt.arc(width - radius, height - radius, radius, 0, Math.PI / 2);
  272. //矩形下边线
  273. cxt.lineTo(radius, height);
  274. //左下角圆弧,弧度从1/2PI到PI
  275. cxt.arc(radius, height - radius, radius, Math.PI / 2, Math.PI);
  276. //矩形左边线
  277. cxt.lineTo(0, radius);
  278. //左上角圆弧,弧度从PI到3/2PI
  279. cxt.arc(radius, radius, radius, Math.PI, (Math.PI * 3) / 2);
  280. //上边线
  281. cxt.lineTo(width - radius, 0);
  282. //右上角圆弧
  283. cxt.arc(width - radius, radius, radius, (Math.PI * 3) / 2, Math.PI * 2);
  284. //右边线
  285. cxt.lineTo(width, height - radius);
  286. cxt.closePath();
  287. }
  288. }
  289. }
  290. </script>
  291. <style lang="scss" scoped>
  292. .canvas-box {
  293. height: 100vh;
  294. display: flex;
  295. align-items: center;
  296. justify-content: center;
  297. background-color: #1ABC9C;
  298. /deep/.nav-box {
  299. width: 100%;
  300. padding: 0 20rpx;
  301. position: absolute;
  302. z-index: 9999;
  303. top: 0;
  304. left: 0;
  305. .title-top {
  306. font-size: 36rpx;
  307. font-weight: 550;
  308. color: #FFFFFF;
  309. display: flex;
  310. justify-content: center;
  311. align-items: center;
  312. position: relative;
  313. margin-bottom: 30rpx;
  314. .title-icon {
  315. position: absolute;
  316. left: 0;
  317. }
  318. }
  319. }
  320. image {
  321. width: 335px;
  322. height: 500px;
  323. }
  324. .footer {
  325. display: flex;
  326. align-items: center;
  327. justify-content: space-between;
  328. position: absolute;
  329. justify-content: center;
  330. padding: 0 40rpx;
  331. width: 100%;
  332. left: 0;
  333. bottom: 10%;
  334. .download {
  335. border: 1rpx solid #ffffff;
  336. color: #ffffff;
  337. display: flex;
  338. align-items: center;
  339. }
  340. view {
  341. padding: 0 20rpx;
  342. height: 78rpx;
  343. text-align: center;
  344. line-height: 78rpx;
  345. font-size: 30rpx;
  346. border-radius: 36rpx;
  347. }
  348. }
  349. }
  350. </style>