vue 使用class創(chuàng)建和清除水印的示例代碼
頁(yè)面添加水印的方法有很多,以下舉例使用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畫(huà)布大小 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)); // 水印在畫(huà)布的位置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、在頁(yè)面種引入使用:
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)建和清除水印的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue-better-scroll 的使用實(shí)例代碼詳解
這篇文章主要介紹了vue-better-scroll 的使用實(shí)例代碼詳解,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-12-12vue+element搭建后臺(tái)小總結(jié) el-dropdown下拉功能
這篇文章主要為大家詳細(xì)介紹了vue+element搭建后臺(tái)小總結(jié),el-dropdown下拉功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09Vue.js項(xiàng)目實(shí)戰(zhàn)之多語(yǔ)種網(wǎng)站的功能實(shí)現(xiàn)(租車(chē))
這篇文章主要介紹了Vue.js項(xiàng)目實(shí)戰(zhàn)之多語(yǔ)種網(wǎng)站(租車(chē))的功能實(shí)現(xiàn) ,需要的朋友可以參考下2019-08-08Vue項(xiàng)目頁(yè)面跳轉(zhuǎn)時(shí)瀏覽器窗口上方顯示進(jìn)度條功能
這篇文章主要介紹了Vue項(xiàng)目頁(yè)面跳轉(zhuǎn)時(shí)瀏覽器窗口上方顯示進(jìn)度條功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03解決vue與node模版引擎的渲染標(biāo)記{{}}(雙花括號(hào))沖突問(wèn)題
這篇文章主要介紹了解決vue與node模版引擎的渲染標(biāo)記{{}}(雙花括號(hào))沖突問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09vue-mounted中如何處理data數(shù)據(jù)
這篇文章主要介紹了vue-mounted中如何處理data數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03vue中v-for和v-if一起使用之使用compute的示例代碼
這篇文章主要介紹了vue中v-for和v-if一起使用之使用compute的相關(guān)知識(shí),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05