react實(shí)現(xiàn)頁(yè)面水印效果的全過(guò)程
前言
1.為什么選用svg 而不是cavans?
因?yàn)閏avans 在高分辨率屏幕下,需要根據(jù) devicePixelRatio做寬高的適配,不然就會(huì)很模糊,而svg是矢量圖,原生支持各種分辨率,不會(huì)產(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.實(shí)現(xiàn)過(guò)程
- 構(gòu)造一個(gè)水印圖
- 將水印圖鋪滿整個(gè)容器
- 水印組件:支持子組件內(nèi)容插槽
構(gòu)造一個(gè)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>`
由上面的代碼,我們可以得到一個(gè)svg xml 的字符串,接下來(lái)我們將它變成url 資源
const blob = new Blob([res], {
type: 'image/svg+xml',
})
const url = URL.createObjectURL(blob)
由此,我們就得到了一個(gè)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', //點(diǎn)擊穿透
}}
></div>
至此,我們很輕松的得到了一個(gè)鋪滿水印的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', //點(diǎn)擊穿透
}}
></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實(shí)現(xiàn)頁(yè)面水印效果的文章就介紹到這了,更多相關(guān)react實(shí)現(xiàn)頁(yè)面水印內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React+高德地圖實(shí)時(shí)獲取經(jīng)緯度,定位地址
思路其實(shí)沒(méi)有那么復(fù)雜,把地圖想成一個(gè)盒子容器,地圖中心點(diǎn)想成盒子中心點(diǎn);扎點(diǎn)在【地圖中心點(diǎn)】不會(huì)動(dòng),當(dāng)移動(dòng)地圖時(shí),去獲取【地圖中心點(diǎn)】經(jīng)緯度,設(shè)置某個(gè)位置的時(shí)候,將經(jīng)緯度設(shè)置為【地圖中心點(diǎn)】即可2021-06-06
解決React報(bào)錯(cuò)React.Children.only expected to rece
這篇文章主要為大家介紹了React報(bào)錯(cuò)React.Children.only expected to receive single React element child分析解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01

