小程序canvas多行文字

news/2024/7/20 1:15:11 标签: 小程序, canvas, 多行文字, 自动换行

效果:

使用:

var that = this;

const ctx = wx.createCanvasContext('savePic');

var colorText = '肤色晶莹白皙,肤质细腻有透明感,适合搭配一些颜色明亮、轻快一点的色彩,营造轻盈感。';

that.draw_long_text(colorText, ctx, 46, 1271);

 

封装:

//canvas文字自动换行封装

/*

longtext 文案

cxt 对应的canvas

begin_width 开始位置的X坐标

begin_height 开始位置的Y坐标

*/

draw_long_text: function (longtext, cxt, begin_width, begin_height) {

var linelenght = 45;//行高

var text = "";

var count = 0;

var begin_width = begin_width;

var begin_height = begin_height;

var stringLenght = longtext.length;

var newtext = longtext.split("");

var context = cxt;

context.textAlign = 'left';

for (var i = 0; i <= stringLenght; i++) {

if (count == 22) {//count一行显示多少个字

context.fillText(text, begin_width, begin_height);

begin_height = begin_height + linelenght;

text = "";

count = 0;

}

if (i == stringLenght) {

context.fillText(text, begin_width, begin_height);

}

var text = text + newtext[0];

count++;

newtext.shift();

}

},


http://www.niftyadmin.cn/n/797854.html

相关文章

MEF 基础简介 一

前言 小编菜鸟级别的程序员最近感慨颇多&#xff0c;经历了三五春秋深知程序路途遥远而我沧海一粟看不到的尽头到不了的终点何处是我停留的驿站。说了段废话下面进入正题吧&#xff01; 什么是MEF&#xff1f; MEF&#xff1a;全称Managed Extensibility Framework&#xff08;…

pc和移动端图片裁剪插件PhotoClip的踩坑

遇到的问题有&#xff0c;苹果手机上传的图片会自动旋转&#xff0c;安卓手机上要裁剪图片的无法进行缩放&#xff08;github上查到原因iscroll插件的bug&#xff0c;替换到2.X版本可修复&#xff09;&#xff0c;已填坑 文章参考https://www.npmjs.com/package/photoclip 文…

Shellinabox on centos6.9

介绍 一款实用的web linux终端, 并且保证操作安全性&#xff08;屏蔽root用户&#xff09; 下面以centos6.9为例 安装 首先安装epel仓库,再安装shellinabox yum -y install epel-release yum -y install shellinabox 配置端口 编辑/etc/sysconfig/shellinaboxd, 修改端口号 # S…

折腾一天终于尝到了 signalr core了

这是服务端的消息 这是客户端的消息&#xff0c;可以看到 无论那边调用 的都同样显示了。 之前一个问题折腾了 大半天要贴出来给大家提个醒 using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNe…

监听url变化

js部分&#xff1a; <script> var lasturlwindow.location.href; function isHashChanged(){ var cururlwindow.location.href; if(lasturl cururl ){ return false; } return true; } function hashChangeFire(){ console.info("变化了"…

小程序input输入框键盘弹出使得布局上移问题

注意点&#xff1a; 1.stylebottom:{{inputBottom}}px单位用px 2.input的 adjust-position{{false}}要用false html部分&#xff1a; <!-- start 输入框 --> <view classinputBottom stylebottom:{{inputBottom}}px> <input typetext classtextInput valu…

Cassandra 数据模型

Cassandra的数据模型类似于关系型数据库的模型&#xff0c;且提供了与SQL语言非常类似的CQL语言进行操作。 但是Cassandra的数据模型类似于多层键值对结构&#xff0c;与关系型数据库存在巨大差别。 本文基于: [cqlsh 5.0.1 | Cassandra 3.11.2 | CQL spec 3.4.4 | Native prot…

layui排序后table样式丢失

table.on(sort(goods-table2), function(obj){ //注&#xff1a;tool是工具条事件名&#xff0c;goods-table2是table原始容器的属性 lay-filter"对应的值" $(".layui-table-main .layui-table tbody tr td").css("height","120px&…