fantasy/main.js

111 lines
3.5 KiB
JavaScript
Raw Normal View History

2018-11-18 18:04:50 +00:00
import factory from "./painting/factory.js";
import Shape from "./painting/shape.js";
2018-11-19 15:03:05 +00:00
import Canvas from "./ui/canvas.js";
2018-11-19 14:09:43 +00:00
import Button from "./ui/button.js";
2018-11-19 14:44:36 +00:00
import Panel from "./ui/panel.js";
2018-11-18 00:00:38 +00:00
import Form from "./ui/editors/form.js";
import NumericBox from "./ui/editors/numericBox.js";
import SwitchBox from "./ui/editors/switchBox.js";
2018-11-18 18:04:50 +00:00
document.body.innerHTML = "";
2018-11-19 15:03:05 +00:00
const maxPanelWidth = 300;
const panelPaddingTop = 90;
Shape.setBounding(window.innerWidth, window.innerHeight);
let canvas = new Canvas(window.innerWidth, window.innerHeight);
2018-11-18 18:04:50 +00:00
let paintButton = new Button("paint");
paintButton.addHandler(paint);
2018-11-19 14:44:36 +00:00
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";
2018-11-19 15:03:05 +00:00
panel.setPosition(10, 10);
2018-11-19 14:44:36 +00:00
let settings = new Button("gear");
settings.addHandler(panel.toggle.bind(panel));
let clear = new Button("blank");
clear.addHandler(canvas.clear.bind(canvas));
2018-11-20 15:23:43 +00:00
let downloadButton = new Button("download", true);
downloadButton.element.download = "fantasy.png";
2018-11-20 15:10:46 +00:00
2018-11-19 14:09:43 +00:00
window.addEventListener("resize", onWindowResize, false);
let form = new Form(panelWidth, panelHeight - panelPaddingTop);
2018-11-20 14:30:01 +00:00
let coloringBox = new SwitchBox(true, 40, 20);
let randomBox = new SwitchBox(true, 40, 20);
2018-11-20 11:49:16 +00:00
let amountBox = new NumericBox(100, 30);
amountBox.setValue(100);
2018-11-20 14:01:34 +00:00
let circlesBox = new SwitchBox(true, 40, 20);
let rectanglesBox = new SwitchBox(true, 40, 20);
let trianglesBox = new SwitchBox(true, 40, 20);
2018-11-20 14:30:01 +00:00
let blurBox = new NumericBox(100, 30);
blurBox.setValue(0);
2018-11-20 14:30:01 +00:00
form.addLine("coloring", "Colored shapes", coloringBox);
form.addLine("random", "Random amount of shapes", randomBox);
form.addLine("amount", "Amount of shapes", amountBox);
form.addLine("circles", "Probability of circles", circlesBox);
form.addLine("rectangles", "Probability of rectangles", rectanglesBox);
form.addLine("triangles", "Probability of triangles", trianglesBox);
2018-11-20 14:30:01 +00:00
form.addLine("blur", "Blur strength", blurBox);
panel.add(form);
2018-11-19 14:09:43 +00:00
document.body.appendChild(canvas.element);
2018-11-19 14:44:36 +00:00
document.body.appendChild(panel.element);
document.body.appendChild(paintButton.element);
2018-11-19 14:44:36 +00:00
document.body.appendChild(settings.element);
document.body.appendChild(clear.element);
2018-11-20 15:10:46 +00:00
document.body.appendChild(downloadButton.element);
adjustButtons(panelWidth);
2018-11-19 15:08:21 +00:00
paint();
2018-11-19 14:09:43 +00:00
function onWindowResize(e) {
Shape.setBounding(window.innerWidth, window.innerHeight);
2018-11-19 15:03:05 +00:00
canvas.setSize(window.innerWidth, window.innerHeight);
let panelWidth = Math.min(maxPanelWidth ,window.innerWidth - 20);
panel.setSize(panelWidth, window.innerHeight - 20);
adjustButtons(panelWidth);
2018-11-19 14:09:43 +00:00
}
function paint() {
2018-11-20 14:30:01 +00:00
canvas.setBlurStrength(blurBox.value);
Shape.setColoring(coloringBox.value);
factory.setCollection({
rectangle: rectanglesBox.value,
circle: circlesBox.value,
triangle: trianglesBox.value
});
let amount;
if (randomBox.value) {
amount = Math.floor(Math.random() * amountBox.value);
} else {
amount = amountBox.value;
}
2018-11-19 14:09:43 +00:00
for (let i = 0; i < amount; ++i) {
let shape = factory.createRandomShape();
canvas.draw(shape);
}
2018-11-20 15:23:43 +00:00
downloadButton.element.href = canvas.getData();
}
function adjustButtons(width) {
2018-11-20 15:10:46 +00:00
let hc = width / 8;
let c = width / 4;
let ws = -Button.width / 2 + 10;
paintButton.setPosition(hc + ws, 25);
settings.setPosition(c + hc + ws, 25);
clear.setPosition(2 * c + hc + ws, 25);
downloadButton.setPosition(3 * c + hc + ws, 25);
2018-11-19 14:09:43 +00:00
}