Created
January 3, 2024 23:55
-
-
Save TechnicJelle/6ae16856e8060b3bd9142c6d6dcbe3d2 to your computer and use it in GitHub Desktop.
A BlueMap script that adds a basic distance measuring tool to the website.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const positions = []; | |
const distanceLineMarker = new BlueMap.LineMarker("distanceLineMarker"); | |
distanceLineMarker.line.depthTest = false; | |
distanceLineMarker.line.linewidth = 2; | |
bluemap.popupMarkerSet.add(distanceLineMarker); | |
hijack(bluemap.popupMarker, 'open', function (original) { | |
return function () { | |
const pos = bluemap.popupMarker.position; | |
positions.push([pos.x, pos.y, pos.z]); | |
if (positions.length === 2) { | |
const prevPos = positions[0]; | |
const newPos = positions[1]; | |
const distance = Math.sqrt(Math.pow(prevPos[0] - newPos[0], 2) + Math.pow(prevPos[1] - newPos[1], 2) + Math.pow(prevPos[2] - newPos[2], 2)); | |
console.log("Distance between " + prevPos + " and " + newPos + " is " + distance); | |
distanceLineMarker.data.label = "Distance: " + distance.toFixed(2) + " blocks"; | |
const avgX = (prevPos[0] + newPos[0]) / 2; | |
const avgY = (prevPos[1] + newPos[1]) / 2; | |
const avgZ = (prevPos[2] + newPos[2]) / 2; | |
distanceLineMarker.position = {x: avgX, y: avgY, z: avgZ}; | |
const points = [ | |
prevPos[0] - avgX + 0.5, prevPos[1] - avgY + 1, prevPos[2] - avgZ + 0.5, | |
newPos[0] - avgX + 0.5, newPos[1] - avgY + 1, newPos[2] - avgZ + 0.5 | |
]; | |
distanceLineMarker.setLine(points); | |
//remove the first element | |
positions.shift(); | |
} | |
original.call(this); | |
}; | |
}); | |
/** | |
* Hijack a function with custom behaviour | |
* from https://gist.github.com/joshwnj/625349/ | |
* @param {object} Context object | |
* @param {string} Name of the context object's function | |
* @param {function} Override function | |
* @return {function} Original function | |
*/ | |
function hijack(object, funcName, override) { | |
var original = object[funcName]; | |
object[funcName] = override(original); | |
return original; | |
} |
I have now moved this script to a full repository, instead of a gist: https://github.com/TechnicJelle/BlueMapWebScripts/tree/main/distance-measurer
If you have comments, issues or feature requests about it, please post them there as GitHub Issues.
⚠️ I will not be responding to comments on this gist any more. ⚠️
Consider it archived.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you! 😊
Maybe add a little "❌" to both ends to cancel/close the line, or is there an easy way to do it already?