animate/docs/js/index.js

146 lines
3.3 KiB
JavaScript
Raw Permalink Normal View History

2023-03-21 05:23:52 +00:00
//jshint esversion:8
let c = document.getElementById("myCanvas");
let ctx = c.getContext("2d");
ctx.canvas.width = window.innerWidth;
ctx.canvas.height = window.innerHeight;
centerX = ctx.canvas.width / 2;
centerY = ctx.canvas.height / 2;
2023-04-17 06:10:38 +00:00
let deg_per_sec = 10;
2023-03-21 05:23:52 +00:00
let targetFps = 60;
let frameDuration = 1000 / targetFps;
let rotation = 0; //was = j = angle
let paused = true;
render_clear();
let drawObj = null;
function createInstance(className, args) {
const classMap = {
2023-11-11 06:54:13 +00:00
NewWave: NewWave,
2023-03-21 05:23:52 +00:00
PolyTwistColourWidth: PolyTwistColourWidth,
FloralPhyllo: FloralPhyllo,
Spiral1: Spiral1,
2023-03-21 06:08:08 +00:00
FloralAccident: FloralAccident,
FloralPhyllo_Accident: FloralPhyllo_Accident,
Nodal_expanding: Nodal_expanding,
2023-04-07 23:20:14 +00:00
Phyllotaxis:Phyllotaxis,
SquareTwist_angle:SquareTwist_angle,
EyePrototype:EyePrototype,
2023-05-04 07:20:19 +00:00
CircleExpand:CircleExpand,
2023-05-23 07:00:24 +00:00
MaryFace:MaryFace,
2023-03-21 05:23:52 +00:00
// Add more class constructors here as needed
};
if (classMap.hasOwnProperty(className)) {
return new classMap[className](...args);
} else {
throw new Error(`Unknown class name: ${className}`);
}
}
2023-04-17 06:10:38 +00:00
2023-03-21 05:23:52 +00:00
async function updateDrawObj() {
const shapeSelector = document.getElementById("shape-selector");
const selectedShape = shapeSelector.value;
const config = await fetchConfig(selectedShape);
if (drawObj) {
drawObj.remove(); // Remove the previous instance
}
// Extract default values from the configuration
const defaultValues = config
// .filter((item) => item.type !== "color") // Exclude color inputs
.map((item) => item.defaultValue);
drawObj = createInstance(selectedShape, defaultValues);
2023-04-17 06:10:38 +00:00
// drawObj = await createShapeWithRandomProperties(813311281, config1);
console.log(drawObj)
2023-03-21 05:23:52 +00:00
drawObj.initialise(config);
}
updateDrawObj();
function render() {
setTimeout(() => {
requestAnimationFrame(() => {
render_clear();
if (drawObj) {
drawObj.draw(rotation);
}
if (!paused) {
rotation += deg_per_sec / targetFps;
}
2023-04-29 07:27:11 +00:00
// drawCenter(300)
2023-03-21 05:23:52 +00:00
});
render();
}, frameDuration);
}
document
.getElementById("shape-selector")
.addEventListener("change", updateDrawObj);
let toolbarShowing = true;
document.addEventListener("keydown", toggleSettings);
2023-03-23 04:28:46 +00:00
function manualToggleSettings(){
console.log("hi")
toolbarShowing = !toolbarShowing;
let tb = document.getElementById("toolbar");
if (toolbarShowing) {
tb.style.display = "flex";
} else {
tb.style.display = "none";
}
}
2023-03-21 05:23:52 +00:00
function toggleSettings(e) {
if (e.key == "p") {
toolbarShowing = !toolbarShowing;
}
if (e.code === "Space") {
paused = !paused;
}
let tb = document.getElementById("toolbar");
if (toolbarShowing) {
tb.style.display = "flex";
} else {
tb.style.display = "none";
}
}
function TogglePause() {
let pb = document.getElementById("pauseButton");
paused = !paused;
if (paused) {
pb.textContent = "Play";
} else {
pb.textContent = "Pause";
}
}
function Reset() {
rotation = 0; //was = j = angle
currentFrame = 0;
}
function ForwardFrame() {
rotation += deg_per_sec / fps; // was = j = innerRotation, now = rotation
currentFrame += 1; // was = i
}
function BackwardFrame() {
rotation -= deg_per_sec / fps; // was = j = innerRotation, now = rotation
currentFrame -= 1; // was = i
}
function ChangeDegPerSec(newValue) {
deg_per_sec = newValue;
}
window.onload = render;