1
0
Fork 0
mirror of https://github.com/Acamaeda/The-Modding-Tree.git synced 2024-11-22 00:21:32 +00:00
The-Modding-Tree/js/technical/canvas.js

89 lines
2.3 KiB
JavaScript
Raw Normal View History

2020-08-19 02:50:24 +00:00
var canvas;
2020-08-25 22:42:43 +00:00
var ctx;
2020-08-19 02:50:24 +00:00
window.addEventListener("resize", (_=>resizeCanvas()));
function retrieveCanvasData() {
let treeCanv = document.getElementById("treeCanvas")
2020-08-25 22:42:43 +00:00
let treeTab = document.getElementById("treeTab")
2020-08-19 02:50:24 +00:00
if (treeCanv===undefined||treeCanv===null) return false;
canvas = treeCanv;
ctx = canvas.getContext("2d");
return true;
}
function resizeCanvas() {
if (!retrieveCanvasData()) return
canvas.width = 0;
canvas.height = 0;
2020-10-28 02:04:16 +00:00
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
drawTree();
2020-08-19 02:50:24 +00:00
}
2021-06-07 03:45:32 +00:00
2020-09-14 19:26:10 +00:00
var colors_theme
2020-09-07 14:16:04 +00:00
2020-08-19 02:50:24 +00:00
function drawTree() {
if (!retrieveCanvasData()) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (layer in layers){
if (tmp[layer].layerShown == true && tmp[layer].branches){
2020-10-16 01:02:05 +00:00
for (branch in tmp[layer].branches)
{
2020-10-16 01:02:05 +00:00
drawTreeBranch(layer, tmp[layer].branches[branch])
}
}
2021-06-07 23:40:34 +00:00
drawComponentBranches(layer, tmp[layer].upgrades, "upgrade-")
drawComponentBranches(layer, tmp[layer].buyables, "buyable-")
drawComponentBranches(layer, tmp[layer].clickables, "clickable-")
2021-06-03 17:02:47 +00:00
2021-06-07 23:40:34 +00:00
}
}
function drawComponentBranches(layer, data, prefix) {
for(id in data) {
if (data[id].branches) {
for (branch in data[id].branches)
{
drawTreeBranch(id, data[id].branches[branch], prefix + layer + "-")
2021-06-03 17:02:47 +00:00
}
2021-06-07 23:40:34 +00:00
2021-06-03 17:02:47 +00:00
}
2020-08-19 02:50:24 +00:00
}
2021-06-07 23:40:34 +00:00
2020-08-19 02:50:24 +00:00
}
2021-06-03 17:02:47 +00:00
function drawTreeBranch(num1, data, prefix) { // taken from Antimatter Dimensions & adjusted slightly
let num2 = data
let color_id = 1
2021-06-07 04:13:18 +00:00
let width = 15
if (Array.isArray(data)){
num2 = data[0]
color_id = data[1]
2021-06-07 04:13:18 +00:00
width = data[2] || width
}
if(typeof(color_id) == "number")
color_id = colors_theme[color_id]
2021-06-03 17:02:47 +00:00
if (prefix) {
num1 = prefix + num1
num2 = prefix + num2
}
if (document.getElementById(num1) == null || document.getElementById(num2) == null)
return
let start = document.getElementById(num1).getBoundingClientRect();
2020-08-19 02:50:24 +00:00
let end = document.getElementById(num2).getBoundingClientRect();
2020-11-08 22:53:40 +00:00
let x1 = start.left + (start.width / 2) + document.body.scrollLeft;
let y1 = start.top + (start.height / 2) + document.body.scrollTop;
let x2 = end.left + (end.width / 2) + document.body.scrollLeft;
let y2 = end.top + (end.height / 2) + document.body.scrollTop;
2021-06-07 04:13:18 +00:00
ctx.lineWidth = width;
2020-08-19 02:50:24 +00:00
ctx.beginPath();
ctx.strokeStyle = color_id
2020-08-19 02:50:24 +00:00
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
2021-06-07 03:45:32 +00:00
}