UNPKG

lulouis-vant

Version:

Lightweight Mobile UI Components built on Vue

138 lines (127 loc) 3.17 kB
"use strict"; exports.__esModule = true; exports.default = void 0; var _utils = require("../utils"); var _raf = require("../utils/raf"); var _color = require("../utils/color"); var _use = (0, _utils.use)('circle'), sfc = _use[0], bem = _use[1]; var PERIMETER = 3140; var PATH = 'M 530 530 m -500, 0 a 500, 500 0 1, 1 1000, 0 a 500, 500 0 1, 1 -1000, 0'; function format(rate) { return Math.min(Math.max(rate, 0), 100); } var _default = sfc({ props: { text: String, value: Number, speed: Number, size: { type: String, default: '100px' }, fill: { type: String, default: 'none' }, rate: { type: Number, default: 100 }, layerColor: { type: String, default: _color.WHITE }, color: { type: String, default: _color.BLUE }, strokeWidth: { type: Number, default: 40 }, clockwise: { type: Boolean, default: true } }, computed: { style: function style() { return { width: this.size, height: this.size }; }, layerStyle: function layerStyle() { var offset = PERIMETER * (100 - this.value) / 100; offset = this.clockwise ? offset : PERIMETER * 2 - offset; return { stroke: "" + this.color, strokeDashoffset: offset + "px", strokeWidth: this.strokeWidth + 1 + "px" }; }, hoverStyle: function hoverStyle() { return { fill: "" + this.fill, stroke: "" + this.layerColor, strokeWidth: this.strokeWidth + "px" }; } }, watch: { rate: { handler: function handler() { this.startTime = Date.now(); this.startRate = this.value; this.endRate = format(this.rate); this.increase = this.endRate > this.startRate; this.duration = Math.abs((this.startRate - this.endRate) * 1000 / this.speed); if (this.speed) { (0, _raf.cancel)(this.rafId); this.rafId = (0, _raf.raf)(this.animate); } else { this.$emit('input', this.endRate); } }, immediate: true } }, methods: { animate: function animate() { var now = Date.now(); var progress = Math.min((now - this.startTime) / this.duration, 1); var rate = progress * (this.endRate - this.startRate) + this.startRate; this.$emit('input', format(parseFloat(rate.toFixed(1)))); if (this.increase ? rate < this.endRate : rate > this.endRate) { this.rafId = (0, _raf.raf)(this.animate); } } }, render: function render(h) { return h("div", { "class": bem(), "style": this.style }, [h("svg", { "attrs": { "viewBox": "0 0 1060 1060" } }, [h("path", { "class": bem('hover'), "style": this.hoverStyle, "attrs": { "d": PATH } }), h("path", { "class": bem('layer'), "style": this.layerStyle, "attrs": { "d": PATH } })]), this.$slots.default || this.text && h("div", { "class": bem('text') }, [this.text])]); } }); exports.default = _default;