UNPKG

@hackoregon/component-library

Version:

Official repo for Hack Oregon React component library

161 lines (128 loc) 6.23 kB
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); } function _templateObject() { var data = _taggedTemplateLiteral(["\n position: fixed;\n"]); _templateObject = function _templateObject() { return data; }; return data; } function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); } import React from "react"; import { css } from "emotion"; import window from "global/window"; import { get, has } from "lodash"; var canvasStyles = css(_templateObject()); var CanvasParticles = function (_React$Component) { _inherits(CanvasParticles, _React$Component); function CanvasParticles() { _classCallCheck(this, CanvasParticles); return _possibleConstructorReturn(this, _getPrototypeOf(CanvasParticles).apply(this, arguments)); } _createClass(CanvasParticles, [{ key: "componentDidMount", value: function componentDidMount() { window.requestAnimFrame = function () { return get(window, "requestAnimationFrame") || get(window, "webkitRequestAnimationFrame") || get(window, "mozRequestAnimationFrame") || get(window, "oRequestAnimationFrame") || get(window, "msRequestAnimationFrame") || function (callback) { has(window, "setTimeout") && window.setTimeout(callback, 1000 / 60); }; }(); var canvas = this.refs.canvas; var ctx = canvas.getContext("2d"); var img = this.refs.image; var W = get(window, "innerWidth", 1000); var H = get(window, "innerHeight", 1000); canvas.width = W * 1.2; canvas.height = H * 1.2; var particleCount = 40; var particles = []; var minDist = 100; function paintCanvas() { ctx.fillStyle = "rgba(255, 255, 255, 1)"; ctx.fillRect(0, 0, W, H); } function Particle() { this.x = Math.random() * W; this.y = Math.random() * H; this.vx = -1 + Math.random() * 2; this.vy = -1 + Math.random() * 2; this.radius = 4; this.draw = function () { ctx.fillStyle = "lightgrey"; ctx.beginPath(); ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false); ctx.fill(); }; } for (var i = 0; i < particleCount; i++) { particles.push(new Particle()); } function draw() { paintCanvas(); for (var _i = 0; _i < particles.length; _i++) { var p = particles[_i]; p.draw(); } update(); } function update() { for (var _i2 = 0; _i2 < particles.length; _i2++) { var p = particles[_i2]; p.x += p.vx; p.y += p.vy; if (p.x + p.radius > W) p.x = p.radius;else if (p.x - p.radius < 0) { p.x = W - p.radius; } if (p.y + p.radius > H) p.y = p.radius;else if (p.y - p.radius < 0) { p.y = H - p.radius; } for (var j = _i2 + 1; j < particles.length; j++) { var p2 = particles[j]; distance(p, p2); } } } function distance(p1, p2) { var dx = p1.x - p2.x; var dy = p1.y - p2.y; var dist = Math.sqrt(dx * dx + dy * dy); if (dist <= minDist) { ctx.beginPath(); ctx.strokeStyle = "rgba(239,74,93,".concat(1.0 - dist / minDist, ")"); ctx.moveTo(p1.x, p1.y); ctx.lineTo(p2.x, p2.y); ctx.stroke(); ctx.closePath(); var ax = dx / 200000; var ay = dy / 200000; p1.vx -= ax; p1.vy -= ay; p2.vx += ax; p2.vy += ay; } } function animloop() { draw(); requestAnimFrame(animloop); } animloop(); } }, { key: "render", value: function render() { return React.createElement("div", null, React.createElement("canvas", { ref: "canvas", className: canvasStyles })); } }]); return CanvasParticles; }(React.Component); export default CanvasParticles;