uniapp好用的canvas插件分享

canvasUtils.js

/**
 * 绘制矩形
 * 参数:cxt、x坐标、y坐标、宽度、高度、圆角、颜色
 */
function fillRoundRect(cxt, x, y, width, height, radius, fillColor) {
   
    console.log(cxt,'fillRoundRect')
    //圆的直径必然要小于矩形的宽高
    if (2 * radius > width || 2 * radius > height) {
   
        return false;
    }
    cxt.save();
    cxt.translate(x, y);
    //绘制圆角矩形的各个边
    drawRoundRectPath(cxt, width, height, radius);
    cxt.fillStyle = fillColor || '#000'; //若是给定了值就用给定的值否则给予默认值
    cxt.fill();
    cxt.restore();
}
// 绘制矩形各个边过程
function drawRoundRectPath(cxt, width, height, radius) {
   
    cxt.beginPath(0);
    //从右下角顺时针绘制,弧度从0到1/2PI
    cxt.arc(width - radius, height - radius, radius, 0, Math.PI / 2);
    //矩形下边线
    cxt.lineTo(radius, height);
    //左下角圆弧,弧度从1/2PI到PI
    cxt.arc(radius, height - radius, radius, Math.PI / 2, Math.PI);
    //矩形左边线
    cxt.lineTo(0, radius);
    //左上角圆弧,弧度从PI到3/2PI
    cxt.arc(radius, radius, radius, Math.PI, (Math.PI * 3) / 2);
    //上边线
    cxt.lineTo(width - radius, 0);
    //右上角圆弧
    cxt.arc(width - radius, radius, radius, (Math.PI * 3) / 2, Math.PI * 2);
    //右边线
    cxt.lineTo(width, height - radius);
    cxt.closePath();
}
/**
 * 绘制矩形边框
 * 参数:cxt、x坐标、y坐标、宽度、高度、圆角、border颜色
 */
function roundRectBorder(cxt, x, y, w, h, r,borderWidth) {
   
    if (w < 2 * r) r = w / 2;
    if (h < 2 * r) r = h / 2;
    cxt.beginPath();
    cxt.moveTo(x+r, y);
    cxt.arcTo(x+w, y, x+w, y+h, r);
    cxt.arcTo(x+w, y+h, x, y+h, r);
    cxt.arcTo(x, y+h, x, y, r);
    cxt.arcTo(x, y, x+w, y, r);
    cxt.closePath();
    cxt.lineWidth = borderWidth
}
//加载图片
function getImageInfo(image) {
   
    return new Promise((req, rj) => {
   
        // #ifndef APP-PLUS
        uni.getImageInfo({
   
            src: image,
            success: function(res) {
   
                req(res)
            },
        });
        // #endif
        // #ifdef APP-PLUS
        if(uni.getSystemInfoSync().platform == 'ios'){
   
            uni.downloadFile({
   
        
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
1. 安装wxml2canvas插件uniapp项目中的HBuilderX编辑器中,打开插件市场,搜索wxml2canvas插件并安装。 2. 引入wxml2canvas插件 在需要使用wxml2canvas插件的页面中,引入wxml2canvas插件: ```javascript import wxml2canvas from '@/wxml2canvas/wxml2canvas.js'; ``` 3. 绘制圆角图片 在页面中,使用wxml2canvas插件的draw函数绘制圆角图片: ```javascript draw() { let that = this; let query = uni.createSelectorQuery().in(this); query.select('#myCanvas').fields({ node: true, size: true }).exec((res) => { const canvas = res[0].node; const ctx = canvas.getContext('2d'); const dpr = uni.getSystemInfoSync().pixelRatio; canvas.width = res[0].width * dpr; canvas.height = res[0].height * dpr; ctx.scale(dpr, dpr); const image = canvas.createImage(); image.src = that.data.imageUrl; image.onload = () => { // 绘制圆角图片 that.drawRoundImage(ctx, image, 0, 0, res[0].width, res[0].height, 10); }; }); }, drawRoundImage(ctx, img, x, y, w, h, r) { ctx.save(); ctx.beginPath(); // 绘制圆角矩形 ctx.arc(x + r, y + r, r, Math.PI, Math.PI * 1.5); ctx.lineTo(x + w - r, y); ctx.arc(x + w - r, y + r, r, Math.PI * 1.5, Math.PI * 2); ctx.lineTo(x + w, y + h - r); ctx.arc(x + w - r, y + h - r, r, 0, Math.PI * 0.5); ctx.lineTo(x + r, y + h); ctx.arc(x + r, y + h - r, r, Math.PI * 0.5, Math.PI); ctx.closePath(); ctx.clip(); // 绘制图片 ctx.drawImage(img, x, y, w, h); ctx.restore(); } ``` 4. 在页面中使用canvas标签 在页面中添加canvas标签,并赋予一个id值: ```html <canvas id="myCanvas" style="width: 100%; height: 300rpx;"></canvas> ``` 5. 调用draw函数 在页面的onLoad函数中,调用draw函数绘制圆角图片: ```javascript onLoad() { this.draw(); }, ``` 以上就是使用wxml2canvas插件uniapp中绘制圆角图片的完整流程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这次最后一次熬夜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值