react实战 : react 与 svg
有一个需求是这样的。
一个组件里若干个区块。区块数量不定。
区块里面是一个波浪效果组件,而这个一般用 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就怎么写。
但是遇到了一个问题。
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的更多相关文章
- react实战 : react 与 canvas
有一个需求是这样的. 一个组件里若干个区块.区块数量不定. 区块里面是一个正六边形组件,而这个用 SVG 和 canvas 都可以.我选择 canvas. 所以就变成了在 react 中使用 canv ...
- 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 执行命令 ...
- 【视频合集】极客时间 react实战进阶45讲 【更新中】
https://up2.v.sharedaka.com/video/ochvq0AVfpa71A24bmugS5EewhFM1553702519936.mp4 01 React出现的历史背景及特性介绍 ...
- 《React Native 精解与实战》书籍连载「React 与 React Native 简介」
此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...
- RN 实战 & React Native 实战
RN 实战 & React Native 实战 https://abc.xgqfrms.xyz/react-native-docs/ 0.59 https://github.com/xgqfr ...
- React实战之将数据库返回的时间转换为几分钟前、几小时前、几天前的形式。
React实战之将数据库返回的时间转换为几分钟前.几小时前.几天前的形式. 不知道大家的时间格式是什么样子的,我先展示下我这里数据库返回的时间格式 ‘2019-05-05T15:52:19Z’ 是这个 ...
- React实战之Ant Design—Upload上传_附件上传
React实战之Ant Design—Upload上传_附件上传 Upload组件大家都在官方文档中看过了,但写的时候还是会遇到许多问题,一些新手看了文档后感觉无从下手,本文过多的简绍就不说了,直接看 ...
- React实战之60s倒计时按钮(发送短信验证按钮)
React实战之60s倒计时按钮——短信验证按钮 导入:(antd组件——Form表单) import { Button, Form, Input } from 'antd'; const FormI ...
- React显示文件夹中SVG
import React from 'react'; import _ from 'lodash'; import styles from './iconPicker.less'; const req ...
随机推荐
- user is not in the sudoers file
使用用户账户使用sudo来运行一些特权命令时出现了如下错误(sudo是一个允许特定的用户组用另一个用户(典型的是root)的特权来运行一个命令): user is not in the sudoers ...
- Golang实现数的几种遍历
目录 PreOrder recursive Iterative InOrder Iterative PostOrder Iterative PreOrder recursive package mai ...
- 成为python程序员,对疫情过后的毕业生来说,真是一个不错的方向吗?
Python最近几年,一直被炒得很火,这其中有商业因素,但更重要的是即将到来的人工智能时代,而python就恰好是最适合的编程语言. 所以无论是在职的人,还是在校的学生,都想着跟上这一趋势,但,在今年 ...
- #linux vscode 保存总提示“Retry as sudo”
linux中,对不同路径下的文件,系统默认指定了不同的操作权限(读/写/执行),出现这个问题是由于文件的权限不足造成的.(路径为/opt/lampp/htdocs/LearnPHP_jayce/hel ...
- tarjan算法求scc & 缩点
前置知识 图的遍历(dfs) 强连通&强连通分量 对于有向图G中的任意两个顶点u和v存在u->v的一条路径,同时也存在v->u的路径,我们则称这两个顶点强连通.以此类推,强连通分量 ...
- Redis快照原理详解
本文对Redis快照的实现过程进行介绍,了解Redis快照实现过程对Redis管理很有帮助. Redis默认会将快照文件存储在Redis当前进程的工作目录中的dump.rdb文件中,可以通过配置dir ...
- 线程基础8-quene讲解
PriorityBlockingQueue是一个基于优先级堆的无界的并发安全的优先级队列(FIFO),队列的元素按照其自然顺序进行排序,或者根据构造队列时提供的 Comparator 进行排序,具体取 ...
- Spring-AOP:一、注解demo及基本概念
切面:Aspect 切面=切入点+通知.在老的spring版本中通常用xml配置,现在通常是一个类带上@Aspect注解.切面负责将 横切逻辑(通知) 编织 到指定的连接点中. 目标对象:Target ...
- Flask01-HelloWorld
# flask学习 参考:http://www.pythondoc.com/flask-mega-tutorial/ ## python3. 默认支持虚拟环境使用(用最简单的方法,解决问题) wget ...
- SpringBoot--数据库管理与迁移(LiquiBase)
随着开发时间积累,一个项目会越来越大,同时表结构也越来越多,管理起来比较复杂,特别是当想要把一个答的项目拆分成多个小项目时,表结构拆分会耗很大的精力:如果使用LiquiBase对数据库进行管理,那么就 ...