温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

react如何实现页面水印效果

发布时间:2021-09-06 17:30:32 来源:亿速云 阅读:206 作者:小新 栏目:开发技术

这篇文章将为大家详细讲解有关react如何实现页面水印效果,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

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')
)

react如何实现页面水印效果

2.实现过程

  • 构造一个水印图

  • 将水印图铺满整个容器

  • 水印组件:支持子组件内容插槽

构造一个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 的资源地址,现在我们将它用于div 的背景图当中

<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

关于“react如何实现页面水印效果”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI