React realizes the whole process of page watermark effect

React realizes the whole process of page watermark effect

Preface

1. 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 examples

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. Implementation process

  • Construct a watermark image
  • Spread the watermark image over the entire container
  • Watermark component: support subcomponent content slot

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 code

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', //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

Summarize

This 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

>>:  Solution to the problem of not being able to access the home page when adding a tomcat container to Docker

Recommend

Is it necessary to give alt attribute to img image tag?

Do you add an alt attribute to the img image tag? ...

Summary of Vue first screen performance optimization component knowledge points

Vue first screen performance optimization compone...

MySQL optimization solution: enable slow query log

Table of contents Preface Setting up slow query l...

Detailed explanation of the use of Element el-button button component

1. Background Buttons are very commonly used, and...

Detailed explanation of MySQL/Java server support for emoji and problem solving

This article describes the support and problem so...

Docker Stack deployment method steps for web cluster

Docker is becoming more and more mature and its f...

Nginx reverse proxy configuration to remove prefix case tutorial

When using nginx as a reverse proxy, you can simp...

Responsive layout summary (recommended)

Basic knowledge of responsive layout development ...

How to install vim editor in Linux (Ubuntu 18.04)

You can go to the Ubuntu official website to down...

jQuery implements the function of adding and deleting employee information

This article shares the specific code of jQuery t...

How to view the IP address of the Docker container

I always thought that Docker had no IP address. I...

HTML page adaptive width table

In the pages of WEB applications, tables are ofte...