Preface1. Why choose svg instead of cavans? Because cavans needs to adapt its width and height according to devicePixelRatio on high-resolution screens, otherwise it will be blurry. SVG is a vector image that natively supports various resolutions and will not cause blur. 1. Usage examplesimport 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. Implementation process
Construct an svg watermark image 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>` From the above code, we can get a svg xml string, and then we turn it into a url resource const blob = new Blob([res], { type: 'image/svg+xml', }) const url = URL.createObjectURL(blob) Thus, we get a svg resource address, and now we use it as the background image of the div <div style={{ position: 'absolute', width: '100%', height: '100%', backgroundImage: `url(${url})`, top: 0, left: 0, zIndex: 999, pointerEvents: 'none', //click through}} ></div> At this point, we have easily obtained a div covered with watermarks. Next, we will integrate the code and encapsulate it into a component. 3. Component codeimport 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', //click through}} ></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 SummarizeThis is the end of this article about how to use react to achieve page watermark effects. For more information about how to use react to achieve page watermark effects, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! |
<<: How to install and connect Navicat in MySQL 8.0.20 and what to pay attention to
Do you add an alt attribute to the img image tag? ...
Vue first screen performance optimization compone...
Table of contents Preface Setting up slow query l...
I saw that Taobao’s webpage uses import, while man...
Docker version: [root@localhost gae_proxy]# docke...
Description of the problem: When the number of as...
1. Background Buttons are very commonly used, and...
This article describes the support and problem so...
Docker is becoming more and more mature and its f...
When using nginx as a reverse proxy, you can simp...
Basic knowledge of responsive layout development ...
You can go to the Ubuntu official website to down...
This article shares the specific code of jQuery t...
I always thought that Docker had no IP address. I...
In the pages of WEB applications, tables are ofte...