UNPKG

neuralaesthetics

Version:

Awesome neural networks for aesthetic web pages

49 lines (48 loc) 2.34 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; // contains all the premade functions import { delay } from "./general"; export const shiftNeurons = (neurons, connections, iter) => { let lng = neurons.length; neurons[lng - 1].newPosX = neurons[0].posX; neurons[lng - 1].newPosY = neurons[0].posY; for (let idx = 0; idx < neurons.length - 1; idx += 1) { neurons[idx].newPosX = neurons[idx + 1].posX; neurons[idx].newPosY = neurons[idx + 1].posY; } }; export const centerNeuronForce = (neurons, forces, iter) => { for (let idx = 0; idx < neurons.length; idx += 1) { const neuron = neurons[idx]; forces[idx].x += neuron.originalPosX - neuron.posX; forces[idx].y += neuron.originalPosY - neuron.posY; } }; export const centerIdleMovement = (neurons, forces, iter) => { // pythagorean theorem for distance between two points let distance = (x1, y1, x2, y2) => { return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)); }; for (let idx = 0; idx < neurons.length; idx += 1) { const neuron = neurons[idx]; if (distance(neuron.originalPosX, neuron.originalPosY, neuron.posX, neuron.posY) > 5) { forces[idx].x += 0.2 * (neuron.originalPosX - neuron.posX); forces[idx].y += 0.2 * (neuron.originalPosY - neuron.posY); } else { forces[idx].x += Math.random() * 20 - 10; forces[idx].y += Math.random() * 20 - 10; } } }; export const additionalForces = (neurons, forces, iter) => __awaiter(void 0, void 0, void 0, function* () { yield delay(1000); forces[0].y += 1000; });