前端显示水印

 

watermark(str) {
    let node = document.getElementById("imageWrapper");
    let can = document.createElement("canvas");
    can.width = document.documentElement.clientWidth / 2 // 一行显示两个
    can.height = (node.offsetHeight - 60) / 3 // 显示三行
    let ctx = can.getContext('2d');
    let fontSize = 14
    ctx.font = `${fontSize}px Georgia`
    let { width: wText } = ctx.measureText(str)
    let hText = fontSize
    ctx.fillStyle = '#CCCCCC'
    ctx.translate((can.width + 10 - (Math.cos(30) * wText))/2, (can.height - hText) / 2)  
    //角度
    f(-30)
    function f(i) {
        let deg = Math.PI / 180 * i
        ctx.rotate(deg, deg)
        ctx.fillText(str, -wText / 2, hText / 2)
    }
    let div = document.createElement("div");
    div.id = 'watermark';
    div.style.opacity = 1
    div.style.pointerEvents = "none";
    div.style.top = "44px";
    div.style.left = "0px";
    div.style.position = "fixed";
    div.style.zIndex = "100000";
      
    div.style.width = document.documentElement.clientWidth + "px";
    div.style.height = (node.offsetHeight - 60) + "px";
    div.style.background = "url(" + can.toDataURL("image/png") + ") left top repeat";
    node.appendChild(div);
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值