Created
November 8, 2021 13:54
-
-
Save DEVTomatoCake/b849162cf32ae039d17f8b6e4916f6ba to your computer and use it in GitHub Desktop.
Node.js Atommodell-Generator (fehlerhafte Valenzelektronen)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { createCanvas, loadImage } = require('canvas') | |
const canvas = createCanvas(256, 256) | |
const ctx = canvas.getContext('2d') | |
const express = require("express") | |
const app = express() | |
app.listen(3000) | |
app.get("/", (req, res) => { | |
res.send("<img width=256 height=256 src='" + generate() + "'>") | |
}) | |
const protonen = 20 | |
var elektronen = protonen // auf jeden Fall notwendig *hust* | |
var elektronenmodify = protonen | |
var bahn = 0 | |
var x = [] | |
var y = [] | |
const center = 128 | |
const steps = 360 | |
function generate() { | |
// Zeichnet den "Kern" | |
ctx.fillStyle = "red" | |
ctx.fillRect(center - 20, center - 20, 40, 40) | |
ctx.fillStyle = "black" | |
ctx.font = '48px Arial' | |
ctx.fillText(protonen, center - 10, center, 20) | |
// Zeichnet die Außenbahnen | |
ctx.fillStyle = "blue" | |
drawAußenbahn(65, bahn++) | |
if (elektronen > 2 && elektronenmodify > 0) drawAußenbahn(80, bahn++) | |
if (elektronen > 10 && elektronenmodify > 0) drawAußenbahn(95, bahn++) | |
if (elektronen > 28 && elektronenmodify > 0) drawAußenbahn(110, bahn++) | |
if (elektronen > 60 && elektronenmodify > 0) drawAußenbahn(125, bahn++) | |
return canvas.toDataURL() | |
} | |
function drawAußenbahn(radius) { // "Wie deutsch soll es noch sein?" - "Ja." | |
// Zeichnet die Außenbahn | |
ctx.beginPath() | |
ctx.arc(center, center, radius, 0, Math.PI * 2, true) | |
ctx.stroke() | |
// Setzt die Position des "Stifts" zurück, damit die Elektronen, naja, halt richtig gezeichnet werden .-. | |
elektronenpos = 0 | |
// Berechnet die Koordinaten der aktuellen Bahn, damit die Elektronen an der richtigen Stelle gezeichnet werden | |
for (var i = 0; i < steps; i++) { | |
x[i] = (center + radius * Math.cos(2 * Math.PI * i / steps)) | |
y[i] = (center + radius * Math.sin(2 * Math.PI * i / steps)) | |
} | |
// Berechnet, wie viele Elektronen auf der aktuellen Bahn sind | |
elektronentemp = 2 * (bahn * bahn) | |
// Wenn die berechnete Anzahl größer ist als die Anzahl der verbleibenden Elektronen, wird die Anzahl korrigiert | |
elektronentemp = Math.min(elektronentemp, elektronenmodify) | |
// Ändert die Anzahl der noch verbleibenden Elektronen | |
elektronenmodify -= 2 * (bahn * bahn) | |
// Zeichnet die Elektronen | |
for (let i = 0; i < elektronentemp; i++) { | |
ctx.fillRect(x[elektronenpos] - 5, y[elektronenpos] - 5, 10, 10) | |
elektronenpos += Math.round(360 / elektronentemp) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment