1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| <template> <div id="watermark"> <canvas id="canvas" height='100%' width='100%'> 你的浏览器不支持canvas,请升级你的浏览器 </canvas> <canvas id="repeat-watermark" height='100%' width='100%'> 你的浏览器不支持canvas,请升级你的浏览器 </canvas> </div> </template> <script> export default { data() { return {
} }, component: {
}, async mounted() { await this.loadWatermark(); await this.getWatermark(); }, methods:{ // 先制作小水印 loadWatermark() { let canvas = document.getElementById('canvas'); if (!canvas.getContext) return; // 小画布的宽高 canvas.height = 100; canvas.width = 100; let ctx = canvas.getContext('2d'); ctx.clearRect(0,0,160,100); //绘制之前画布清除 ctx.font="20px 黑体"; ctx.rotate(-20*Math.PI/180); ctx.fillStyle = "rgba(100,100,100,0.1)"; ctx.fillText("刘泽浩", -20, 80); ctx.rotate('20*Math.PI/180'); //坐标系还原 }, getWatermark() { // 平铺满整个容器 let crw = document.getElementById('repeat-watermark'); let canvas = document.getElementById('canvas'); if (!crw.getContext) return; // 整个容器的宽高 let height = document.getElementById('watermark').offsetHeight; let width= document.getElementById('watermark').offsetWidth; crw.width = width; crw.height = height; let ctxr = crw.getContext("2d"); ctxr.clearRect(0,0,crw.width,crw.height); //清除整个画布 \ //主要是这个API let pat = ctxr.createPattern(canvas, "repeat"); //在指定的方向上重复指定的元素 ctxr.fillStyle = pat; ctxr.fillRect(0, 0, crw.width, crw.height); } }, } </script> <style lang="scss" scoped> #watermark { position: relative; width: 100%; height: 100%; } #repeat-watermark{ position:absolute; top:0; z-index:10; background: #fff; } </style>
|