欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

react實(shí)現(xiàn)頁(yè)面水印效果的全過(guò)程

 更新時(shí)間:2021年09月06日 14:52:37   作者:五邊形的男人  
大家常常關(guān)注的是網(wǎng)站圖片增加水印,而很少關(guān)注頁(yè)面水印,其實(shí)這個(gè)需求也是比較常見(jiàn)的,比如公文系統(tǒng)、合同系統(tǒng)等,這篇文章主要給大家介紹了關(guān)于react實(shí)現(xiàn)頁(yè)面水印效果的相關(guān)資料,需要的朋友可以參考下

前言

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è)水印圖
  • 將水印圖鋪滿(mǎn)整個(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è)鋪滿(mǎn)水印的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-router v4 路由配置方法小結(jié)

    React-router v4 路由配置方法小結(jié)

    本篇文章主要介紹了React-router v4 路由配置方法小結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-08-08
  • React+高德地圖實(shí)時(shí)獲取經(jīng)緯度,定位地址

    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
  • 取消正在運(yùn)行的Promise技巧詳解

    取消正在運(yùn)行的Promise技巧詳解

    這篇文章主要為大家介紹了取消正在運(yùn)行的Promise技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • antd踩坑之javascriptEnabled配置方式

    antd踩坑之javascriptEnabled配置方式

    這篇文章主要介紹了antd踩坑之javascriptEnabled配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • react中的DOM操作實(shí)現(xiàn)

    react中的DOM操作實(shí)現(xiàn)

    某些情況下需要在典型數(shù)據(jù)流外強(qiáng)制修改子代。要修改的子代可以是 React 組件實(shí)例,也可以是 DOM 元素。這時(shí)就要用到refs來(lái)操作DOM,本文詳細(xì)的介紹一下使用,感興趣的可以了解一下
    2021-06-06
  • 解決React報(bào)錯(cuò)React.Children.only expected to receive single React element child

    解決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
  • React中如何使用scss

    React中如何使用scss

    這篇文章主要介紹了React中如何使用scss問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • react如何快速設(shè)置文件路徑別名

    react如何快速設(shè)置文件路徑別名

    React是用于構(gòu)建用戶(hù)界面的JavaScript庫(kù), 起源于Facebook的內(nèi)部項(xiàng)目,這篇文章主要介紹了react如何快速設(shè)置文件路徑別名,需要的朋友可以參考下
    2021-04-04
  • react實(shí)現(xiàn)原生下拉刷新

    react實(shí)現(xiàn)原生下拉刷新

    這篇文章主要為大家詳細(xì)介紹了react實(shí)現(xiàn)原生下拉刷新,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • React?Context原理深入理解源碼示例分析

    React?Context原理深入理解源碼示例分析

    這篇文章主要為大家介紹了React?Context原理深入理解源碼示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01

最新評(píng)論