HTML Table元素加水印(超出范围不显示)

 

首先说明:有三个文件:1个H5,一个js(代码较长),一个水印js插件(地址:https://code.jquery.com/jquery-git.js

所有代码已经上传到github:作者水印github位置

备注:经过测试,该效果在IE7.8.9版本下无法显示水印,其他浏览器版本都可以


网页效果图:

HTML代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
</head>
<body>
<table  border="0" width="800" height="800" align="center" cellpadding="0" cellspacing="0" id="table1" bgcolor="#008b8b">
    <tr align="center">
        <td>测试页面  测试页面</td>
        <td>测试页面  测试页面</td>
    </tr>
</table>
</body>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-git.js"></script>
<script type="text/javascript" src="../js/shuiyin2.js" charset="utf-8"></script>
<script type="text/javascript">
    var nowDay = getnowDay();
    var nowTime = getnowTime();
    $('#table1').watermark({
        texts : ["A B C X Y Z +"+nowDay,nowTime], //水印文字
        textColor : "#DCDCDC", //文字颜色
        textFont : '14px 微软雅黑', //字体
        width : 130, //水印文字的水平间距
        height : 120,  //水印文字的高度间距(低于文字高度会被替代)
        textRotate : -20 //-90到0, 负数值,不包含-90
    })
</script>
<script>

</script>
</html>

 水印:shuiyin2.js代码:

(function(){

    var watermark = function(self){
        this.elem = self;
    }

    watermark.prototype = {
        defaults : {
            texts : ['此处水印文字'],
            width : 100, //每行水印文字的水平间距
            height : 100, //水印文字的高度间距(低于文字高度会被替代)
            textRotate : -30 , //文字旋转 度数
            textColor : '#ede8e8', //文字颜色
            textFont : '15px 微软雅黑' //字体
        },
        options : {
            canvas : []
        },
        init : function(options){
            $.extend(this.options, this.defaults, options);
            var $body = $('body'),
                can1 = this.__createCanvas($body),
                can2 = this.__createCanvas($body),
                canAll = this.__createCanvas($body),
                settings = this.options,
                txtlen = settings.texts.length;

            settings.deg = settings.textRotate * Math.PI / 180; //js里的正弦余弦用的是弧度

            this.__calcTextSize($body);
            var repeatTimes = Math.ceil(screen.width / settings.txts.length / settings.width);
            settings.canvasWidth = settings.canvasWidth * repeatTimes;
            var extTxts = [];
            while(repeatTimes--) extTxts = extTxts.concat(settings.txts);
            settings.txts = extTxts;

            var fixH = settings.maxWidth * Math.abs(Math.sin(settings.deg)) + Math.cos(settings.deg) * settings.textHeight;
            if(fixH > settings.height) settings.height = fixH;
            var ctx1 = this.__setCanvasStyle(can1, settings.canvasWidth, settings.height);
            var ctx2 = this.__setCanvasStyle(can2, settings.canvasWidth, settings.height);
            var ctx = this.__setCanvasStyle(canAll, settings.canvasWidth, settings.height * 2, true);

            this.__drawText(ctx1, settings.txts);
            this.__drawText(ctx2, settings.txts.reverse());

            //合并canvas
            ctx.drawImage(can1, 0, 0, settings.canvasWidth, settings.height);
            ctx.drawImage(can2, 0, settings.height, settings.canvasWidth, settings.height);
            var dataURL = canAll.toDataURL("image/png");
            $(this.elem).css('backgroundImage', "url("+ dataURL +")");
            //this.__destory();
        },
        __createCanvas : function($container){
            var canvas = document.createElement('canvas');
            $container.append(canvas);
            this.options.canvas.push(canvas);
            return canvas;
        },
        __calcTextSize : function($container){
            var txts = [],
                maxWidth = 0,
                canvasWidth = 0,
                settings = this.options;
            $.each(settings.texts, function(i, text){
                var span = $('<span style="font:'+settings.textFont+';visibility: hidden;display: inline-block;"> '+text+ '</span>')
                    .appendTo($container);
                var tWidth = span[0].offsetWidth,
                    tHeight = span[0].offsetHeight;
                span.remove();
                txts.push({
                    txt : text,
                    width : tWidth,
                    height : tHeight
                });
                maxWidth = Math.max(maxWidth, tWidth);
                settings.textHeight = tHeight;
                var shadow = Math.cos(settings.deg) * tWidth;
                canvasWidth += (settings.width < shadow ? shadow : settings.width) - tHeight * Math.sin(settings.deg);
            });
            settings.txts = txts;
            settings.maxWidth = maxWidth;
            settings.canvasWidth = canvasWidth;
        },
        __setCanvasStyle : function(canvas, width, height, notextstyle){
            canvas.width = width;
            canvas.height = height;
            canvas.style.display='none';

            var ctx = canvas.getContext('2d');
            if(!notextstyle){
                var deg = this.options.deg,
                    absSindeg = Math.abs(Math.sin(deg));
                ctx.rotate(deg);
                //基于视窗的 x、y偏移量
                var offset = absSindeg * this.options.height - this.options.textHeight * absSindeg;
                var nx = - offset * Math.cos(deg),
                    ny = - offset * absSindeg;
                ctx.translate( nx, ny * absSindeg);

                ctx.font = this.options.textFont;
                ctx.fillStyle = this.options.textColor;
                ctx.textAlign = 'left';
                ctx.textBaseline = 'Middle';
            }
            return ctx;
        },
        __drawText: function(ctx, txts){
            var settings = this.options;
            $.each(txts, function(i, obj){

                var wnap = (settings.maxWidth - obj.width) / 2 ;
                var x = settings.width * Math.cos(settings.deg) * i,
                    y = - x * Math.tan(settings.deg) + settings.height;
                ctx.fillText(obj.txt, x + wnap, y);
            });
        },
        __destory : function(){
            $.each(this.options.canvas, function(i, canvas){
                canvas.remove();
                canvas = null;
            });
        }
    }

    $.fn.watermark = function(options){
        new watermark(this).init(options);
    }

})(jQuery);

function getnowDay() {
    var d = new Date();
    var year = d.getFullYear();
    var month = change(d.getMonth() + 1);
    var day = change(d.getDate());
    function change(t) {
        if (t < 10) {
            return "0" + t;
        } else {
            return t;
        }
    }
    var time = year + '年' + month + '月' + day + '日 ';
    return time;
}

function getnowTime() {
    var d = new Date();
    var hour = change(d.getHours());
    var minute = change(d.getMinutes());
    var second = change(d.getSeconds());
    function change(t) {
        if (t < 10) {
            return "0" + t;
        } else {
            return t;
        }
    }
    var time = hour + '时' + minute + '分' + second + '秒';
    return time;
}

 

posted @ 2020-01-14 17:09  毛兔dimples  阅读(98)  评论(0编辑  收藏  举报