Vue使用自定義指令實(shí)現(xiàn)頁(yè)面底部加水印
給項(xiàng)目的整個(gè)背景加上自定義水印,可以改變水印的文案和字體顏色等
實(shí)現(xiàn)思路 這里使用的技術(shù)主要是canvas,在實(shí)現(xiàn)水印的過(guò)程中,主要使用了canvas的特性 使用 canvas 特性生成 base64 格式的圖片文件,然后設(shè)置其字體大小,顏色等 最后將其設(shè)置為背景圖片,這就實(shí)現(xiàn)了頁(yè)面的水印效果實(shí)現(xiàn)效果
實(shí)現(xiàn)代碼
<template> <div > <div v-waterMarker='{text:’卡洛背心 - 版權(quán)所有’,textColor:’rgba(180, 180, 180, 0.4)’}'><div class='water-marker-item'>測(cè)試問(wèn)題啊測(cè)試問(wèn)題啊測(cè)試問(wèn)題啊測(cè)試問(wèn)題啊測(cè)試問(wèn)題啊測(cè)試問(wèn)題啊</div> </div> </div></template><script>import waterMarker from ’../../directive/test/waterMarker’export default { directives: { waterMarker }, data(){ return{ } }, methods:{ }}</script><style lang='scss'>.water-marker{ height: 300px; .water-marker-item{ line-height: 300px; }}</style>
waterMarker.js文件如下:
function addWaterMarker(str, parentNode, font, textColor) { // 水印文字,父元素,字體,文字顏色 var can = document.createElement(’canvas’) parentNode.appendChild(can) can.width = 200 can.height = 150 can.style.display = ’none’ var cans = can.getContext(’2d’) cans.rotate((-20 * Math.PI) / 180) cans.font = font || ’16px Microsoft JhengHei’ cans.fillStyle = textColor || ’rgba(180, 180, 180, 0.3)’ cans.textAlign = ’left’ cans.textBaseline = ’Middle’ cans.fillText(str, can.width / 10, can.height / 2) parentNode.style.backgroundImage = ’url(’ + can.toDataURL(’image/png’) + ’)’}const waterMarker = { bind: function (el, binding) { addWaterMarker(binding.value.text, el, binding.value.font, binding.value.textColor) },}export default waterMarker
到此這篇關(guān)于Vue使用自定義指令實(shí)現(xiàn)頁(yè)面底部加水印的文章就介紹到這了,更多相關(guān)Vue 頁(yè)面底部加水印內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 完美解決vue 中多個(gè)echarts圖表自適應(yīng)的問(wèn)題2. PHP實(shí)現(xiàn)簡(jiǎn)單線性回歸之?dāng)?shù)學(xué)庫(kù)的重要性3. 利用CSS制作3D動(dòng)畫(huà)4. Java GZip 基于內(nèi)存實(shí)現(xiàn)壓縮和解壓的方法5. jsp+servlet簡(jiǎn)單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))6. JAMon(Java Application Monitor)備忘記7. SpringBoot+TestNG單元測(cè)試的實(shí)現(xiàn)8. Springboot 全局日期格式化處理的實(shí)現(xiàn)9. 存儲(chǔ)于xml中需要的HTML轉(zhuǎn)義代碼10. idea配置jdk的操作方法
