vue 使用class創(chuàng)建和清除水印的示例代碼
頁面添加水印的方法有很多,以下舉例使用class定義的方法進(jìn)行水印內(nèi)容渲染:
1、新建文件:WatermarkClass.js
export default class WatermarkClass { constructor({id='watermarkID', str = '', fontSize = 18, width = 400, height = 400, fillStyle='#333333', opacity = 1 }) { this.id = id; this.str = str; this.fontSize = fontSize; this.width = width; this.height = height; this.fillStyle = fillStyle this.opacity = opacity; } // 繪制水印 draw() { if (document.getElementById(this.id) !== null) { document.body.removeChild(document.getElementById(this.id)); } const canvas = document.createElement('canvas'); // 設(shè)置canvas畫布大小 canvas.width = this.width; canvas.height = this.height; const ctx = canvas.getContext('2d'); ctx.rotate(-(15 * Math.PI) / 180); // 水印旋轉(zhuǎn)角度 ctx.font = `${this.fontSize}px Vedana`; ctx.fillStyle = this.fillStyle; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; this.str.split(',').forEach((item, index) => { ctx.fillText(item, canvas.width / 2, canvas.height / 2 + (index * this.fontSize + 10)); // 水印在畫布的位置x,y軸 }); const div = document.createElement('div'); div.id = this.id; div.style.pointerEvents = 'none'; div.style.top = '30px'; div.style.left = '10px'; div.style.opacity = this.opacity; div.style.position = 'fixed'; div.style.zIndex = '999999'; div.style.width = `${document.documentElement.clientWidth}px`; div.style.height = `${document.documentElement.clientHeight}px`; div.style.background = `url(${canvas.toDataURL('image/png')}) left top repeat`; document.body.appendChild(div); } setOptions({fontSize = 18, width = 300, height = 300, opacity = 1, str = ''}) { this.fontSize = fontSize; this.width = width; this.height = height; this.fillStyle = fillStyle this.opacity = opacity; this.str = str; this.draw(); } // 繪制 render() { this.draw(); window.onresize = () => { this.draw(); }; } // 移除水印 removeWatermark() { if (document.getElementById(this.id) !== null) { document.body.removeChild(document.getElementById(this.id)); } } }
2、在頁面種引入使用:
import watermarkClass from '@/libs/watermarkClass';export default { name: 'App', mounted: function () { this.initWatermark() }, methods: { initWatermark() { // 方法一 let watermark = new watermarkClass({ id: 'watermarkID', str: '紫藤蘿-watermarkClass', fontSize: 20, width: 300, height: 200, fillStyle: '#dddddd', opacity: 0.4, }); watermark.render(); // 5秒后,清除水印 setTimeout(() => { watermark.removeWatermark(); }, 5000); } },};
以上就是vue 使用class創(chuàng)建和清除水印的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于vue 創(chuàng)建和清除水印的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Python安裝并操作redis實(shí)現(xiàn)流程詳解2. pip已經(jīng)安裝好第三方庫但pycharm中import時(shí)還是標(biāo)紅的解決方案3. CSS自定義滾動(dòng)條樣式案例詳解4. 詳解CSS偽元素的妙用單標(biāo)簽之美5. 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法6. Ajax實(shí)現(xiàn)表格中信息不刷新頁面進(jìn)行更新數(shù)據(jù)7. HTML <!DOCTYPE> 標(biāo)簽8. Java Spring WEB應(yīng)用實(shí)例化如何實(shí)現(xiàn)9. ajax post下載flask文件流以及中文文件名問題10. msxml3.dll 錯(cuò)誤 800c0019 系統(tǒng)錯(cuò)誤:-2146697191解決方法
