SlidngScale.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. import { ScrollView, View, Text, Image } from "@tarojs/components";
  2. import VirtualList from '@tarojs/components/virtual-list';
  3. import './SlidngScale.scss'
  4. import { useEffect, useRef, useState } from "react";
  5. import Taro from "@tarojs/taro";
  6. import { rpxToPx } from "@/utils/tools";
  7. import { throttle } from 'lodash';
  8. // import { SvgXml } from "react-native-svg";
  9. var timerA = null
  10. var lastValue = 0
  11. let SvgXml;
  12. if (process.env.TARO_ENV == 'rn') {
  13. SvgXml = require("react-native-svg").SvgXml
  14. }
  15. export default function Component(props: {
  16. step: number, min: number,
  17. max: number, default_value: number,
  18. scale: number,
  19. changed: Function, unit: string
  20. themeColor: string,
  21. updateStatus: Function
  22. }) {
  23. const scrollViewRef = useRef<any>();
  24. const minNum: number = props.min;//props.step < 1 ?props.default_value-2:props.default_value-20//
  25. const maxNum: number = props.max;//props.step < 1 ?props.default_value+2:props.default_value+20//
  26. const min = props.min//(props.default_value - props.step * 20) < props.min ? props.min : props.default_value - props.step * 20
  27. const max = props.max//(props.default_value + props.step * 20) > props.max ? props.max : props.default_value + props.step * 20
  28. // const [minNum, setMinNum] = useState(min)
  29. // const [maxNum, setMaxNum] = useState(max)
  30. const stepNum: number = props.step * 10;
  31. const jingdu: number = props.step < 0.1 ? 100 : props.step < 1 ? 10 : 1;
  32. const list: any[] = [];
  33. // const [list, setList] = useState([])
  34. const slidngWidth = 10
  35. const [current, setCurrent] = useState(props.default_value)
  36. const [left, setLeft] = useState((props.default_value - min) * slidngWidth / props.step);
  37. const [isDraging, setIsDraging] = useState(false)
  38. const [isEnd, setIsEnd] = useState(true)
  39. const [enableText, setEnableText] = useState(true)
  40. for (var i: number = minNum; i <= maxNum; i += props.step) {
  41. var value: number = parseFloat(i.toFixed(props.scale == 0 ? 1 : props.scale));
  42. var isBig: boolean = Math.round((value - minNum) * jingdu) % Math.round(stepNum * jingdu) == 0;
  43. var isMiddle: boolean = (Math.round((value - minNum) * jingdu) * 2) % Math.round(stepNum * jingdu) == 0;
  44. list.push({
  45. value: props.step < 1 ? value : Math.round(value),
  46. showBig: isBig && i >= minNum && i <= maxNum,
  47. showMiddle: isMiddle && !isBig && i >= minNum && i <= maxNum
  48. })
  49. }
  50. // const [showScrollView, setShowScrollView] = useState(false)
  51. useEffect(() => {
  52. // var array: any = []
  53. // for (var i: number = min; i <= max; i += props.step) {
  54. // var value: number = parseFloat(i.toFixed(props.scale));
  55. // var isBig: boolean = Math.round((value - min) * jingdu) % Math.round(stepNum * jingdu) == 0;
  56. // var isMiddle: boolean = (Math.round((value - min) * jingdu) * 2) % Math.round(stepNum * jingdu) == 0;
  57. // array.push({
  58. // value: props.step < 1 ? value : Math.round(value),
  59. // showBig: isBig && i >= min && i <= max,
  60. // showMiddle: isMiddle && !isBig && i >= min && i <= max
  61. // })
  62. // }
  63. // setList(array)
  64. // setTimeout(() => {
  65. // var min2 = props.min
  66. // var max2 = props.max
  67. // setMinNum(min2)
  68. // setMaxNum(max2)
  69. // setLeft((props.default_value - min2) * slidngWidth / props.step)
  70. // var array2: any = []
  71. // for (var i: number = min2; i <= max2; i += props.step) {
  72. // var value: number = parseFloat(i.toFixed(1));
  73. // var isBig: boolean = Math.round((value - min2) * jingdu) % Math.round(stepNum * jingdu) == 0;
  74. // var isMiddle: boolean = (Math.round((value - min2) * jingdu) * 2) % Math.round(stepNum * jingdu) == 0;
  75. // array2.push({
  76. // value: props.step < 1 ? value : Math.round(value),
  77. // showBig: isBig && i >= min2 && i <= max2,
  78. // showMiddle: isMiddle && !isBig && i >= min2 && i <= max2
  79. // })
  80. // }
  81. // setList(array2)
  82. // }, 50)
  83. }, [])
  84. useEffect(() => {
  85. if (process.env.TARO_ENV == 'weapp') {
  86. if (isEnd && !isDraging) {
  87. props.updateStatus(true)
  88. setEnableText(true)
  89. console.log('true')
  90. }
  91. else {
  92. props.updateStatus(false)
  93. setEnableText(false)
  94. console.log('false')
  95. }
  96. }
  97. }, [isEnd, isDraging])
  98. function scrollContent(e) {
  99. if (process.env.TARO_ENV == 'rn') {
  100. update(e)
  101. setIsDraging(false)
  102. setIsEnd(true)
  103. return
  104. }
  105. lastValue = e.detail.scrollLeft;
  106. if (timerA)
  107. clearTimeout(timerA);
  108. timerA = setTimeout(() => {
  109. update(e)
  110. setIsDraging(false)
  111. }, 250) as any
  112. }
  113. const handleScroll = throttle((e) => {
  114. console.log(e)
  115. update(e)
  116. }, 500);
  117. function touchEnd() {
  118. setIsEnd(true)
  119. var temp = lastValue
  120. setTimeout(() => {
  121. if (temp == lastValue) {
  122. setIsDraging(false)
  123. }
  124. }, 250)
  125. }
  126. function touchStart() {
  127. if (process.env.TARO_ENV == 'weapp') {
  128. props.updateStatus(false)
  129. setEnableText(false)
  130. }
  131. }
  132. function dragStart(e) {
  133. lastValue = e.detail.scrollLeft;
  134. setIsEnd(false)
  135. setIsDraging(true)
  136. }
  137. function dragEnd(e) {
  138. setIsEnd(true)
  139. console.log('end')
  140. // setIsDraging(false)
  141. }
  142. function update(e) {
  143. const { scrollLeft } = e.detail;
  144. var count = scrollLeft / slidngWidth;
  145. var strValue = (minNum + Math.round(count) * props.step).toFixed(props.scale == 0 ? 1 : props.scale);
  146. if ((strValue as any) < minNum) {
  147. strValue = minNum as any;
  148. }
  149. if ((strValue as any) > maxNum) {
  150. strValue = maxNum as any;
  151. }
  152. if (parseFloat(strValue) != current) {
  153. var data = strValue as any;
  154. if (props.step < 1) {
  155. data = parseFloat(strValue).toFixed(props.scale == 0 ? 1 : props.scale);
  156. if (data.indexOf('.') > 0) {
  157. const regexp = /(?:\.0*|(\.\d+?)0+)$/
  158. data = data.replace(regexp, '$1')
  159. }
  160. }
  161. else {
  162. data = Math.round(data);
  163. }
  164. setCurrent(data);
  165. props.changed(data);
  166. }
  167. }
  168. const scrollEnd = (e) => {
  169. console.log(e, 'end')
  170. // const { scrollLeft } = e.detail;
  171. // setLeft(scrollLeft)
  172. }
  173. function itemContent(data) {
  174. var item = list[data.index]
  175. var index = data.index
  176. return <View style={{ width: 2, height: 100, backgroundColor: 'red', marginRight: 8 }}></View>
  177. // return <View className={item.showBig ? 'slidng_item_big' : item.showMiddle ? 'slidng_item_middle' : 'slidng_item'} style={{ width: 2, marginRight: 8, backgroundColor: props.themeColor, zIndex: 0 }} key={index}>
  178. // {
  179. // item.showBig ? <Text className="slidng_text">{item.value}</Text> : null
  180. // }
  181. // </View>
  182. }
  183. // var svg2 = `<svg xmlns="http://www.w3.org/2000/svg" width="${list.length * 10 - 8}" height="50">`;
  184. // for (var i = 0; i < list.length; i++) {
  185. // var obj = list[i];
  186. // svg2 += `<line x1="${i * 10}" y1="0" x2="${i * 10}" y2="${obj.showBig ? rpxToPx(28) : obj.showMiddle ? rpxToPx(24) : rpxToPx(16)}" stroke="${props.themeColor}" stroke-width="2"/>`
  187. // if (obj.showBig) {
  188. // svg2 += `<text x="${i * 10}" y="${rpxToPx(30 + 36)}" text-anchor="middle" fill="white" font-size="${rpxToPx(36)}">${obj.value}</text>`
  189. // }
  190. // }
  191. // svg2 += `</svg>`
  192. // console.log(svg2)
  193. // debugger
  194. var svgLine = `<svg xmlns="http://www.w3.org/2000/svg" width="100" height="${rpxToPx(28)}">`;
  195. var svgNumber = `<svg xmlns="http://www.w3.org/2000/svg" width="${list.length * 10 - 8 + 60}" height="${rpxToPx(38)}">`;
  196. for (var i = 0; i < 10; i++) {
  197. var obj = list[i];
  198. svgLine += `<line x1="${i * 10 + 1}" y1="0" x2="${i * 10 + 1}" y2="${obj.showBig ? rpxToPx(28) : obj.showMiddle ? rpxToPx(24) : rpxToPx(16)}" stroke="${props.themeColor}" stroke-width="2"/>`
  199. }
  200. for (var i = 0; i < list.length; i++) {
  201. var obj = list[i];
  202. if (obj.showBig) {
  203. //字体设置参考 https://segmentfault.com/a/1190000006110417
  204. svgNumber += `<text x="${i * 10 + 1 + 30}" y="${rpxToPx(2 + 36)}" text-anchor="middle" fill="white" font-family="PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei" font-size="${rpxToPx(36)}">${obj.value}</text>`
  205. }
  206. }
  207. svgLine += `</svg>`
  208. svgNumber += `</svg>`
  209. var basestr = encodeURIComponent(svgLine)
  210. var imgLines = `url("data:image/svg+xml,${basestr}");`
  211. var basestr2 = encodeURIComponent(svgNumber)
  212. var imgNum = `url("data:image/svg+xml,${basestr2}");`
  213. var svgLine2 = `<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">`
  214. svgLine2 += `<pattern id="pattern" width="100" height="${rpxToPx(28)}" patternUnits="userSpaceOnUse">`
  215. for (var i = 0; i < 10; i++) {
  216. var obj = list[i];
  217. svgLine2 += `<line x1="${i * 10 + 1}" y1="0" x2="${i * 10 + 1}" y2="${obj.showBig ? rpxToPx(28) : obj.showMiddle ? rpxToPx(24) : rpxToPx(16)}" stroke="${props.themeColor}" stroke-width="2"/>`
  218. }
  219. svgLine2 += `</pattern>
  220. <rect width="100%" height="100%" fill="url(#pattern)" />
  221. </svg>`
  222. const isLeonTest = true
  223. return <View className="slidng">
  224. <View className="number_bg" style={{ opacity: enableText ? 1 : 0.4 }}>
  225. {
  226. process.env.TARO_ENV == 'weapp' ? <Text className="number" style={{ fontFamily: "PingFang SC,Helvetica Neue, Helvetica, Arial, Hiragino Sans GB, Heiti SC, Microsoft YaHei, WenQuanYi Micro Hei, sans-serif;" }}>{current}</Text> :
  227. <Text className="number">{current}</Text>
  228. }
  229. <Text className="unit">{props.unit}</Text>
  230. </View>
  231. <View className="scroll_bg">
  232. <View className="shadow_top" />
  233. <View className="shadow_left" />
  234. <View className="shadow_right" />
  235. <View className="top_line" style={{ backgroundColor: props.themeColor }} />
  236. <ScrollView
  237. style={{ zIndex: 0, position: 'relative' }} ref={scrollViewRef}
  238. showsHorizontalScrollIndicator={false}
  239. scrollX scrollLeft={left} className="scroll"
  240. enablePassive={true}
  241. fastDeceleration={true}
  242. onScrollEnd={scrollEnd}
  243. onDragEnd={dragEnd}
  244. onTouchStart={touchStart}
  245. onTouchEnd={touchEnd}
  246. onDragStart={dragStart}
  247. onScroll={scrollContent}
  248. enhanced
  249. >
  250. <View className="scrollContent">
  251. <View className="scrollPadding" style={{ width: rpxToPx(372) }} />
  252. <View className="content">
  253. {
  254. !isLeonTest && list.map((item, index) => {
  255. return <View className={(item as any).showBig ? 'slidng_item_big' : (item as any).showMiddle ? 'slidng_item_middle' : 'slidng_item'}
  256. style={{ width: 2, marginRight: list.length - 1 == index ? 0 : 8, backgroundColor: props.themeColor, zIndex: 0 }} key={index}>
  257. {
  258. (item as any).showBig ? <Text className="slidng_text">{(item as any).value}</Text> : null
  259. }
  260. </View>
  261. })
  262. }
  263. {
  264. isLeonTest && <View>
  265. <View style={{ width: list.length * 10 - 8, height: 100 }}>
  266. {
  267. process.env.TARO_ENV == 'weapp' && <View style={{ width: list.length * 10 - 8, height: rpxToPx(28), backgroundImage: imgLines }} />
  268. }
  269. {
  270. process.env.TARO_ENV == 'rn' && <SvgXml xml={svgLine2} width={list.length * 10 - 8} height={rpxToPx(28)} />
  271. }
  272. {process.env.TARO_ENV == 'weapp' &&
  273. <View style={{ width: list.length * 10 - 8 + 60, marginLeft: -30, height: rpxToPx(38), backgroundImage: imgNum }} />}
  274. {
  275. process.env.TARO_ENV == 'rn' &&
  276. <View style={{ flexDirection: 'row', marginLeft: -30 }}>
  277. <SvgXml xml={svgNumber} width={list.length * 10 - 8 + 60} height={rpxToPx(38)} />
  278. </View>
  279. }
  280. </View>
  281. </View>
  282. }
  283. </View>
  284. <View className="scrollPadding" />
  285. </View>
  286. </ScrollView>
  287. {/* <VirtualList height={rpxToPx(300)} width={rpxToPx(750)} onScroll={handleScroll} layout="horizontal"
  288. initialScrollOffset={left}
  289. itemCount={list.length}
  290. item={itemContent}
  291. itemSize={10}
  292. itemData={list}
  293. /> */}
  294. <Image className="center_line" src={require('@assets/images/scale_center.png')} />
  295. {/* <View className="center_line" /> */}
  296. </View>
  297. </View>
  298. }