ukelli-ui
Version:
[](https://travis-ci.org/ukelli/ukelli-ui) [](https://packagephobia.now.sh/result?p=ukelli-ui)
182 lines (154 loc) • 7.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
var CountdownBg =
/*#__PURE__*/
function (_PureComponent) {
_inherits(CountdownBg, _PureComponent);
function CountdownBg() {
_classCallCheck(this, CountdownBg);
return _possibleConstructorReturn(this, _getPrototypeOf(CountdownBg).apply(this, arguments));
}
_createClass(CountdownBg, [{
key: "componentDidMount",
value: function componentDidMount() {
var id = this.props.id;
var canvas = document.querySelector('#countDown' + id);
var ctx = canvas.getContext('2d');
var lineWidth = 3;
var canvasW = canvas.width;
var circleX = canvasW / 2;
var circleY = circleX;
var radius = (canvasW - lineWidth) / 2;
this.animationTime = 200; // ms
this.canvasInfo = {
ctx: ctx,
circleX: circleX,
circleY: circleY,
radius: radius,
lineWidth: lineWidth
};
this.draw();
}
}, {
key: "componentWillReceiveProps",
value: function componentWillReceiveProps(nextProps) {
var self = this;
var currPercent = self.props.percent;
if (this.props.text !== nextProps.text) {
if (!!self.timer) clearInterval(self.timer);
self.timer = setInterval(function () {
var nextPercent = nextProps.percent;
var unit = (nextPercent - currPercent) / 20;
if (!nextPercent || currPercent >= nextPercent) return clearInterval(self.timer);
currPercent += unit < 1 ? 1 : unit;
self.draw(currPercent || 0);
}, 20);
}
}
}, {
key: "circle",
value: function circle(cx, cy, r) {
var _this$canvasInfo = this.canvasInfo,
ctx = _this$canvasInfo.ctx,
circleX = _this$canvasInfo.circleX,
circleY = _this$canvasInfo.circleY,
radius = _this$canvasInfo.radius,
lineWidth = _this$canvasInfo.lineWidth;
ctx.beginPath();
ctx.moveTo(cx + r, cy);
ctx.lineWidth = lineWidth;
ctx.strokeStyle = '#eee';
ctx.arc(cx, cy, r, 0, Math.PI * 2);
ctx.closePath();
ctx.stroke();
}
}, {
key: "sector",
value: function sector(cx, cy, r, startAngle, endAngle, anti) {
var _this$canvasInfo2 = this.canvasInfo,
ctx = _this$canvasInfo2.ctx,
circleX = _this$canvasInfo2.circleX,
circleY = _this$canvasInfo2.circleY,
radius = _this$canvasInfo2.radius,
lineWidth = _this$canvasInfo2.lineWidth;
ctx.beginPath();
ctx.moveTo(cx, cy + r); // 从圆形底部开始画
ctx.lineWidth = lineWidth; // 渐变色 - 可自定义
var linGrad = ctx.createLinearGradient(circleX, circleY - radius - lineWidth, circleX, circleY + radius + lineWidth);
linGrad.addColorStop(0.0, '#fe0362');
linGrad.addColorStop(1.0, '#7473e3');
ctx.strokeStyle = linGrad; // 圆弧两端的样式
ctx.lineCap = 'round'; // 圆弧
ctx.arc(cx, cy, r, startAngle * (Math.PI / 180.0) + Math.PI / 2, endAngle * (Math.PI / 180.0) + Math.PI / 2, !!anti);
ctx.stroke();
}
}, {
key: "draw",
value: function draw(nextPercent) {
var _this$canvasInfo3 = this.canvasInfo,
ctx = _this$canvasInfo3.ctx,
circleX = _this$canvasInfo3.circleX,
circleY = _this$canvasInfo3.circleY,
radius = _this$canvasInfo3.radius,
lineWidth = _this$canvasInfo3.lineWidth,
fontSize = _this$canvasInfo3.fontSize;
var _this$props = this.props,
percent = _this$props.percent,
text = _this$props.text;
var self = this; // 清除canvas内容
ctx.clearRect(0, 0, circleX * 2, circleY * 2);
this.circle(circleX, circleY, radius); // 中间的字
// ctx.font = fontSize + 'px Arial, "Microsoft YaHei", "微软雅黑"';
// ctx.textAlign = 'center';
// ctx.textBaseline = 'middle';
// ctx.fillStyle = '#999';
// ctx.fillText(text, circleX, circleY);
// 圆形
// 圆弧
this.sector(circleX, circleY, radius, 0, nextPercent / 100 * 360); // 控制结束时动画的速度
// if (process / percent > 0.90) {
// process += 0.30;
// } else if (process / percent > 0.80) {
// process += 0.55;
// } else if (process / percent > 0.70) {
// process += 0.75;
// } else {
// process += 1.0;
// }
}
}, {
key: "render",
value: function render() {
var id = this.props.id;
return _react.default.createElement("canvas", {
id: "countDown" + id,
className: "countdown-bg",
width: "60",
height: "60"
});
}
}]);
return CountdownBg;
}(_react.PureComponent);
exports.default = CountdownBg;
CountdownBg.propTypes = {
id: _propTypes.default.any.isRequired,
percent: _propTypes.default.any.isRequired,
text: _propTypes.default.any.isRequired
};