canvas遇到的坑

1.文字换行

2.真机不能使用网络数据图片(真坑) 点击显示效果我就不写了,你们可以自己加一下

全部代码贴出来

   css

    #preview {
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.6);
position: fixed;
z-index: 999;
top: 0;
overflow: hidden;
bottom: 0;
} #preview button {
width: 43%;
position: absolute;
bottom: 218rpx;
left: 28%;
z-index: 5;
border-radius: 37rpx;
height: 66rpx;
line-height: 66rpx;
background: #fd5b4c;
} #preview image {
width: 87%;
position: absolute;
top: 8%;
left: 6%;
z-index: 3;
border-radius: 19rpx;
} #preview .hide1 image {
width: 10%;
height: 6%;
position: absolute;
right: 0;
left: 84%;
} #preview text {
position: absolute;
bottom: 170rpx;
left: 25%;
z-index: 5;
font-size: 22rpx;
color: gainsboro;
} canvas {
position: fixed;
top: 0;
left: 10000rpx;
} wxml <canvas canvas-id="shareImg" style="width:545px;height:771px;"></canvas>
<view hidden='{{hidden}}' id='preview' class="hide1{{show1?'':'show'}}">
<image src='{{prurl}}' mode='widthFix'></image>
<button type='primary' size='mini' bindtap='save'>保存分享图</button>
<text>保存后,可以从手机相册分享到朋友圈</text>
<view class="hide1{{show1?'':'show'}}" bindtap='onTap'>
<image src='../../images/Close.png'></image>
</view>
</view> js getData() {
/*商品详情接口数据代码省略*/
//下载图片
var that = this;
wx.downloadFile({ //需要小程序后台添加downloadFile域名
url: res.result.product.images[0].url, //接口返回数据
type: 'image', //类别
success: function (res) {
var _avatarPath = res.tempFilePath
const ctx = wx.createCanvasContext('shareImg');
var imgPath = _avatarPath //商品图
console.log(imgPath)
var bgImgPath = '../../images/qrcode.jpg'; //二维码图
ctx.setFillStyle('white')
ctx.fillRect(0, 0, 6000, 2800);
ctx.drawImage(imgPath, 120, 30, 320, 320);
ctx.drawImage(bgImgPath, 40, 500, 120, 110);
ctx.setFontSize(22)
ctx.beginPath("")
var lineWidth = 0;
var canvasWidth = 300;
var initHeight = 380;
var lastSubStrIndex = 0;
ctx.setFillStyle('black')
var str = that.data.product.title //商品价格名称
ctx.fillText('长按识别图中的小程序码', 190, 540)
ctx.fillText('查看详情', 190, 580)
//文字换行 这个地方有可能报错注释掉就可以
for (let i = 0; i < str.length; i++) {
lineWidth += ctx.measureText(str[i]).width;
if (lineWidth > canvasWidth) {
ctx.fillText(str.substring(lastSubStrIndex, i), 18, initHeight); //绘制截取部分
initHeight += 34; //文字行高
lineWidth = 0;
lastSubStrIndex = i;
}
if (i == str.length - 1) {//绘制剩余部分
ctx.fillText(str.substring(lastSubStrIndex, i + 1), 18, initHeight);
}
}
ctx.setFillStyle('red')
ctx.fillText(that.data.product.price, 460, 380) //that.data.product.price 商品价格数据
ctx.setStrokeStyle('gainsboro')
ctx.strokeRect(20, 450, 510, 0.1)
ctx.stroke()
ctx.draw()
}
})
}

小程序canvasu真机上数据图片不能使用的更多相关文章

  1. 微信小程序开发工具的数据,配置,日志等目录在哪儿? 怎么找?

    原文地址:http://www.wxapp-union.com/portal.php?mod=view&aid=359 本文由本站halfyawn原创:感谢原创者:如有疑问,请在评论内回复   ...

  2. 在微信小程序页面间传递数据总结

    在微信小程序页面间传递数据 原文链接:https://www.jianshu.com/p/dae1bac5fc75 在开发微信小程序过程之中,遇到这么一些需要在微信小程序页面之间进行数据的传递的情况, ...

  3. 微信小程序前台的用户数据入库(后台Laravel)

    首先 我们可以看到微信小程序官方 文档 wx.login   api-login.jpg 通过此图 我们知道 前台要传 一个 code给后台,后台拿到code 并结合appid和appsecret请求 ...

  4. 小程序canvas绘制base64数据格式图片

    翻了微信小程序官方文档,看了看画板drawImage的用法,官方对所要绘制的图片资源路径并没有很详细,模棱两可,没说支持什么格式的路径.今天我就试一下支不支持base64格式的图片 随便找张图片从网上 ...

  5. 微信小程序使用wxParse,解决图片显示路径问题

    我们经常用到发布文章,用的是UEditor百度富文本编辑器,方便排版,存储的也是html代码,这样小程序解析出来的也是排版的样式,但是使用wxParse解析html的时候,因为存储的是图片的相对路径, ...

  6. 【微信小程序】微信小程序wx.previewImage预览图片

    一.小知识 二.例子,配合轮播图使用效果更佳!(如图1) 1.wxml <scroll-view scroll-y="true"> <swiper catchta ...

  7. [转]微信小程序开发(二)图片上传+服务端接收

    本文转自:http://blog.csdn.net/sk719887916/article/details/54312573 文/YXJ 地址:http://blog.csdn.net/sk71988 ...

  8. 微信小程序 setData动态修改数据数组的值

    1.问题说明 有一组数据,用来存储图片路径,动态修改图片的路径来上传图片,而小程序JS只能通过事件获取时机和setData方法修改数据来改变view. 而用这样写的方式明显是错误的 2.解决办法 字符 ...

  9. 小程序editor篇-基本使用图片上传

    今天小程序项目内,要弄一个editor,富文本编辑功能,支持图文并茂,前几天刚好看了小程序的demo应用,刚好看到editor这个东东,那就安排! 官网示例git地址 大概看了下文档,拉下官方示例,看 ...

随机推荐

  1. 《剑指offer》— JavaScript(9)变态跳台阶

    变态跳台阶 题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级--它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 实现代码 function jumpFloor(number) { ...

  2. 机器学习算法的Python实现 (1):logistics回归 与 线性判别分析(LDA)

    先收藏............ 本文为笔者在学习周志华老师的机器学习教材后,写的课后习题的的编程题.之前放在答案的博文中,现在重新进行整理,将需要实现代码的部分单独拿出来,慢慢积累.希望能写一个机器学 ...

  3. 简化版SMO算法标注

    ''' 随机选择随机数,不等于J ''' def selectJrand(i,m): j=i #we want to select any J not equal to i while (j==i): ...

  4. 03-树2. List Leaves (25) 二叉树的层序遍历

    03-树2. List Leaves (25) 题目来源:http://www.patest.cn/contests/mooc-ds/03-%E6%A0%912 Given a tree, you a ...

  5. 「Linux」centos7安装mysql

    1.yum仓库下载MySQL:sudo yum localinstall https://repo.mysql.com//mysql80-community-release-el7-1.noarch. ...

  6. 1.4 Matplotlib:绘图

    sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campai ...

  7. OpenCV---轮廓发现

    推文:OpenCV-Python教程(11.轮廓检测) 轮廓发现 是基于图像边缘提取的基础,寻找对象轮廓的方法,所以边缘提取的阈值选定会影响最终轮廓的发现 相关API findContours 发现轮 ...

  8. python---基础知识回顾(四)(模块sys,os,random,hashlib,re,序列化json和pickle,xml,shutil,configparser,logging,datetime和time,其他)

    前提:dir,__all__,help,__doc__,__file__ dir:可以用来查看模块中的所有特性(函数,类,变量等) >>> import copy >>& ...

  9. [JSOI2010]Group

    1821: [JSOI2010]Group 部落划分 Group Time Limit: 10 Sec  Memory Limit: 64 MB[Submit][Status][Discuss] De ...

  10. 你必须了解Spring的生态

    Spring不止是提供了IOC.AOP的功能,还提供了大量的基于Spring的项目,拿来用就行了,用于一站式开发,大大降低了开发的难度. 下面列举下主要的一些Spring的生态项目: Spring B ...