小程序问题

1.获取节点
wx.createSelectorQuery()
wx.createSelectorQuery().in(this) //组件中加in(this),不然获取不到

2.使用实例

wx.createSelectorQuery().in(this)
      .select('#share')
      .fields({
        node: true,
        size: true
      })
      .exec(async (res) => {
      		const canvas = res[0].node;
        // Canvas 绘制上下文
        const ctx = canvas.getContext('2d');

      //  let dpr = this.data.dpr
        //画布大小根据屏幕分辨率进行缩放,防止模糊
        const renderWidth = res[0].width
        const renderHeight = res[0].height


        // 初始化画布大熊啊
       //  const dpr = wx.getSystemInfoSync().pixelRatio
        canvas.width = renderWidth*ratio
        canvas.height = renderHeight*ratio
        ctx.scale(ratio, ratio)
        //画布背景色
        ctx.fillStyle="#ffffff"
        ctx.fillRect(0,0,canvas.width, canvas.height)
        //画图片
        const image = canvas.createImage()
        image.crossOrigin = "Anonymous" // 亚跨域使用
        image.src = imageCover
        image.onload = ()=>{
        	//计算裁剪图片
          // const imageWidth = image.width
          // const imageHeight = image.height
          // const cropRatio = 3/4
          // const cropWidth = imageWidth * cropRatio
          // const cropHeight = imageHeight
          // 计算裁剪的起始位置,这里以居中为例
          // const startX = (imageWidth - cropWidth) / 2
          // const startY = (imageHeight - cropHeight) / 2
          // ctx.drawImage(image, startX, startY, cropWidth, cropHeight, 0, 0, 300, 400)
          ctx.drawImage(image, 0, 0, 300, 400);
        }
      })

填充文字样式

 ctx.font= '600 12px PingFang TC'
 ctx.fillStyle = '#ffffff'
 ctx.fillText('你好',231,362)

填充文字边框样式

 ctx.font= '600 12px PingFang TC'
 ctx.fillStyle = '#ffffff'
 ctx.strokeStyle = "#000000";
 ctx.fillText('你好',231,362)
 ctx.strokeText('你好',231,362);

计算文字宽度
var metrics = c

ctx.measureText(testLine).width;

3.css文字描边:
-webkit-text-stroke: 1px red; 会使文字越来越瘦,占文字本身的宽度,黑色字体样式会减少在这里插入图片描述

可以使用
data里面设置 textValue: ‘愉快’
在这里插入图片描述

<view class="title-greet" data-color="#0C20E9" data-attr-greet="{{textValue}}">{{textValue}}</view>
.title-greet{
  -webkit-text-stroke-width: 4px;  
  -webkit-text-stroke-color: #fff; 
  -webkit-text-stroke: 4px #fff; 
  position: relative;
  z-index: 1;
}
.title-greet:after {
  position: absolute;
  z-index: 2;
  left: 0;
  right: 0;
  top: 0;
  -webkit-text-stroke-width: 0px;
  content: attr(data-attr-greet);
  color: attr(data-color);
}

相关推荐

  1. 微信程序渲染有问题

    2024-07-11 16:42:04       31 阅读
  2. 解决程序的异步请求问题

    2024-07-11 16:42:04       25 阅读
  3. uniapp程序项目解决键盘问题

    2024-07-11 16:42:04       22 阅读
  4. 程序面试问答(解决方案)

    2024-07-11 16:42:04       36 阅读
  5. 程序分享携带参数,被覆盖问题

    2024-07-11 16:42:04       52 阅读
  6. 微信程序:selectComponent返回null的问题

    2024-07-11 16:42:04       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 16:42:04       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 16:42:04       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 16:42:04       58 阅读
  4. Python语言-面向对象

    2024-07-11 16:42:04       69 阅读

热门阅读

  1. 07-7.4.1 B树

    2024-07-11 16:42:04       23 阅读
  2. Jupyter Notebook简介

    2024-07-11 16:42:04       21 阅读
  3. 面向对象编程基本特征--封装 继承 多态

    2024-07-11 16:42:04       22 阅读
  4. 单机版k8s搭建

    2024-07-11 16:42:04       24 阅读
  5. k8s资源管理中request和limit的区别

    2024-07-11 16:42:04       24 阅读
  6. 软设之UML中的关系

    2024-07-11 16:42:04       20 阅读
  7. 编程语言在医疗健康领域的创新应用

    2024-07-11 16:42:04       21 阅读
  8. lvs三种模式

    2024-07-11 16:42:04       24 阅读
  9. 电商商城网站防护选购指南,高防CDN使用攻略

    2024-07-11 16:42:04       26 阅读
  10. [题解]P1113 杂务||拓扑排序板子题,但是dp求解

    2024-07-11 16:42:04       22 阅读
  11. PgMP考试报名攻略,不会的看这里!

    2024-07-11 16:42:04       24 阅读
  12. 高效利用iCloud指南

    2024-07-11 16:42:04       21 阅读