react實現(xiàn)頁面水印效果的全過程
前言
1.為什么選用svg 而不是cavans?
因為cavans 在高分辨率屏幕下,需要根據(jù) devicePixelRatio做寬高的適配,不然就會很模糊,而svg是矢量圖,原生支持各種分辨率,不會產(chǎn)生模糊的情況。
1.使用示例
import React from 'react'
import ReactDOM from 'react-dom'
import './index.css'
import WaterMarkContent from './components/WaterMarkContent'
import App from './App'
ReactDOM.render(
<React.StrictMode>
<WaterMarkContent>
<App />
</WaterMarkContent>
</React.StrictMode>,
document.getElementById('root')
)

2.實現(xiàn)過程
- 構(gòu)造一個水印圖
- 將水印圖鋪滿整個容器
- 水印組件:支持子組件內(nèi)容插槽
構(gòu)造一個svg 的水印圖
const { text = 'waterMark', fontSize = 16, fillOpacity = '0.2', fillColor = '#000' } = props
const res = `
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="180px" height="180px" viewBox="0 0 180 180">
<text x="-100" y="-30" fill='${fillColor}' transform = "rotate(-35 220 -220)" fill-opacity='${fillOpacity}' font-size='${fontSize}'> ${text}</text>
</svg>`
由上面的代碼,我們可以得到一個svg xml 的字符串,接下來我們將它變成url 資源
const blob = new Blob([res], {
type: 'image/svg+xml',
})
const url = URL.createObjectURL(blob)
由此,我們就得到了一個svg 的資源地址,現(xiàn)在我們將它用于div 的背景圖當(dāng)中
<div
style={{
position: 'absolute',
width: '100%',
height: '100%',
backgroundImage: `url(${url})`,
top: 0,
left: 0,
zIndex: 999,
pointerEvents: 'none', //點擊穿透
}}
></div>
至此,我們很輕松的得到了一個鋪滿水印的div,下面我們將代碼整合,并封裝成組件。
3.組件代碼
import React from 'react'
import { ReactNode, useMemo } from 'react'
type svgPropsType = {
text?: string
fontSize?: number
fillOpacity?: number
fillColor?: string
}
const SvgTextBg = (props: svgPropsType) => {
const { text = 'waterMark', fontSize = 16, fillOpacity = '0.2', fillColor = '#000' } = props
const res = `
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="180px" height="180px" viewBox="0 0 180 180">
<text x="-100" y="-30" fill='${fillColor}' transform = "rotate(-35 220 -220)" fill-opacity='${fillOpacity}' font-size='${fontSize}'> ${text}</text>
</svg>`
const blob = new Blob([res], {
type: 'image/svg+xml',
})
const url = URL.createObjectURL(blob)
return (
<div
style={{
position: 'absolute',
width: '100%',
height: '100%',
backgroundImage: `url(${url})`,
top: 0,
left: 0,
zIndex: 999,
pointerEvents: 'none', //點擊穿透
}}
></div>
)
}
type propsType = {
children?: ReactNode
} & Partial<svgPropsType>
const WaterMarkContent = (props: propsType) => {
const { text, fontSize, fillOpacity, fillColor } = props
const memoInfo = useMemo(
() => ({
text,
fontSize,
fillOpacity,
fillColor,
}),
[text, fontSize, fillOpacity, fillColor]
)
return (
<div style={{ position: 'relative', width: '100%', height: ' 100%' }}>
{props.children}
<SvgTextBg {...memoInfo} />
</div>
)
}
export default WaterMarkContent
總結(jié)
到此這篇關(guān)于react實現(xiàn)頁面水印效果的文章就介紹到這了,更多相關(guān)react實現(xiàn)頁面水印內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React.memo 和 useMemo 的使用問題小結(jié)
隨著代碼的增加,每次的狀態(tài)改變,頁面進行一次 reRender ,這將產(chǎn)生很多不必要的 reRender 不僅浪費性能,從而導(dǎo)致頁面卡頓,這篇文章主要介紹了React.memo 和 useMemo 的使用問題小結(jié),需要的朋友可以參考下2022-11-11
React實現(xiàn)數(shù)字滾動組件numbers-scroll的示例詳解
數(shù)字滾動組件,也可以叫數(shù)字輪播組件,這個名字一聽就是非常普通常見的組件。本文將利用React實現(xiàn)這一組件,感興趣的小伙伴可以了解一下2023-03-03
React中使用ResizeObserver來觀察元素size變化的方法
在 React 中使用 ResizeObserver 來觀察元素的大小變化,可以通過創(chuàng)建一個自定義 Hook 來封裝 ResizeObserver 的邏輯,并在組件中使用這個 Hook,以下是一個完整的示例,展示了如何在 React 中使用 ResizeObserver 來觀察元素的大小變化,需要的朋友可以參考下2024-12-12
Next.js搭建Monorepo組件庫文檔實現(xiàn)詳解
這篇文章主要為大家介紹了Next.js搭建Monorepo組件庫文檔,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
React模擬實現(xiàn)Vue的keepAlive功能
Vue中,keep-alive組件可以緩存組件狀態(tài),在路由切換時重新掛載,實現(xiàn)這一功能在React中并不簡單,但我們可以借助一個第三方庫——react-activation 來模擬Vue的keep-alive功能,需要的朋友可以參考下2024-10-10

