import { createNamespace, isObj, addUnit } from '../utils';
import { raf, cancelRaf } from '../utils/dom/raf';
import { BLUE, WHITE } from '../utils/constant';
const [createComponent, bem] = createNamespace('circle');
const PERIMETER = 3140;
let uid = 0;
function format(rate) {
return Math.min(Math.max(rate, 0), 100);
}
function getPath(clockwise, viewBoxSize) {
const sweepFlag = clockwise ? 1 : 0;
return `M ${viewBoxSize / 2} ${viewBoxSize / 2} m 0, -500 a 500, 500 0 1, ${sweepFlag} 0, 1000 a 500, 500 0 1, ${sweepFlag} 0, -1000`;
}
export default createComponent({
props: {
text: String,
value: {
type: Number,
default: 0
},
speed: {
type: Number,
default: 0
},
size: {
type: [String, Number],
default: 100
},
fill: {
type: String,
default: 'none'
},
rate: {
type: Number,
default: 100
},
layerColor: {
type: String,
default: WHITE
},
color: {
type: [String, Object],
default: BLUE
},
strokeWidth: {
type: Number,
default: 40
},
clockwise: {
type: Boolean,
default: true
}
},
beforeCreate() {
this.uid = `van-circle-gradient-${uid++}`;
},
computed: {
style() {
const size = addUnit(this.size);
return {
width: size,
height: size
};
},
path() {
return getPath(this.clockwise, this.viewBoxSize);
},
viewBoxSize() {
return 1000 + this.strokeWidth;
},
layerStyle() {
const offset = (PERIMETER * this.value) / 100;
return {
stroke: `${this.color}`,
strokeWidth: `${this.strokeWidth + 1}px`,
strokeDasharray: `${offset}px ${PERIMETER}px`
};
},
hoverStyle() {
return {
fill: `${this.fill}`,
stroke: `${this.layerColor}`,
strokeWidth: `${this.strokeWidth}px`
};
},
gradient() {
return isObj(this.color);
},
LinearGradient() {
if (!this.gradient) {
return;
}
const Stops = Object.keys(this.color)
.sort((a, b) => parseFloat(a) - parseFloat(b))
.map((key, index) => (