@nicecode/funny
Version:
funny,一些有趣的代码,funny code
134 lines • 5.91 kB
JavaScript
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
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, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
// @ts-nocheck
/**
* 星空初始化
*/
var NightSky = /*#__PURE__*/function () {
function NightSky(opt) {
_classCallCheck(this, NightSky);
_defineProperty(this, "opt", void 0);
_defineProperty(this, "ctx", void 0);
_defineProperty(this, "starList", void 0);
this.opt = _objectSpread({
width: 500,
height: 500,
num: 120,
canvas: null
}, opt);
this.opt.canvas.width = this.opt.width;
this.opt.canvas.height = this.opt.height;
this.ctx = this.opt.canvas && this.opt.canvas.getContext('2d');
this.opt.canvas.style.backgroundColor = '#000';
this.starList = [];
this.draw = this.draw;
this.init();
}
_createClass(NightSky, [{
key: "init",
value: function init() {
this.drawStar();
this.animate();
}
}, {
key: "drawStar",
value: function drawStar() {
var _this$opt = this.opt,
width = _this$opt.width,
height = _this$opt.height,
num = _this$opt.num;
for (var i = 0; i < num; i++) {
this.starList[i] = new Star({
maxRadius: 3,
ctx: this.ctx,
width: width,
height: height
});
this.starList[i].draw();
}
}
}, {
key: "animate",
value: function animate() {
var ctx = this.ctx;
var starList = this.starList;
var _this$opt2 = this.opt,
width = _this$opt2.width,
height = _this$opt2.height;
function _move() {
ctx.clearRect(0, 0, width, height);
for (var i in starList) {
starList[i].move();
}
window.requestAnimationFrame(_move);
}
window.requestAnimationFrame(_move);
}
}, {
key: "draw",
value: function draw(val) {
return val;
}
}]);
return NightSky;
}();
var Star = /*#__PURE__*/function () {
function Star(opt) {
_classCallCheck(this, Star);
_defineProperty(this, "ctx", void 0);
_defineProperty(this, "x", void 0);
_defineProperty(this, "y", void 0);
_defineProperty(this, "height", void 0);
_defineProperty(this, "width", void 0);
_defineProperty(this, "speed", void 0);
_defineProperty(this, "maxRadius", void 0);
_defineProperty(this, "r", void 0);
_defineProperty(this, "color", void 0);
var width = opt.width,
height = opt.height,
_opt$maxRadius = opt.maxRadius,
maxRadius = _opt$maxRadius === void 0 ? 2 : _opt$maxRadius,
ctx = opt.ctx,
_opt$speed = opt.speed,
speed = _opt$speed === void 0 ? 0.5 : _opt$speed;
this.x = Math.random() * width;
this.y = Math.random() * height;
this.height = height;
this.width = width;
this.speed = speed;
this.maxRadius = maxRadius;
this.ctx = ctx;
this.r = Math.random() * maxRadius;
var alpha = (Math.floor(Math.random() * 10) + 1) / 10;
this.color = "rgba(255, 255, 255, ".concat(alpha, ")");
}
_createClass(Star, [{
key: "draw",
value: function draw() {
this.ctx.fillStyle = this.color;
this.ctx.shadowBlur = this.r * 2;
this.ctx.beginPath();
this.ctx.arc(this.x, this.y, this.r * Math.random(), 0, 2 * Math.PI, false);
this.ctx.closePath();
this.ctx.fill();
}
}, {
key: "move",
value: function move() {
this.y -= this.speed;
if (this.y <= -10) {
this.y = this.height + 10;
}
this.draw();
}
}]);
return Star;
}();
export default NightSky;