JS中图片生成水印,使用canvas绘图

JS中图片生成水印,使用canvas绘图

通过js赋值到页面上,可以根据canvas进行绘图,在这里需要注意的是,要计算好图片的尺寸,不然图片会失真(模糊),下面展示的 代码片.


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <style>
        .str {
            font: normal bold 5px Arial;
            /*            font-size: 5px;*/
            color: rgb(224, 226, 226,0.4);
            position: absolute;
            padding-left: 16px;
            padding-top: 53px;
            display: none;
        }

        .str2 {
            font: normal bold 5px Arial;
            /* font-size: 5px; */
            color: rgb(224, 226, 226,0.4);
            position: absolute;
            padding-left: 66px;
            padding-top: 130px;
            display: none;
        }
    </style>
</head>
<body>
    <div class="str">HOMEANGEL</div>
    <div class="str2">HOMEANGEL</div>
    <img src="https://dss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3252521864,872614242&fm=26&gp=0.jpg" style="width:100px;height:100px;" />
    <div class="layui-input-inline" id="picture" style="width:100%;margin-left: 60px;">

    </div>

    <script>
        $(function () {
            var img = ["https://dss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3252521864,872614242&fm=26&gp=0.jpg"];
            GetCanvas(img);
        })
        function AddCanvas(src, ById) {
            var img = new Image();
            img.src = src
            img.onload = function () {
                imgW = img.width;
                imgH = img.height;
                //准备canvas环境
                var canvas = document.getElementById(ById);
                canvas.width = img.width;
                canvas.height = img.height;
                var ctx = canvas.getContext("2d");
                // 绘制图片
                ctx.drawImage(img, 0, 0, img.width, img.height);
                // 绘制水印
                ctx.font = "20px bold Arial";
                ctx.fillStyle = "rgb(224, 226, 226,0.6)";//这里是颜色
                ctx.fillText("watermark  watermark", 20, 60);
                ctx.fillText("watermark  watermark", 120, 160);
                ctx.fillText("watermark  watermark", 220, 220);
                //ctx.fillText("HOMEANGEL", 90, 130);
                canvas.style.width = "100px";
                canvas.style.height = "100px";
            }
        }
        function GetCanvas(Strhtml) {
            if (Strhtml.length > 0 && Strhtml != "") {
                var html = "";
                for (var i = 0; i < Strhtml.length; i++) {
                    var str = "sample" + (i + 1);
                    html += ' <canvas   id=' + str + ' name="test" src=' + Strhtml[i] + '  "></canvas>';
                }
                $("#picture").html(html);//这里图片添加到html,然后for,添加水印
            }
            for (var i = 0; i < Strimg.length; i++) {
                var str = "sample" + (i + 1);
                var src = Strhtml[i];
                AddCanvas(src, str);//id没传递
            }
        }

    </script>
</body>

总结

在canvas中,使用到的绘图,还有许多功能等着我们去学习。一起来学习吧!!!
转载请标明去初。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值