码迷,mamicode.com
首页 > 其他好文 > 详细

H5页面背景添加水印

时间:2019-02-28 16:42:56      阅读:898      评论:0      收藏:0      [点我收藏+]

标签:back   fun   turn   charset   ext   rip   latex   ase   rect   

HTML代码

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>
        </style>
    </head>
    <body>
        <script type="text/javascript" src="js/index.js" ></script>
    </body>
</html>

Js代码

(function waterMark(){
    function textBecomeImg(text,fontsize,fontcolor){
        var canvas = document.createElement(‘canvas‘);
        $buHeight = 0;
        if(fontsize <= 32){ $buHeight = 99; }
        else if(fontsize > 32 && fontsize <= 60 ){ $buHeight = 2;}
        else if(fontsize > 60 && fontsize <= 80 ){ $buHeight = 4;}
        else if(fontsize > 80 && fontsize <= 100 ){ $buHeight = 6;}
        else if(fontsize > 100 ){ $buHeight = 10;}
        canvas.height=fontsize + $buHeight ;
        canvas.padding=30;
        var context = canvas.getContext(‘2d‘);
        context.clearRect(0, 0, canvas.width*2, canvas.height);
        context.fillStyle = fontcolor;
        context.font=fontsize+"px Arial";
        context.textAlign = "center";
        context.textBaseline = ‘middle‘; 
        context.fillText(text,0,fontsize/2);
        var canvasWidth = canvas.width/99;
        canvasWidth = context.measureText(text).width;
        canvas.width = 450;
        canvas.height = 200;
        context.fillStyle = fontcolor;
        context.font=fontsize+"px Arial";
        context.textBaseline = ‘middle‘; 
        context.fillText(text,0,fontsize/2);
        var dataUrl = canvas.toDataURL(‘image/png‘);
        return dataUrl;
    }
    var text = new Date()+‘111‘;
    var shuiyinDiv = document.createElement(‘div‘);
    var style = shuiyinDiv.style;
    style.position = ‘fixed‘;
    style.left = 0;
    style.top = ‘-10%‘;
    style.width = ‘200%‘;
    style.height = ‘200%‘;
    style.opacity = ‘0.1‘;
    style.background = "url("+textBecomeImg(text,22,"gray")+")";
    style.zIndex = 9999999991;
    style.transform = "translateX(-100px) rotate(-30deg)";
    style.pointerEvents = "none";
    document.body.appendChild(shuiyinDiv);
})();

 

H5页面背景添加水印

标签:back   fun   turn   charset   ext   rip   latex   ase   rect   

原文地址:https://www.cnblogs.com/xiaohualu/p/10451213.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!