当前位置:   article > 正文

微信小程序 Canvas 画板使用,踩坑记录_canvas.width = res[0].width canvas.height = res[0]

canvas.width = res[0].width canvas.height = res[0].height

微信小程序 Canvas 2dAPI的绘图使用

Html

  1. <!-- canvas 画板 -->
  2. <view :class="showCanvasCSS" >
  3. <canvas
  4. type="2d"
  5. id="canvas"
  6. class='canvas-style'
  7. />
  8. <view class="linkcopy">
  9. <u-button class="linkcopyBtn" icon="download" type="info" @click="CloseCanvas">取消</u-button>
  10. <u-button class="linkcopyBtn" icon="download" type="primary" @click="uploads">保存图片</u-button>
  11. </view>
  12. </view>

CSS

  1. .canvas-style{
  2. margin: 0 auto;
  3. width: 100%;
  4. height: 990rpx;
  5. // border: 1px solid black;
  6. }
  7. .container{
  8. z-index: 3;
  9. position: fixed;
  10. top:150rpx;
  11. width: 100%;
  12. height: 1100rpx;
  13. // border: 1px solid red;
  14. }
  15. .container2{
  16. position: fixed;
  17. left:9950rpx;
  18. width: 100%;
  19. height: 1100rpx;
  20. // border: 1px solid red;
  21. }
  22. .linkcopy{
  23. width: 100%;
  24. }
  25. .linkcopyBtn{
  26. display: inline-block;
  27. float: left;
  28. margin-top: 25rpx;
  29. margin-left: 25rpx;
  30. width: 45%;
  31. height: 350rpx;
  32. }

js

  1. //初始化画板 - 微信小程序 canvas 2D
  2. drawing(){
  3. //定义全局canvas
  4. const query = wx.createSelectorQuery();
  5. query
  6. .select('#canvas')
  7. .fields({ node: true, size: true })
  8. .exec((res) => {
  9. const canvas = res[0].node;
  10. this.canvasObj=canvas;
  11. const ctx = canvas.getContext('2d');
  12. const dpr = wx.getSystemInfoSync().pixelRatio;
  13. canvas.width = res[0].width * dpr;
  14. canvas.height = res[0].height * dpr;
  15. this.canvasWidth=res[0].width * dpr;
  16. this.canvasHeight = res[0].height * dpr;
  17. ctx.scale(dpr, dpr);
  18. // 填充一个白色矩形
  19. ctx.fillStyle ='white';
  20. ctx.fillRect(8, 55, 345,425);
  21. //头部图片
  22. const headerImg = canvas.createImage();
  23. headerImg.src = this.TopList.product_image_url;
  24. headerImg.onload = () => {
  25. ctx.drawImage(headerImg, 83, 65, 200, 186);
  26. };
  27. // 标题
  28. ctx.fillStyle ='#333333';
  29. ctx.font = 'normal bold 15px sans-serif';
  30. ctx.fillText(this.TopList.brand_name+" | "+this.TopList.gpc_product.FNAME+" | "+this.TopList.gpc_product.FSPECIFICATION, 20, 270);
  31. // 二维码
  32. const QRcodeImg = canvas.createImage();
  33. QRcodeImg.src = this.QRcodeImg;
  34. QRcodeImg.onload = () => {
  35. ctx.drawImage(QRcodeImg, 215, 345, 124.5, 130);
  36. };
  37. //logo
  38. const logoImg = canvas.createImage();
  39. logoImg.src = this.logoImg;
  40. logoImg.onload = () => {
  41. ctx.drawImage(logoImg, 25, 345, 180, 80);
  42. };
  43. })
  44. },

截图保存分享

  1. //下载画板生成图片
  2. uploads() {
  3. var that = this;
  4. var timer = setTimeout(function(){
  5. wx.canvasToTempFilePath({
  6. x: 8,
  7. y: 55,
  8. width: 345,
  9. height: 425,
  10. destWidth: this.canvasWidth,
  11. destHeight: this.canvasHeight,
  12. canvas: that.canvasObj,
  13. success(res) {
  14. wx.saveImageToPhotosAlbum({
  15. filePath: res.tempFilePath,
  16. success: function (data) {
  17. wx.showToast({
  18. title: '保存成功,快去分享吧',
  19. icon: 'none',
  20. duration: 2000
  21. });
  22. that.showCanvas=false;
  23. that.showCanvasCSS="container2";
  24. clearTimeout(timer);
  25. },
  26. fail: function (error) {
  27. console.log(error)
  28. wx.showToast({
  29. title: '很遗憾,保存失败',
  30. icon: 'none',
  31. duration: 2000
  32. })
  33. clearTimeout(timer)
  34. }
  35. })
  36. }
  37. })
  38. },1000)
  39. }

注:需要注意的是,对于Canvas画板需要隐藏/显示,运用wx:if 、v-if、v-show、hidden都不可行,解决方案为将canvas设置定位,比如,然后指定left一个比较大的值,让canvas的显示超出屏幕范围。

  <canvas canvas-id='canOne' class='canOne' style='width:1080px;height:1643px;position:fixed;left:9000px;'></canvas>

微信小程序 Canvas 旧版API的绘图使用 鉴转载:
 

https://blog.csdn.net/javascript411/article/details/95466716

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/955077
推荐阅读
相关标签
  

闽ICP备14008679号