fantasy/main.js

85 lines
2.4 KiB
JavaScript

import factory from "./painting/factory.js";
import Shape from "./painting/shape.js";
import Canvas from "./ui/canvas.js";
import Button from "./ui/button.js";
import Panel from "./ui/panel.js";
import Form from "./ui/editors/form.js";
import NumericBox from "./ui/editors/numericBox.js";
import SwitchBox from "./ui/editors/switchBox.js";
document.body.innerHTML = "";
const maxPanelWidth = 300;
const panelPaddingTop = 90;
let random = true;
let amountOfShapes = 100;
Shape.setBounding(window.innerWidth, window.innerHeight);
let canvas = new Canvas(window.innerWidth, window.innerHeight);
let paintButton = new Button("paint");
paintButton.addHandler(paint);
let panelWidth = Math.min(maxPanelWidth ,window.innerWidth - 20);
let panelHeight = window.innerHeight - 20;
let panel = new Panel(panelWidth, panelHeight);
panel.element.style.paddingTop = panelPaddingTop + "px";
panel.setPosition(10, 10);
let settings = new Button("gear");
settings.addHandler(panel.toggle.bind(panel));
let clear = new Button("blank");
clear.addHandler(canvas.clear.bind(canvas));
window.addEventListener("resize", onWindowResize, false);
let form = new Form(panelWidth, panelHeight - panelPaddingTop);
let randomBox = new SwitchBox(random, 50, 30);
let amountBox = new NumericBox(50, 100);
amountBox.setValue(amountOfShapes);
form.addLine("random", "Random amount of shapes",randomBox);
form.addLine("amount", "Amount of shapes", amountBox);
panel.add(form);
document.body.appendChild(canvas.element);
document.body.appendChild(panel.element);
document.body.appendChild(paintButton.element);
document.body.appendChild(settings.element);
document.body.appendChild(clear.element);
adjustButtons(panelWidth);
paint();
function onWindowResize(e) {
Shape.setBounding(window.innerWidth, window.innerHeight);
canvas.setSize(window.innerWidth, window.innerHeight);
let panelWidth = Math.min(maxPanelWidth ,window.innerWidth - 20);
panel.setSize(panelWidth, window.innerHeight - 20);
adjustButtons(panelWidth);
}
function paint() {
let amount;
if (random) {
amount = Math.floor(Math.random() * amountOfShapes);
} else {
amount = amountOfShapes;
}
for (let i = 0; i < amount; ++i) {
let shape = factory.createRandomShape();
canvas.draw(shape);
}
}
function adjustButtons(width) {
paintButton.setPosition(width / 4, 25);
settings.setPosition(width / 2, 25);
clear.setPosition(width * 0.75, 25);
}