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

64 lines
2.2 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();
}
function drawTree() {
if (!retrieveCanvasData()) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (layerUnl('b')) drawTreeBranch("p", "b")
if (layerUnl('g')) drawTreeBranch("p", "g")
if (layerUnl('e')) {
drawTreeBranch("b", "e")
drawTreeBranch("g", "e")
}
if (layerUnl('t')) drawTreeBranch("b", "t")
2020-08-22 01:16:23 +00:00
if (layerUnl('sb')) drawTreeBranch("b", "sb")
2020-08-19 02:50:24 +00:00
if (layerUnl('s')) drawTreeBranch("g", "s")
2020-08-26 01:45:22 +00:00
if (layerUnl('h')) drawTreeBranch("t", "h")
if (layerUnl('q')) drawTreeBranch("e", "q")
2020-08-29 23:43:53 +00:00
if (layerUnl('hb')) {
drawTreeBranch("sb", "hb")
drawTreeBranch("t", "hb")
}
if (layerUnl('ss')) {
drawTreeBranch("e", "ss")
drawTreeBranch("s", "ss")
}
2020-08-19 02:50:24 +00:00
needCanvasUpdate = false;
}
function drawTreeBranch(num1, num2) { // taken from Antimatter Dimensions & adjusted slightly
let start = document.getElementById(num1).getBoundingClientRect();
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();
ctx.strokeStyle = "white"
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
}