Skip to content

Instantly share code, notes, and snippets.

@maderlock
Created November 16, 2020 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maderlock/3348190b284d8a233ec26c6113c4b245 to your computer and use it in GitHub Desktop.
Save maderlock/3348190b284d8a233ec26c6113c4b245 to your computer and use it in GitHub Desktop.
Roll20 API script to contract and expand maps
myGetCleanImagesrc = function (imgsrc) {
var parts = imgsrc.match(/(.*\/images\/.*)(thumb|med|original|max)([^?]*)(\?[^?]+)?$/);
if(parts) {
return parts[1]+'thumb'+parts[3]+(parts[4]?parts[4]:`?${Math.round(Math.random()*9999999)}`);
}
return;
};
// When a map changes vertical location, resize
const x_threshold = 800;
on("change:graphic:top", function(obj, prev) {
if (obj.get("name").startsWith("Map:")) {
var left = obj.get("left");
var prevLeft = prev["left"];
// If below threshold, resize keeping top of image still
if (left <= x_threshold && prevLeft > x_threshold) {
// Save width and height
obj.set("bar1_value",obj.get("width"));
obj.set("bar2_value",obj.get("height"));
// Resize down
obj.set("width",240);
obj.set("height",40);
// Set to side 1
if (obj.get("sides") != "") {
var allSides = obj.get("sides").split("|");
obj.set({
currentSide: 1,
imgsrc: myGetCleanImagesrc(decodeURIComponent(allSides[1]))
});
}
}
// If above threshold, return to original size, keeping bottom of image still
if (left > x_threshold && prevLeft <= x_threshold) {
// Restore size
obj.set("width",obj.get("bar1_value"));
obj.set("height",obj.get("bar2_value"));
// Clear saved size
obj.set("bar1_value","");
obj.set("bar2_value","");
// Set to side 0
if (obj.get("sides") != "") {
var allSides = obj.get("sides").split("|");
obj.set({
currentSide: 0,
imgsrc: myGetCleanImagesrc(decodeURIComponent(allSides[0]))
});
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment