轻源码

  • QingYuanMa.com
  • 全球最大的互联网技术和资源下载平台
搜索
一起源码网 门户 初级进阶 查看主题

微信小程序使用canvas进行二维码绘制

发布者: 小王 | 发布时间: 2018-1-4 05:30| 查看数: 2343| 评论数: 1|帖子模式


var canvas = {
    width: 100,
    height:36
};
function verification(ctx) {

    // //清空画布
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    // //生成随机颜色
    function getRandomColor() {
        return "#" + ("00000" + ((Math.random() * 16777215 + 0.5) >> 0).toString(16)).slice(-6);
    }
    // //定义线性渐变
    var gradient = ctx.createLinearGradient(0, 0, canvas.width, canvas.height);
    gradient.addColorStop("0", getRandomColor());
    gradient.addColorStop("0.5", getRandomColor());
    gradient.addColorStop("1.0", getRandomColor());

    // //生成随机数
    function rnd(min, max) {
        return min + Math.floor(Math.random() * (max - min + 1));
    }

    // //绘制干扰线条
    function line() {
        ctx.beginPath();
        ctx.moveTo(rnd(0, canvas.width), rnd(0, canvas.height));
        ctx.lineTo(rnd(0, canvas.width), rnd(0, canvas.height));
        ctx.closePath();
        ctx.lineWidth = rnd(1, 3);
        ctx.setFillStyle(gradient)
        ctx.stroke();
    }

    // //绘制干扰点
    function point() {
        ctx.fillRect(rnd(0, canvas.width), rnd(0, canvas.height), 2, 2);
    }

    // //绘制验证码
    var text = rnd(1000, 9999);
    ctx.setFontSize(30)
    ctx.setFillStyle(gradient)
    ctx.fillText(text,2, 25);

    // //生成干扰元素
    for (var i = 0; i < 8; i++) {
        line();
    }
    for (var i = 0; i <100; i++) {
        point();
    }
    ctx.draw()
    return text;
}
module.exports = { verification: verification };


上面直接放在一个js中吧方法暴露出来就可以了,顺便说一下关于 样式的问题,因为没必要再去写一篇了,样式:padding ,
以前在html页面上我有时候习惯padding:0px,这样设置为0,但是在小程序中写成padding:0rpx,仍会有内边距存在,写成padding:0,不要带单位就

最新评论

郑林 发表于 2022-5-7 21:21
源代码2高清免费观看

轻源码让程序更轻更快

QingYuanMa.com

工作时间 周一至周六 8:00-17:30

侵权处理

客服QQ点击咨询

关注抖音号

定期抽VIP

Copyright © 2016-2021 https://www.171739.xyz/ 滇ICP备13200218号

快速回复 返回顶部 返回列表