有一个需求是这样的。

一个组件里若干个区块。区块数量不定。

区块里面是一个波浪效果组件,而这个一般用 SVG 做。

所以就变成了在 react 中使用 SVG 的问题。

首先是波浪效果需要的样式。

.p{
font-size: 12px;
line-height: 2;
text-align: center;
margin:0;
width: 52px;
color: #fff;
} .irrigate_svg {
height: 52px;
width: 52px;
}
.masked {
-webkit-mask: url(#myMask);
mask: url(#myMask);
}
.irrigate_wrap {
transform: translateY(112px);
}
.irrigate_svg {
overflow: hidden;
}
.irrigate_rate {
animation-name:wavingleft, wavingUp;
animation-duration:6s, 6s;
animation-timing-function:linear, linear;
animation-iteration-count:infinite, 1;
}
@keyframes wavingleft {
0% {
transform: translateX(-239px);
}
50% {
transform: translateX(0px);
}
100% {
transform: translateX(-239px);
}
}
@keyframes wavingright {
0% {
transform: translateX(0px);
}
50% {
transform: translateX(-239px);
}
100% {
transform: translateX(0px);
}
}
@keyframes wavingUp{
0% {
transform: translateX(-239px) translateY(100px);
}
50% {
transform: translateX(0px);
}
100% {
transform: translateX(-239px) translateY(0px);
}
}

引入样式,以及组件文件的结构。

import React from 'react'
import Styles from './waveContainer.less' class Wave extends React.Component {} class WaveContainer extends React.Component {} export default WaveContainer

一个组件文件里可能有很多层组件,只需要输出最外面的一层。

SVG 组件。

class Wave extends React.Component {
constructor(props){
super(props)
this.state = {
}
} render(){
const item = this.props.data const getNumberPosition = (number) => {
let style
const s = {
s1:"matrix(1 0 0 1 96.44 150)",
s2:"matrix(1 0 0 1 66.44 150)",
s3:"matrix(1 0 0 1 46.44 150)",
} switch (parseInt(number).toString().length) {
case 0:
style = s.s2
break;
case 1:
style = s.s1
break;
case 2:
style = s.s2
break;
case 3:
style = s.s3
break;
default:
style = s.s2
}
return style
} const getColor = (item) => {
return item.color
} return (
<div >
<div className={Styles.irrigate_svg}>
<svg version="1.1" id="图层_5" xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" viewBox="0 0 244.2 244.2" style={{ enableBackground:"new 0 0 244.2 244.2" }} >
<defs>
<mask id="myMask">
<circle style={{ fill:"#fff",stroke:"#fff","strokeMiterlimit":"10" }} cx="122.1" cy="122.1" r="122.1"/>
</mask>
</defs>
<g className={Styles.masked}>
<g className={Styles.irrigate_wrap}>
{/* <linearGradient id="SVGID_1_" gradientUnits="userSpaceOnUse" x1="1120.8899" y1="262.1278" x2="1120.8899" y2="1.81" gradientTransform="matrix(-1 0 0 -1 1430.8899 246)">
<stop offset="0" style={{ stopColor:getColor(item),"stopOpacity":"0.8" }}/>
<stop offset="1" style={{ stopColor:getColor(item),"stopOpacity":"0.7"}}/>
</linearGradient> */}
<path className={Styles.irrigate_rate} style={{ fill:getColor(item) }} d="M0-3.8c0,0,44.7-14.3,77-12.1c32.9,2.3,95.6,33.3,128.3,31.1c38.9-2.6,116.7-33.7,153-29.7
c22.9,2.5,73,20.4,95.7,24.4c30.9,5.5,64.2,8.4,90.3,6.8C567.8,15.2,620-3.8,620-3.8v248H0V-3.8z"/>
</g>
</g>
<g>
<g id="图层_5-2">
<circle style={{ fill:"none",stroke:"#25437C",strokeWidth:"1",strokeMiterlimit:"1" }} cx="122.1" cy="122.1" r="122.1"/>
<text transform={getNumberPosition(item.data)} style={{ fill:"#FFF", fontSize:"100px" }}> {item.data} </text>
<linearGradient id="SVGID_3_" gradientUnits="userSpaceOnUse" x1="0" y1="123.91" x2="244.18" y2="123.91" gradientTransform="matrix(1 0 0 -1 0 246)">
<stop offset="0" style={{ stopColor:"rgb(55,107,112)" }}/>
<stop offset="1" style={{ stopColor:"rgb(55,107,112)" }}/>
</linearGradient>
<circle style={{ fill:"none",stroke:"url(#SVGID_3_)",strokeWidth:"1",strokeLinecap:"round",strokeMiterlimit:"1" }} cx="122.1" cy="122.1" r="122.1"/>
</g>
</g>
</svg>
</div>
<p className={Styles.p}> {item.name} </p>
</div>
);
}
}

SVG原本怎么写,JSX就怎么写。

但是遇到了一个问题。

linearGradient, 这个用来做渐变的标签,直接使用没有问题,但如果循环输出的时候把需要的值赋值进去,就会发现所有 SVG 组件的颜色都和第一个一样了。而用开发者工具查看的时候,颜色确实是赋值进去了。不知道是怎么回事。
 
然后是容器组件。
class WaveContainer extends React.Component {
constructor(props){
super(props)
this.state = {
}
} render(){
const baseFlex = {
display: 'flex',
justifyContent: 'center',
alignItems: 'center'
}
const theStyle = {
main:{
...baseFlex,
width:'100%',
height:'100%',
color:"#fff"
},
tem:{
...baseFlex,
flex:"auto",
color:'#fff'
},
shellA:{
...baseFlex,
width:'100%',
height:'100%'
},
shellB:{
...baseFlex,
width:'100%',
height:'50%'
}
} // const dataBar = this.props.dataBar const dataBar = (() => {
if (this.props.curcity && this.props.curcity === 'all') {
return {
data:{
sData:[
{ name: 'a', data: 55, color:'rgb(79,239,223)' },
{ name: 'a', data: 5, color:'rgb(79,239,223)'},
{ name: 'a', data: 36, color:'rgb(79,239,223)'},
{ name: 'a', data: 476, color:'rgb(87,207,30)'},
{ name: 'a', data: 226, color:'rgb(79,239,223)'},
{ name: 'a', data: 273, color:'rgb(251,211,36)'}
]
}
}
} else {
return {
data:{
sData:[
{ name: 'a', data: 124, color:'rgb(79,239,223)' },
{ name: 'a', data: 253, color:'rgb(79,239,223)'},
{ name: 'a', data: 321, color:'rgb(79,239,223)'},
{ name: 'a', data: 156, color:'rgb(87,207,30)'},
{ name: 'a', data: 2, color:'rgb(79,239,223)'},
{ name: 'a', data: 77, color:'rgb(251,211,36)'}
]
}
}
}
})() const Container = ((dataBar) => { const flexMatrix = [
[0,0],
[1,0],
[2,0],
[3,0],
[2,2],
[3,2],
[3,3],
[4,3],
[4,4],
[5,4],
[5,5],
[6,5],
[6,6]
] const sData = dataBar.data.sData
const length = sData.length const matrix = flexMatrix[length] ? flexMatrix[length] : flexMatrix[12] if (matrix[0] === 0) {
return ""
} let temShell, temA, temB temA = sData.slice(0, matrix[0]).map((item, index) =>
<div style={theStyle.tem} key={index.toString()}> <Wave data={item} /> </div>
); if (matrix[1] === 0) {
temB = ""
} else {
temB = sData.slice(matrix[0], (matrix[0] + matrix[1])).map((item, index) =>
<div style={theStyle.tem} key={index.toString()}> <Wave data={item} /> </div>
);
} if (matrix[1] === 0) {
temShell = <div style={theStyle.shellA} > {temA} </div>
} else {
temShell = [0,0].map((item, index) =>
<div style={theStyle.shellB} key={"temShell" + index.toString()}> {index === 0 ? temA : temB} </div>
); theStyle.main.flexWrap = "wrap"
} console.log(temShell) return temShell
})(dataBar) return (
<div style={theStyle.main}>
{/* <Wave /> */}
{ Container }
</div>
);
}
}

以上。

react实战 : react 与 svg的更多相关文章

  1. react实战 : react 与 canvas

    有一个需求是这样的. 一个组件里若干个区块.区块数量不定. 区块里面是一个正六边形组件,而这个用 SVG 和 canvas 都可以.我选择 canvas. 所以就变成了在 react 中使用 canv ...

  2. react实战项目开发(1) 搭建react开发环境初始化项目(Create-react-app)

    前言 Create React App npm install -g create-react-app create-react-app my-app cd my-app npm start 执行命令 ...

  3. 【视频合集】极客时间 react实战进阶45讲 【更新中】

    https://up2.v.sharedaka.com/video/ochvq0AVfpa71A24bmugS5EewhFM1553702519936.mp4 01 React出现的历史背景及特性介绍 ...

  4. 《React Native 精解与实战》书籍连载「React 与 React Native 简介」

    此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...

  5. RN 实战 & React Native 实战

    RN 实战 & React Native 实战 https://abc.xgqfrms.xyz/react-native-docs/ 0.59 https://github.com/xgqfr ...

  6. React实战之将数据库返回的时间转换为几分钟前、几小时前、几天前的形式。

    React实战之将数据库返回的时间转换为几分钟前.几小时前.几天前的形式. 不知道大家的时间格式是什么样子的,我先展示下我这里数据库返回的时间格式 ‘2019-05-05T15:52:19Z’ 是这个 ...

  7. React实战之Ant Design—Upload上传_附件上传

    React实战之Ant Design—Upload上传_附件上传 Upload组件大家都在官方文档中看过了,但写的时候还是会遇到许多问题,一些新手看了文档后感觉无从下手,本文过多的简绍就不说了,直接看 ...

  8. React实战之60s倒计时按钮(发送短信验证按钮)

    React实战之60s倒计时按钮——短信验证按钮 导入:(antd组件——Form表单) import { Button, Form, Input } from 'antd'; const FormI ...

  9. React显示文件夹中SVG

    import React from 'react'; import _ from 'lodash'; import styles from './iconPicker.less'; const req ...

随机推荐

  1. 对 JsonConvert 的认识太肤浅了,终于还是遇到了问题

    一:背景 1. 讲故事 在开始本文之前,真的好想做个问卷调查,到底有多少人和我一样,对 JsonConvert 的认识只局限在 SerializeObject 和 DeserializeObject ...

  2. Netty源码分析之自定义编解码器

    在日常的网络开发当中,协议解析都是必须的工作内容,Netty中虽然内置了基于长度.分隔符的编解码器,但在大部分场景中我们使用的都是自定义协议,所以Netty提供了  MessageToByteEnco ...

  3. JavaWeb网上图书商城完整项目--day02-12.激活功能各层实现

    1.我们来看程序的代码 数据库层: 1.通过激活码查找到对应的用户 2.设置用户的激活状态 2.业务层 1.通过数据库接口通过验证码得到对应的用户 2.判断当用户是否为空,如果没有通过激活码查找到对应 ...

  4. 6.30集训模拟赛4(炸裂的一天qwq)

    T1浇水: 题目描述 在一条长n米,宽m米米的长方形草地上放置着k个喷水装置.假设长方形草地的坐标范围为[ 0 , 0 ] ~ [ n , m ],那么第 i 个喷水装置的位置为(ai,m/2),也就 ...

  5. windows7 添加tomcat(解压缩版或者免安装版)自启动服务

    https://blog.csdn.net/piggachen/article/details/69943223 :(service.bat方式安装服务或者windows任务方式添加) https:/ ...

  6. springMVC中的HttpSession与Model

    目录 1.1 spring的@MODELATTRIBUTE 2.1 session的概念 3.1 示例 4.1 为什么springmvc框架要使用model这个对象呢? 突然发问:相信很多人在做WEB ...

  7. 如何基于 echarts 实现区间柱状图(包括横向)?

    目录 需求 借鉴 echarts 的 demo 最终实现思路 实现效果 遇到的问题: 代码映射 源码 最后 始终如一 需求 需要利用 echarts 实现区间柱状图,效果如下: 效果来源于:g2-柱状 ...

  8. fastjson到底做错了什么?为什么会被频繁爆出漏洞?

    GitHub 15.8k Star 的Java工程师成神之路,不来了解一下吗! GitHub 15.8k Star 的Java工程师成神之路,真的不来了解一下吗! GitHub 15.8k Star ...

  9. NIVIDIA Tegra K1 QWT安装使用问题和解决办法

    在Linux系统下,Tegra K1(ARM体系),只有QtCreator的环境下,去安装Qwt6.1.0: 下载: 系统安装好之后,直接联网在系统自带的软件安装程序中搜索Qt,安装Qt5.2.1,之 ...

  10. sql语句-CASE WHEN用法详解

    当我们需要从数据源上 直接判断数据显示代表的含义的时候 ,就可以在SQL语句中使用 Case When这个函数了. Case具有两种格式.简单Case函数和Case搜索函数. 第一种 格式 : 简单C ...