From ae9b4423a4a4e2e14966081bf20798934b3cc861 Mon Sep 17 00:00:00 2001 From: keyan Date: Fri, 16 Jul 2021 11:47:18 -0500 Subject: [PATCH] use rand lightning animation when not logged in --- components/header.js | 7 +++++++ components/lightning.js | 36 +++++++++++++++++++++--------------- lib/rand.js | 3 +++ pages/_app.js | 11 +++++++---- 4 files changed, 38 insertions(+), 19 deletions(-) create mode 100644 lib/rand.js diff --git a/components/header.js b/components/header.js index 7d674432..aae1ffa3 100644 --- a/components/header.js +++ b/components/header.js @@ -9,6 +9,9 @@ import { useMe } from './me' import { useApolloClient } from '@apollo/client' import Head from 'next/head' import { signOut, signIn, useSession } from 'next-auth/client' +import { useLightning } from './lightning' +import { useEffect } from 'react' +import { randInRange } from '../lib/rand' function WalletSummary ({ me }) { return `[${me.stacked},${me.sats}]` @@ -86,6 +89,10 @@ export default function Header () { ) } else { + const strike = useLightning() + useEffect(() => { + setTimeout(strike, randInRange(3000, 10000)) + }, []) return path !== '/login' && } } diff --git a/components/lightning.js b/components/lightning.js index 40e7cda3..b3d0b31c 100644 --- a/components/lightning.js +++ b/components/lightning.js @@ -1,16 +1,22 @@ -import React, { useRef, useEffect } from 'react' +import React, { useRef, useEffect, useContext } from 'react' +import { randInRange } from '../lib/rand' -export const LightningContext = React.createContext() +export const LightningContext = React.createContext({ + bolts: 0, + strike: () => {} +}) export class LightningProvider extends React.Component { state = { bolts: 0, - strike: () => this.setState(state => { - return { - ...this.state, - bolts: this.state.bolts + 1 - } - }) + strike: (repeat) => { + this.setState(state => { + return { + ...this.state, + bolts: this.state.bolts + 1 + } + }) + } } render () { @@ -25,6 +31,10 @@ export class LightningProvider extends React.Component { } export const LightningConsumer = LightningContext.Consumer +export function useLightning () { + const { strike } = useContext(LightningContext) + return strike +} export function Lightning () { const canvasRef = useRef(null) @@ -82,7 +92,7 @@ function Bolt (ctx, options) { this.draw = (isChild) => { ctx.beginPath() ctx.moveTo(this.point[0], this.point[1]) - const angleChange = rand(1, this.options.spread) + const angleChange = randInRange(1, this.options.spread) this.lastAngle += this.lastAngle > this.options.angle ? -angleChange : angleChange const radians = this.lastAngle * Math.PI / 180 @@ -100,11 +110,11 @@ function Bolt (ctx, options) { // make skinnier? // ctx.lineWidth = ctx.lineWidth * 0.98 - if (rand(0, 99) < this.options.branches && this.children.length < this.options.maxBranches) { + if (randInRange(0, 99) < this.options.branches && this.children.length < this.options.maxBranches) { this.children.push(new Bolt(ctx, { startPoint: [this.point[0], this.point[1]], length: d * 0.8, - angle: this.lastAngle + rand(350 - this.options.spread, 370 + this.options.spread), + angle: this.lastAngle + randInRange(350 - this.options.spread, 370 + this.options.spread), resistance: this.options.resistance, speed: this.options.speed - 2, spread: this.options.spread - 2, @@ -139,7 +149,3 @@ function Bolt (ctx, options) { setTimeout(() => { this.fade() }, 50) } } - -function rand (min, max) { - return Math.random() * (max - min) + min -} diff --git a/lib/rand.js b/lib/rand.js new file mode 100644 index 00000000..2e4b62aa --- /dev/null +++ b/lib/rand.js @@ -0,0 +1,3 @@ +export function randInRange (min, max) { + return Math.random() * (max - min) + min +} diff --git a/pages/_app.js b/pages/_app.js index 164b23d5..cb7d4eb0 100644 --- a/pages/_app.js +++ b/pages/_app.js @@ -4,6 +4,7 @@ import { Provider } from 'next-auth/client' import { FundErrorModal, FundErrorProvider } from '../components/fund-error' import { MeProvider } from '../components/me' import PlausibleProvider from 'next-plausible' +import { LightningProvider } from '../components/lightning' const client = new ApolloClient({ uri: '/api/graphql', @@ -61,10 +62,12 @@ function MyApp ({ Component, pageProps }) { - - - - + + + + + +