• js实现文字头像的生成


    原文地址:https://www.phyer.cn/article/9277。欢迎大家访问我的博客(●ˇ∀ˇ●)

    使用canvas画出文字就好啦

    function gen_text_img(size, s) {
        let colors = [
            "rgb(239,150,26)", 'rgb(255,58,201)', "rgb(111,75,255)", "rgb(36,174,34)", "rgb(80,80,80)"
        ];
        let cvs = document.createElement("canvas");
        cvs.setAttribute('width', size[0]);
        cvs.setAttribute('height', size[1]);
        let ctx = cvs.getContext("2d");
        ctx.fillStyle = colors[Math.floor(Math.random()*(colors.length))];
        ctx.fillRect(0, 0, size[0], size[1]);
        ctx.fillStyle = 'rgb(255,255,255)';
        ctx.font = size[0]*0.6+"px Arial";
        ctx.textBaseline = "middle";
        ctx.textAlign = "center";
        ctx.fillText(s,size[0]/2,size[1]/2);
    
        return  cvs.toDataURL('image/jpeg', 1);
    }

    效果如下某些浏览器文字不能水平居中,暂时没有找到解决办法:

  • 相关阅读:
    Add Two Numbers
    Reverse Linked List II
    Reverse Linked List
    Remove Duplicates from Sorted List
    Remove Duplicates from Sorted List II
    Partition List
    Intersection of Two Linked Lists
    4Sum
    3Sum
    2Sum
  • 原文地址:https://www.cnblogs.com/yunyuyuan/p/12432800.html
Copyright © 2020-2023  润新知