| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- import { TimeFormatter } from "@/utils/time_format";
- import { View } from "@tarojs/components";
- import Taro from "@tarojs/taro";
- import { useRef } from "react";
- import Svg, { Circle, Path } from 'react-native-svg';
- import { useSelector } from "react-redux";
- export type RingCommon = {
- useCase: string;
- status: string;
- isFast?: boolean;
- radius: number;
- lineWidth: number;
- }
- export type CurrentDot = {
- color: string;
- lineWidth: number;
- borderColor: string;
- timestamp?: number;
- }
- export type RealRing = {
- color: string;
- startArc: number;
- durationArc: number;
- }
- export type TargetRing = {
- color: string;
- startArc: number;
- durationArc: number;
- }
- export type BgRing = {
- color: string;
- }
- export default function Component(props: {
- common: RingCommon; currentDot?: CurrentDot;
- realRing?: RealRing; targetRing?: TargetRing;
- breathAnimation?: boolean;
- bgRing: BgRing; canvasId?: string;
- ctx?: any; setCtx?: any;
- canvas?: any; setCanvas?: any;
- dotList?: Array<CurrentDot>;
- stageList?: Array<RealRing>;
- }) {
- const r = props.common.radius
- const strokeWidth = props.common.lineWidth;
- // const color = props.color || 'orange'
- const canvasRef = useRef(null);
- const canvasId = props.canvasId ? 'canvas_' + props.canvasId : 'progress-canvas';
- const dpr = Taro.getSystemInfoSync().pixelRatio; // 获取设备的像素比
- const radius = r; // 圆形进度条的半径
- const lineWidth = strokeWidth; // 圆形进度条的线宽
- const timeObj = useSelector((state: any) => state.time);
- const user = useSelector((state: any) => state.user);
- const center = radius + lineWidth / 2 + 3; // 圆心坐标
- function drawContent() {
- var time = new Date();
- // if (timeObj.status != 'WAIT_FOR_START' && timeObj && (timeObj.fast || timeObj.sleep)) {
- // var timestamp = time.getTime()
- // //判断逻辑
- // if (timeObj.fast.real_start_time_zone || timeObj.sleep.real_start_time_zone) {
- // timestamp = TimeFormatter.transferTimestamp(timestamp, timeObj.fast.real_start_time_zone ? timeObj.fast.real_start_time_zone : timeObj.sleep.real_start_time_zone)
- // time = new Date(timestamp)
- // }
- // }
- var seconds = time.getHours() * 3600 + time.getMinutes() * 60 + time.getSeconds();
- var arc = seconds / 86400 * 2 * Math.PI - Math.PI / 2.0;
- return <Svg width={(radius * 2 + lineWidth) + 6} height={(radius * 2 + lineWidth) + 6} fillOpacity={0}>
- <Circle
- cx={center}
- cy={center}
- r={radius}
- fillOpacity={0}
- stroke={props.bgRing.color}
- strokeWidth={strokeWidth}
- strokeLinecap="round"
- />
- {/* // 绘制target进度环
- if (props.targetRing) {
- ctx.beginPath();
- ctx.arc(center, center, radius, props.targetRing!.startArc,
- props.targetRing!.startArc + props.targetRing!.durationArc);
- ctx.lineWidth = lineWidth;
- ctx.strokeStyle = props.targetRing!.color;
- ctx.lineCap = 'round'; // 设置为圆角
- ctx.stroke();
- } */}
- {
- props.targetRing && <Circle
- cx={center}
- cy={center}
- r={radius}
- fillOpacity={0}
- stroke={props.targetRing!.color}
- transform={`rotate(${props.targetRing!.startArc / (2 * Math.PI) * 360} ${center} ${center})`}
- strokeDasharray={`${props.targetRing!.durationArc * radius},1000`}
- strokeWidth={lineWidth}
- strokeLinecap="round"
- />
- }
- {/* //绘制real进度环
- if (props.realRing) {
- if (props.realRing.durationArc <0.01) props.realRing.durationArc=0.01;
- ctx.beginPath();
- ctx.arc(center, center, radius, props.realRing!.startArc,
- props.realRing!.startArc + props.realRing!.durationArc);
- ctx.lineWidth = lineWidth;
- ctx.strokeStyle = props.realRing!.color;
- ctx.lineCap = 'round'; // 设置为圆角
- ctx.stroke();
- } */}
- {
- props.realRing && <Circle
- cx={center}
- cy={center}
- r={radius}
- fillOpacity={0}
- stroke={props.realRing!.color}
- transform={`rotate(${props.realRing!.startArc / (2 * Math.PI) * 360} ${center} ${center})`}
- strokeDasharray={`${props.realRing!.durationArc * radius},1000`}
- strokeWidth={lineWidth}
- strokeLinecap="round"
- />
- }
- {
- props.stageList && props.stageList.map((item) => {
- return <Circle
- cx={center}
- cy={center}
- r={radius}
- fillOpacity={0}
- stroke={item.color}
- transform={`rotate(${item.startArc / (2 * Math.PI) * 360} ${center} ${center})`}
- strokeDasharray={`${item.durationArc * radius},1000`}
- strokeWidth={lineWidth}
- strokeLinecap="round"
- />
- })
- }
- {
- props.currentDot && <Circle
- cx={center}
- cy={center}
- r={radius}
- fillOpacity={0}
- stroke={props.currentDot!.borderColor}
- transform={`rotate(${arc / (2 * Math.PI) * 360} ${center} ${center})`}
- strokeDasharray={`0,1000`}
- strokeWidth={lineWidth + 4}
- strokeLinecap="round"
- />
- }
-
- {
- props.currentDot && <Circle
- cx={center}
- cy={center}
- r={radius}
- fillOpacity={0}
- stroke={props.currentDot!.color}
- transform={`rotate(${arc / (2 * Math.PI) * 360} ${center} ${center})`}
- strokeDasharray={`0,1000`}
- strokeWidth={lineWidth}
- strokeLinecap="round"
- />
- }
- {
- props.dotList && props.dotList.map(item => {
- var time1 = item.timestamp ? new Date(item.timestamp) : new Date();
- var seconds1 = time1.getHours() * 3600 + time1.getMinutes() * 60 + time1.getSeconds();
- var arc1 = seconds1 / 86400 * 2 * Math.PI - Math.PI / 2.0;
- return <Circle
- cx={center}
- cy={center}
- r={radius}
- fillOpacity={0}
- stroke={item.borderColor}
- transform={`rotate(${arc1 / (2 * Math.PI) * 360} ${center} ${center})`}
- strokeDasharray={`0,1000`}
- strokeWidth={lineWidth + 6}
- strokeLinecap="round"
- />
- })
- }
- {
- props.dotList && props.dotList.map(item => {
- var time1 = item.timestamp ? new Date(item.timestamp) : new Date();
- var seconds1 = time1.getHours() * 3600 + time1.getMinutes() * 60 + time1.getSeconds();
- var arc1 = seconds1 / 86400 * 2 * Math.PI - Math.PI / 2.0;
- return <Circle
- cx={center}
- cy={center}
- r={radius}
- fillOpacity={0}
- stroke={item.color}
- transform={`rotate(${arc1 / (2 * Math.PI) * 360} ${center} ${center})`}
- strokeDasharray={`0,1000`}
- strokeWidth={lineWidth}
- strokeLinecap="round"
- />
- })
- }
- </Svg>
- }
- return <Svg width={(radius * 2 + lineWidth) + 6} height={(radius * 2 + lineWidth) + 6} fillOpacity={0}>
- {
- drawContent()
- }
- </Svg>
- // return <View style={{ width: 100, height: 100, backgroundColor: 'pink' }}></View>
- }
|