webview.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <template>
  2. <div>
  3. <web-view src="https://blog.csdn.net/m0_56104994/article/details/131311656"></web-view>
  4. </div>
  5. </template>
  6. <script>
  7. import wxcode from 'uniapp-qrcode';
  8. export default {
  9. data() {
  10. return {
  11. imgUrl: '',
  12. statusBarHeight: 0,
  13. bgimg: '',
  14. textX: 10, //文字x轴坐标
  15. avatar: 'https://4dscene.4dage.com/new4dkk/deng/static/img/flower_colorful@2x.png', //头像地址
  16. hello: 'https://4dscene.4dage.com/new4dkk/deng/static/img/img_label@2x.png', // hello图标
  17. mony: 'https://4dscene.4dage.com/new4dkk/deng/static/img/flower_pink@2x.png' //圆的钱图标
  18. }
  19. },
  20. async created() {
  21. },
  22. async mounted() {
  23. },
  24. methods: {
  25. getBack() {
  26. uni.navigateBack({
  27. delta: 1
  28. });
  29. },
  30. listdrawText(ctx, list){
  31. let mylist = []
  32. list.map(ele => {
  33. if(ele && ele.length > 21){
  34. mylist.push(ele.substring(21, ele.length))
  35. mylist.push(ele.substring(0, 21))
  36. }else {
  37. mylist.push(ele)
  38. }
  39. // mylist.push(ele)
  40. })
  41. this.textX = 200 - (mylist.length/2)* 40;
  42. console.log('mylist', mylist, this.textX)
  43. mylist.map(ele => {
  44. this.drawTextVertical(ctx, ele, this.textX, 160);
  45. this.textX += 30;
  46. })
  47. },
  48. drawTextVertical(context, text, xx, y) {
  49. let x = xx || 0;
  50. var arrText = text.split('');
  51. var arrWidth = arrText.map(function (letter) {
  52. return 18;
  53. });
  54. var align = context.textAlign;
  55. var baseline = context.textBaseline;
  56. if (align == 'left') {
  57. x = x + Math.max.apply(null, arrWidth) / 2;
  58. } else if (align == 'right') {
  59. x = x - Math.max.apply(null, arrWidth) / 2;
  60. }
  61. if (baseline == 'bottom' || baseline == 'alphabetic' || baseline == 'ideographic') {
  62. y = y - arrWidth[0] / 2;
  63. } else if (baseline == 'top' || baseline == 'hanging') {
  64. y = y + arrWidth[0] / 2;
  65. }
  66. context.textAlign = 'center';
  67. context.textBaseline = 'middle';
  68. context.font = '18px normal'
  69. context.setFillStyle("#303030")
  70. // 开始逐字绘制
  71. arrText.forEach(function (letter, index) {
  72. // 确定下一个字符的纵坐标位置
  73. var letterWidth = arrWidth[index];
  74. // 是否需要旋转判断
  75. var code = letter.charCodeAt(0);
  76. console.log('arrWidth', arrWidth, align, index, letter)
  77. if (code <= 256) {
  78. context.translate(x, y);
  79. // 英文字符,旋转90°
  80. context.rotate(90 * Math.PI / 180);
  81. context.translate(-x, -y);
  82. } else if (index > 0 && text.charCodeAt(index - 1) < 256) {
  83. // y修正
  84. y = y + arrWidth[index - 1] / 2;
  85. }
  86. context.fillText(letter, x, y);
  87. // 旋转坐标系还原成初始态
  88. context.setTransform(1, 0, 0, 1, 0, 0);
  89. // 确定下一个字符的纵坐标位置
  90. var letterWidth = arrWidth[index];
  91. y = y + letterWidth;
  92. });
  93. // 水平垂直对齐方式还原
  94. context.textAlign = align;
  95. context.textBaseline = baseline;
  96. },
  97. saveImage() { //点击保存
  98. var _this = this;
  99. uni.saveImageToPhotosAlbum({
  100. filePath: _this.imgUrl,
  101. success() {
  102. uni.showModal({
  103. title: "保存成功",
  104. content: "图片已成功保存到相册,快去分享到您的圈子吧",
  105. showCancel: false
  106. })
  107. }
  108. })
  109. },
  110. // 将网络图片转为临时图片地址
  111. async getImageInfo({imgSrc}) {
  112. return new Promise((resolve, errs) => {
  113. uni.downloadFile({
  114. src: imgSrc,
  115. success: function(image) {
  116. resolve(image);
  117. },
  118. fail(err) {
  119. errs(err);
  120. }
  121. });
  122. });
  123. },
  124. // 绘制圆形头像
  125. drawCircular(ctx, url, x, y, width, height) {
  126. //画圆形头像
  127. var avatarurl_width = width;
  128. var avatarurl_heigth = height;
  129. var avatarurl_x = x;
  130. var avatarurl_y = y;
  131. ctx.save(); //先保存状态,已便于画完园再用
  132. ctx.beginPath(); //开始绘制
  133. ctx.arc(avatarurl_width / 2 + avatarurl_x, avatarurl_heigth / 2 + avatarurl_y, avatarurl_width / 2, 0, Math
  134. .PI * 2, false);
  135. ctx.setFillStyle("#FFFFFF")
  136. ctx.fill() //保证图片无bug填充
  137. ctx.clip(); //剪切
  138. ctx.drawImage(url, avatarurl_x, avatarurl_y, avatarurl_width, avatarurl_heigth); //推进去图片
  139. ctx.restore();
  140. },
  141. // 绘制矩形图片
  142. drawrectcular(ctx, url, x, y, width, height) {
  143. //画圆形头像
  144. var avatarurl_width = width;
  145. var avatarurl_heigth = height;
  146. var avatarurl_x = x;
  147. var avatarurl_y = y;
  148. ctx.save(); //先保存状态,已便于画完园再用
  149. ctx.beginPath(); //开始绘制
  150. ctx.rect(x, y, width, height);
  151. // ctx.arc(avatarurl_width / 2 + avatarurl_x, avatarurl_heigth / 2 + avatarurl_y, avatarurl_width / 2, 0, Math
  152. // .PI * 2, false);
  153. ctx.setFillStyle("#FFFFFF")
  154. ctx.fill() //保证图片无bug填充
  155. ctx.clip(); //剪切
  156. ctx.drawImage(url, avatarurl_x, avatarurl_y, avatarurl_width, avatarurl_heigth); //推进去图片
  157. ctx.restore();
  158. },
  159. // 绘制带圆角的矩形方法
  160. fillRoundRect(cxt, x, y, width, height, radius, fillColor) {
  161. //圆的直径必然要小于矩形的宽高
  162. if (2 * radius > width || 2 * radius > height) {
  163. return false;
  164. }
  165. cxt.save();
  166. cxt.translate(x, y);
  167. //绘制圆角矩形的各个边
  168. this.drawRoundRectPath(cxt, width, height, radius);
  169. cxt.fillStyle = fillColor || '#fff'; //若是给定了值就用给定的值否则给予默认值
  170. cxt.fill();
  171. cxt.restore();
  172. },
  173. drawRoundRectPath(cxt, width, height, radius) {
  174. cxt.beginPath(0);
  175. //从右下角顺时针绘制,弧度从0到1/2PI
  176. cxt.arc(width - radius, height - radius, radius, 0, Math.PI / 2);
  177. //矩形下边线
  178. cxt.lineTo(radius, height);
  179. //左下角圆弧,弧度从1/2PI到PI
  180. cxt.arc(radius, height - radius, radius, Math.PI / 2, Math.PI);
  181. //矩形左边线
  182. cxt.lineTo(0, radius);
  183. //左上角圆弧,弧度从PI到3/2PI
  184. cxt.arc(radius, radius, radius, Math.PI, (Math.PI * 3) / 2);
  185. //上边线
  186. cxt.lineTo(width - radius, 0);
  187. //右上角圆弧
  188. cxt.arc(width - radius, radius, radius, (Math.PI * 3) / 2, Math.PI * 2);
  189. //右边线
  190. cxt.lineTo(width, height - radius);
  191. cxt.closePath();
  192. }
  193. }
  194. }
  195. </script>
  196. <style lang="scss" scoped>
  197. .canvas-box {
  198. height: 100vh;
  199. display: flex;
  200. align-items: center;
  201. justify-content: center;
  202. background-color: #1ABC9C;
  203. /deep/.nav-box {
  204. width: 100%;
  205. padding: 0 20rpx;
  206. position: absolute;
  207. z-index: 9999;
  208. top: 0;
  209. left: 0;
  210. .title-top {
  211. font-size: 36rpx;
  212. font-weight: 550;
  213. color: #FFFFFF;
  214. display: flex;
  215. justify-content: center;
  216. align-items: center;
  217. position: relative;
  218. margin-bottom: 30rpx;
  219. .title-icon {
  220. position: absolute;
  221. left: 0;
  222. }
  223. }
  224. }
  225. image {
  226. width: 335px;
  227. height: 500px;
  228. }
  229. .footer {
  230. display: flex;
  231. align-items: center;
  232. justify-content: space-between;
  233. position: absolute;
  234. justify-content: center;
  235. padding: 0 40rpx;
  236. width: 100%;
  237. left: 0;
  238. bottom: 10%;
  239. .download {
  240. border: 1rpx solid #ffffff;
  241. color: #ffffff;
  242. display: flex;
  243. align-items: center;
  244. }
  245. view {
  246. padding: 0 20rpx;
  247. height: 78rpx;
  248. text-align: center;
  249. line-height: 78rpx;
  250. font-size: 30rpx;
  251. border-radius: 36rpx;
  252. }
  253. }
  254. }
  255. </style>