1
0
Fork 0
mirror of https://github.com/Acamaeda/The-Modding-Tree.git synced 2024-11-30 20:02:07 +00:00
The-Modding-Tree/js/canvas.js

71 lines
2.1 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;
2020-08-25 22:42:43 +00:00
if (treeTab===undefined||treeTab===null) return false;
2020-08-19 02:50:24 +00:00
canvas = treeCanv;
ctx = canvas.getContext("2d");
return true;
}
function resizeCanvas() {
if (!retrieveCanvasData()) return
canvas.width = 0;
canvas.height = 0;
2020-08-25 22:42:43 +00:00
canvas.width = document.getElementById("treeTab").scrollWidth;
canvas.height = document.getElementById("treeTab").scrollHeight;
2020-08-19 02:50:24 +00:00
drawTree();
}
2020-09-07 14:16:04 +00:00
var colors = {
2020-09-14 19:26:10 +00:00
default: {
1: "#ffffff",
2: "#bfbfbf",
3: "#7f7f7f",
},
aqua: {
2020-09-14 21:18:02 +00:00
1: "#bfdfff",
2: "#8fa7bf",
3: "#5f6f7f",
2020-09-14 19:26:10 +00:00
},
2020-09-07 14:16:04 +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 (layers[layer].layerShown() && layers[layer].branches){
for (branch in layers[layer].branches)
{
drawTreeBranch(layer, layers[layer].branches[branch])
}
}
2020-08-19 02:50:24 +00:00
}
}
function drawTreeBranch(num1, data) { // taken from Antimatter Dimensions & adjusted slightly
let num2 = data[0]
let color_id = data[1]
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-08-25 22:42:43 +00:00
let x1 = start.left + (start.width / 2) + (document.getElementById("treeTab").scrollLeft || document.body.scrollLeft);
let y1 = start.top + (start.height / 2) + (document.getElementById("treeTab").scrollTop || document.body.scrollTop);
let x2 = end.left + (end.width / 2) + (document.getElementById("treeTab").scrollLeft || document.body.scrollLeft);
let y2 = end.top + (end.height / 2) + (document.getElementById("treeTab").scrollTop || document.body.scrollTop);
2020-08-19 02:50:24 +00:00
ctx.lineWidth = 15;
ctx.beginPath();
2020-09-14 19:26:10 +00:00
ctx.strokeStyle = colors_theme[color_id]
2020-08-19 02:50:24 +00:00
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
}