编程 js使用canvas进行图片文字合成技术

2024-11-19 09:07:51 +0800 CST views 2733

废话不多说 直接看效果图

canves.jpg

完整代码

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>个人诚信证书</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
        body{
            padding: 0;   
            margin: 0;    
            width:100%;
            display:flex;
            align-items:center;
            justify-content: center;
            height: 100vh;
            
        }
        img{            
            width:100%;
            height: 100%;
        }
        @font-face {
            font-family: 'fzdbs';            
            src: url('/static/img/certificate/fzdbs.ttf') format('truetype');
            font-weight: normal;
            font-style: normal;
        }
        @font-face {
            font-family: 'fz';            
            src: url('/static/img/certificate/fz.ttf') format('truetype');
            font-weight: normal;
            font-style: normal;
        }
    </style>
    <script src="/static/js/jquery-1.8.3.min.js"></script>
</head>
<body >
    <h1>正在为您载入证书...</h1>
    <div class="main"></div>
<script>
    let height = $(window).height(),
        width = $(window).width(),
        images=[
                {
                    "type":"img",
                    "url":"/static/img/certificate/bg.jpg",
                    "location":{
                        x:0,
                        y:0,
                        w:600,
                        h:755
                    }
                },
                {
                    "type":"img",
                    "url":"/attachment/users/share/qrcode/2018/10/17/1539738665.png",
                    "location":{
                        x:252,
                        y:337,
                        w:91,
                        h:91
                    }
                },
                {
                    "type":"img",
                    "url":"/static/img/certificate/qy.png",
                    "location":{
                        x:221,
                        y:152,
                        w:171,
                        h:37
                    }
                },
                {
                    "type":"text",
                    "text":"授予:{$db['realname']}",
                    "font":"26px fzdbs",
                    "color":"#282828",
                    "location":{
                        x:163,
                        y:231,
                    }
                },
                {
                    "type":"text",
                    "text":"诚信守护者",
                    "font":"50px fz",
                    "color":"#b62f33",
                    "location":{
                        x:178,
                        y:300,
                    }
                },
                {
                    "type":"text",
                    "text":"人人参与 诚信建设",
                    "font":"22px fzdbs",
                    "color":"#282828",
                    "location":{
                        x:213,
                        y:460,
                    }
                }
                ,
                {
                    "type":"text",
                    "text":"共同守护 信用社会",
                    "font":"22px fzdbs",
                    "color":"#282828",
                    "location":{
                        x:213,
                        y:500,
                    }
                }
                ,
                {
                    "type":"text",
                    "text":"福建期望管家信用管理有限公司",
                    "font":"24px fzdbs",
                    "color":"#282828",
                    "location":{
                        x:148,
                        y:575,
                    }
                }
                ,
                {
                    "type":"text",
                    "text":"{$db['year']}",
                    "font":"24px fzdbs",
                    "color":"#282828",
                    "location":{
                        x:295,
                        y:615,
                    }
                },
                {
                    "type":"img",
                    "url":"/static/img/certificate/gs.png",
                    "location":{
                        x:340,
                        y:520,
                        w:121,
                        h:117
                    }
                },
        ];
    var c=document.createElement('canvas'),
        len = images.length;
        ctx=c.getContext('2d');
    c.width=600;
    c.height=775;
    ctx.rect(0,0,c.width,c.height);
    ctx.fillStyle= 'transparent';
    ctx.fill();
    function drawing(n){
        if (n<len) {
            let db = images[n];
            if (db.type == "img"){
                var img = new Image;
                img.src = db.url;
                img.onload = function(){
                    ctx.drawImage(img,db.location.x,db.location.y,db.location.w,db.location.h);
                    drawing(n+1);                              
                }
            }else if (db.type == "text"){
                ctx.font = db.font;
                ctx.fillStyle = db.color;
                ctx.fillText(db.text,db.location.x,db.location.y);
                drawing(n+1);
            }
            
        }else{
            convertCanvasToImage(c);
        }
        
        
    }
    drawing(0);
    function convertCanvasToImage(canvas) {
            var hc_image = new Image();
            hc_image.src = canvas.toDataURL("image/png");
            $("h1").hide();
            $('.main').html(hc_image);
    }  
</script>
</body>
</html>

file.png

复制全文 生成海报 js

推荐文章

Python设计模式之工厂模式详解
2024-11-19 09:36:23 +0800 CST
智慧加水系统
2024-11-19 06:33:36 +0800 CST
Linux查看系统配置常用命令
2024-11-17 18:20:42 +0800 CST
thinkphp swoole websocket 结合的demo
2024-11-18 10:18:17 +0800 CST
Golang Sync.Once 使用与原理
2024-11-17 03:53:42 +0800 CST
Vue3中的Scoped Slots有什么改变?
2024-11-17 13:50:01 +0800 CST
网络数据抓取神器 Pipet
2024-11-19 05:43:20 +0800 CST
H5抖音商城小黄车购物系统
2024-11-19 08:04:29 +0800 CST
宝塔面板 Nginx 服务管理命令
2024-11-18 17:26:26 +0800 CST
智能视频墙
2025-02-22 11:21:29 +0800 CST
浏览器自动播放策略
2024-11-19 08:54:41 +0800 CST
js一键生成随机颜色:randomColor
2024-11-18 10:13:44 +0800 CST
GROMACS:一个美轮美奂的C++库
2024-11-18 19:43:29 +0800 CST
淘宝npm镜像使用方法
2024-11-18 23:50:48 +0800 CST
JavaScript中的常用浏览器API
2024-11-18 23:23:16 +0800 CST
mysql 计算附近的人
2024-11-18 13:51:11 +0800 CST
Elasticsearch 的索引操作
2024-11-19 03:41:41 +0800 CST
地图标注管理系统
2024-11-19 09:14:52 +0800 CST
如何在Rust中使用UUID?
2024-11-19 06:10:59 +0800 CST
HTML和CSS创建的弹性菜单
2024-11-19 10:09:04 +0800 CST
Vue中的异步更新是如何实现的?
2024-11-18 19:24:29 +0800 CST
MySQL数据库的36条军规
2024-11-18 16:46:25 +0800 CST
MySQL 优化利剑 EXPLAIN
2024-11-19 00:43:21 +0800 CST
程序员茄子在线接单