initial working version

This commit is contained in:
AtHeartEngineer
2022-04-05 13:30:42 -04:00
commit 718bde4ef7
17 changed files with 28082 additions and 0 deletions

23
.gitignore vendored Normal file
View File

@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# production
/build
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*

70
README.md Normal file
View File

@@ -0,0 +1,70 @@
# Getting Started with Create React App
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
## Available Scripts
In the project directory, you can run:
### `npm start`
Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in your browser.
The page will reload when you make changes.\
You may also see any lint errors in the console.
### `npm test`
Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
### `npm run build`
Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.
The build is minified and the filenames include the hashes.\
Your app is ready to be deployed!
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
### `npm run eject`
**Note: this is a one-way operation. Once you `eject`, you can't go back!**
If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.
You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.
## Learn More
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
To learn React, check out the [React documentation](https://reactjs.org/).
### Code Splitting
This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
### Analyzing the Bundle Size
This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
### Making a Progressive Web App
This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
### Advanced Configuration
This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
### Deployment
This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
### `npm run build` fails to minify
This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)

27627
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

37
package.json Normal file
View File

@@ -0,0 +1,37 @@
{
"name": "gossipsub_dandelion_visualization",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.3",
"@testing-library/react": "^12.1.4",
"@testing-library/user-event": "^13.5.0",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"react-scripts": "5.0.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}

BIN
public/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

43
public/index.html Normal file
View File

@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>

25
public/manifest.json Normal file
View File

@@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}

3
public/robots.txt Normal file
View File

@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:

22
src/App.js Normal file
View File

@@ -0,0 +1,22 @@
import React from 'react'
import Canvas from './Canvas'
import GeneratePeers from './GeneratePeers'
import render_peers from './RenderPeers'
import render_connections from './RenderConnections'
import { clear_and_setup, render_frameCount } from './Utils'
function App() {
const peers = GeneratePeers()
const draw = (ctx, frameCount) => {
clear_and_setup(ctx)
render_frameCount(ctx, frameCount)
render_connections(ctx, peers)
render_peers(ctx, peers)
}
let height = window.innerHeight;
let width = window.innerWidth;
return <Canvas draw={draw} width={width + "px"} height={height + "px"}/>
}
export default App

33
src/Canvas.js Normal file
View File

@@ -0,0 +1,33 @@
import React from 'react'
import useCanvas from './useCanvas'
function resizeCanvasToDisplaySize(canvas) {
const { width, height } = canvas.getBoundingClientRect()
if (canvas.width !== width || canvas.height !== height) {
const { devicePixelRatio: ratio = 1 } = window
const context = canvas.getContext('2d')
canvas.width = width * ratio
canvas.height = height * ratio
context.scale(ratio, ratio)
return true
}
return false
}
const _predraw = (context, canvas) => {
context.save()
resizeCanvasToDisplaySize(context, canvas)
const { width, height } = context.canvas
context.clearRect(0, 0, width, height)
}
const Canvas = props => {
const { draw, predraw = _predraw, ...rest } = props
const canvasRef = useCanvas(draw, { predraw })
return <canvas ref={canvasRef} {...rest} />
}
export default Canvas

63
src/GeneratePeers.js Normal file
View File

@@ -0,0 +1,63 @@
function GeneratePeers(num_peers, peers = [], min_dst = 50, connection_distance= 110) {
function circle_points(radius, angle, center_x = 0, center_y = 0) {
let x = radius * Math.sin(Math.PI * 2 * angle / 360);
let y = radius * Math.cos(Math.PI * 2 * angle / 360);
return { x: x + center_x, y: y + center_y };
}
function create_peers_around_point(radius,
number,
center_x = 0,
center_y = 0,
start_degree = 0,
stop_degree = 360) {
for (let i = 0; i < number; i++) {
let angle = 360 / number * i;
if (angle >= start_degree && angle <= stop_degree) {
let point = circle_points(radius, angle, center_x, center_y);
peers.push(point);
}
}
return peers;
}
const connect_peers = () => {
peers.forEach((peer, idx) => {
if (peer.connected_to === undefined) {
peer.connected_to = [];
}
peers.forEach((peer2, idx2) => {
if (peer2.connected_to === undefined) {
peer2.connected_to = [];
}
if (idx !== idx2) {
let distance = Math.sqrt(Math.pow(peer.x - peer2.x, 2) + Math.pow(peer.y - peer2.y, 2))
if (distance < connection_distance) {
let chance_bias = (peer.connected_to.length + peer2.connected_to.length) / 4
if (Math.floor(Math.random() * chance_bias) === 0) {
peer.connected_to.push(idx2)
peer2.connected_to.push(idx)
}
}
}
})
})
}
if (peers.length === 0) {
peers.push({ x: 0, y: 0 });
}
create_peers_around_point(100, 8);
create_peers_around_point(100, 6, peers[3].x, peers[3].y, 10, 170);
create_peers_around_point(100, 6, peers[5].x, peers[5].y, 120, 260);
create_peers_around_point(100, 6, peers[7].x, peers[7].y, 200, 360);
create_peers_around_point(100, 6, peers[1].x, peers[1].y, 0, 90);
create_peers_around_point(100, 6, peers[1].x, peers[1].y, 270, 360);
connect_peers()
console.log(peers)
return peers
}
export default GeneratePeers;

16
src/RenderConnections.js Normal file
View File

@@ -0,0 +1,16 @@
import { centered, draw_line } from './Utils'
const render_connections = (ctx, peers) => {
peers.forEach((peer, idx) => {
if (peer.connected_to !== undefined) {
let connected_peers = peer.connected_to
connected_peers.forEach((connected_peer_id) => {
let _peer = centered(peer.x, peer.y, ctx)
let _peer2 = centered(peers[connected_peer_id].x, peers[connected_peer_id].y, ctx)
draw_line(ctx, _peer.x, _peer.y, _peer2.x, _peer2.y)
})
}
})
}
export default render_connections

24
src/RenderPeers.js Normal file
View File

@@ -0,0 +1,24 @@
import { rand_int_range, centered, } from './Utils'
const render_peers = (ctx, peers) => {
peers.forEach((peer, idx) => {
peer.id = idx;
if (peer.hue === undefined) {
peer.hue = rand_int_range(0, 360);
peer.saturation = rand_int_range(50, 100);
peer.lightness = rand_int_range(40, 80);
}
let c_coordinates = centered(peer.x, peer.y, ctx)
ctx.fillStyle = 'hsl(' + peer.hue + ', ' + peer.saturation + '%, ' + peer.lightness + '%)';
ctx.beginPath()
ctx.arc(c_coordinates.x, c_coordinates.y, 15, 0, 2 * Math.PI)
ctx.stroke();
ctx.fill()
peer.fontColor = peer.lightness > 50 ? 0 : 100;
ctx.fillStyle = 'hsl(' + (peer.hue - 90 % 360) + ', 100%, ' + peer.fontColor + '%)';
ctx.fillText(idx, c_coordinates.x, c_coordinates.y + 5);
})
}
export default render_peers;

45
src/Utils.js Normal file
View File

@@ -0,0 +1,45 @@
const clear_and_setup = (ctx) => {
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height)
ctx.lineWidth = 5;
ctx.strokeStyle = "#FFFFFF";
ctx.font = '18px sans';
ctx.textAlign = "center";
}
const render_frameCount = (ctx,frameCount) => {
ctx.lineWidth = 5;
ctx.strokeStyle = "#FFFFFF";
ctx.font = '18px sans';
ctx.textAlign = "center";
ctx.fillText(frameCount, ctx.canvas.width/2, 25);
}
function rand_int_range(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min)
}
function centered(x, y, ctx) {
let width = ctx.canvas.width
let height = ctx.canvas.height
let x_center = width / 2
let y_center = height / 2
let centered_x = x + x_center
let centered_y = y + y_center
return { x: centered_x, y: centered_y }
}
function draw_line(ctx, x1, y1, x2, y2, fillStyle = '#FFFFFF') {
ctx.beginPath();
ctx.fillStyle = fillStyle;
ctx.moveTo(x1, y1)
ctx.lineTo(x2, y2);
ctx.stroke();
}
export {
clear_and_setup,
render_frameCount,
rand_int_range,
centered,
draw_line
};

16
src/index.css Normal file
View File

@@ -0,0 +1,16 @@
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
background-color: black;
display: flex;
justify-content: center;
align-items: center;
}
canvas {
margin: 0 auto
}

7
src/index.js Normal file
View File

@@ -0,0 +1,7 @@
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);

28
src/useCanvas.js Normal file
View File

@@ -0,0 +1,28 @@
import { useRef, useEffect } from 'react'
const useCanvas = (draw) => {
const canvasRef = useRef(null)
useEffect(() => {
const canvas = canvasRef.current
const context = canvas.getContext('2d')
let frameCount = 0
let animationFrameId
const render = () => {
frameCount++
if (frameCount > 65534) {
frameCount = 0
}
draw(context, frameCount)
animationFrameId = window.requestAnimationFrame(render)
}
render()
return () => {
window.cancelAnimationFrame(animationFrameId)
}
}, [draw])
return canvasRef
}
export default useCanvas