TWS/WAP Cheatsheet

brain.js.org/#/

projects/brainjsxor

projects/brainjscolors

HTML

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="//unpkg.com/brain.js"></script> <link href="style.css" rel="stylesheet" type="text/css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet"> </head> <body> <div class="wrapper"> <p id="text">Hello World!</p> <div id="netPic"></div> <button id="run">RUN</button> </div> <script src="main.js"></script> </body> </html>

CSS

body { overflow: hidden; } .wrapper { display: flex; align-items: center; justify-content: center; width: 100%; height: 100vh; } p { font-size: 28px; font-family: 'Roboto', sans-serif; }

JavaScript

const net = new brain.NeuralNetwork(); const data = [ { input: { r: 0, g: 0, b: 0}, output: [1] }, { input: { r: 1, g: 1, b: 1}, output: [0] } ]; // 0 - black, 1 - white net.train(data); const netPic = document.getElementById('netPic'); netPic.innerHTML = brain.utilities.toSVG(net); console.log(net.run({r: 1, g: 0, b: 1})); let color; const text = document.getElementById('text'); const run = document.getElementById('run'); const generate = () => { color = { r: Math.random(), g: Math.random(), b: Math.random() }; const textColor = net.run(color)[0]; text.style.color = textColor > .5 ? 'white' : 'black'; document.body.style.backgroundColor = `rgb(${color.r * 255}, ${color.g * 255}, ${color.b * 255})`; }; run.onclick = generate;

Page