react实现页面水印效果的全过程

发布时间:2022-04-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了react实现页面水印效果的全过程脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

前言

1.为什么选用svg 而不是cavans?

因为cavans 在高分辨率屏幕下,需要根据 devicePixelRatio做高的适配,不然就会很模糊,而svg是矢量图,原生支持各种分辨率,不会产生模糊的情况。

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.实现过程

  • 构造一个水印图
  • 将水印图铺满整个容器
  • 水印组件:支持子组件内容插槽

构造一个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
} &amp; 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实现页面水印效果的文章就介绍到这了,更多相关react实现页面水印内容请搜索脚本宝典以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本宝典!

脚本宝典总结

以上是脚本宝典为你收集整理的react实现页面水印效果的全过程全部内容,希望文章能够帮你解决react实现页面水印效果的全过程所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:react