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
| var Watermark = function(containerCls) { var container = document.querySelector(containerCls);
var markTextEl = document.querySelector('#watermark-text'); var watermarkText = markTextEl.innerText;
var markWidth = markTextEl.clientWidth * 0.87 + markTextEl.clientHeight * 0.5; var markHeight = markTextEl.clientWidth * 0.5 + markTextEl.clientHeight * 0.87;
var canvasEl1 = document.createElement('canvas'); canvasEl1.setAttribute('class', 'watermark'); canvasEl1.setAttribute('width', markWidth); canvasEl1.setAttribute('height', markHeight); canvasEl1.setAttribute('style', 'display: none');
container.appendChild(canvasEl1);
var imgBgEl = document.createElement('div'); imgBgEl.setAttribute('class', 'repeat-watermark'); container.appendChild(imgBgEl);
this.opt = { container: container, markWidth: markWidth || 160, markHeight: markHeight || 100, watermark: watermarkText, docWidth: container.clientWidth, docHeight: container.clientHeight, fontStyle: "20px Microsoft YaHei", rotateAngle: -30 * Math.PI / 180, fontColor: "rgba(220, 220, 220, 127)",
firstLinePositionX: -(markHeight - markTextEl.clientHeight * 0.87) * 0.5, firstLinePositionY: (markHeight - markTextEl.clientHeight * 0.87) * 0.87 + markTextEl.clientHeight }; this.draw(this.opt.docWidth, this.opt.docHeight); };
Watermark.prototype = {
draw: function(docWidth, docHeight) { var cw = this.opt.container.querySelector('.watermark'); var imgBg = this.opt.container.querySelector('.repeat-watermark');
var ctx = cw.getContext("2d"); ctx.clearRect(0, 0, this.opt.markWidth, this.opt.markHeight);
ctx.font = this.opt.fontStyle; ctx.rotate(this.opt.rotateAngle); ctx.fillStyle = this.opt.fontColor; ctx.fillText(this.opt.watermark, this.opt.firstLinePositionX, this.opt.firstLinePositionY); ctx.rotate(-this.opt.rotateAngle);
var data = cw.toDataURL('image/png', .1); imgBg.style.background = "url(" + data + ")" } };
|