Skip to content

Commit 7e779a7

Browse files
committed
Allow user to change color of any balloon
1 parent a05dc5a commit 7e779a7

File tree

1 file changed

+54
-5
lines changed

1 file changed

+54
-5
lines changed

js/tracker.js

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,6 +1957,7 @@ function updateVehicleInfo(vcallsign, newPosition) {
19571957
((vehicle.vehicle_type!="car") ? '<span class="sbutton" onclick="shareVehicle(\'' + vcallsign + '\')" style="top:'+(vehicle.image_src_size[1]+55)+'px">Share</span>' : '') +
19581958
((vehicle.vehicle_type!="car") ? '<span class="sbutton" onclick="skewTdraw(\'' + vcallsign + '\')" style="top:'+(vehicle.image_src_size[1]+85)+'px">SkewT</span>' : '') +
19591959
((vehicle.vehicle_type!="car") ? '<span class="sbutton" onclick="openURL(\'' + grafana_dashboard_url + '\')" style="top:'+(vehicle.image_src_size[1]+115)+'px">Plots</span>' : '') +
1960+
((vehicle.vehicle_type!="car") ? '<span class="sbutton" onclick="recolorVehicle(\'' + vcallsign + '\')" style="top:'+(vehicle.image_src_size[1]+145)+'px">Recolor</span>' : '') +
19601961
'<div class="left">' +
19611962
'<dl>';
19621963
//mobile
@@ -1969,6 +1970,7 @@ function updateVehicleInfo(vcallsign, newPosition) {
19691970
((vehicle.vehicle_type!="car") ? '<span class="sbutton" onclick="shareVehicle(\'' + vcallsign + '\')" style="top:55px">Share</span>' : '') +
19701971
((vehicle.vehicle_type!="car") ? '<span class="sbutton" onclick="skewTdraw(\'' + vcallsign + '\')" style="top:85px">SkewT</span>' : '') +
19711972
((vehicle.vehicle_type!="car") ? '<span class="sbutton" onclick="openURL(\'' + grafana_dashboard_url + '\')" style="top:115px">Plots</span>' : '') +
1973+
((vehicle.vehicle_type!="car") ? '<span class="sbutton" onclick="recolorVehicle(\'' + vcallsign + '\')" style="top:115px">Recolor</span>' : '') +
19721974
'<div class="left">' +
19731975
'<dl>';
19741976
var b = '</dl>' +
@@ -2336,13 +2338,24 @@ function drawLaunchPrediction(vcallsign) {
23362338
// Takes in an SVG for a balloon, parachute, target, car, etc and sets a dynamic-color
23372339
// variable which that SVG can use to recolor any relevant elements.
23382340
// See balloon.svg, target.svg, etc for examples
2339-
function recolorSVG(svg_path, color) {
2340-
const xhr = new XMLHttpRequest();
2341-
xhr.open('GET', svg_path, false);
2342-
xhr.send();
2341+
2342+
// Can be called either with a SVG file path, or with an existing SVG.
2343+
function recolorSVG(svg_input, color) {
2344+
let svgContent;
2345+
// If svg_input starts with 'data:image/svg+xml', assume it's a full SVG document string
2346+
if (svg_input.startsWith('data:image/svg+xml')) {
2347+
// remove the data:image... to leave raw XML data, then decode.
2348+
svgContent = decodeURIComponent(svg_input.replace('data:image/svg+xml;charset=utf-8,', ''));
2349+
} else {
2350+
// Otherwise, treat it as a file path and fetch that file to be our SVG.
2351+
const xhr = new XMLHttpRequest();
2352+
xhr.open('GET', svg_input, false);
2353+
xhr.send();
2354+
svgContent = xhr.responseText;
2355+
}
23432356

23442357
const parser = new DOMParser();
2345-
const svgDocument = parser.parseFromString(xhr.responseText, 'image/svg+xml');
2358+
const svgDocument = parser.parseFromString(svgContent, 'image/svg+xml');
23462359
svgDocument.documentElement.style.setProperty("--dynamic-color", color);
23472360
return 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svgDocument.documentElement.outerHTML);
23482361
}
@@ -3617,6 +3630,42 @@ function addPosition(position) {
36173630
return;
36183631
}
36193632

3633+
// When "Recolor" button is clicked in the data pane for this vehicle,
3634+
// cycle it to the next color in the sequence. Allows user to change color,
3635+
// primarily for sake of increased contrast on maps.
3636+
function recolorVehicle(vcallsign) {
3637+
const vehicle = vehicles[vcallsign];
3638+
3639+
vehicle.color_index = (vehicle.color_index + 1) % balloon_colors.length;
3640+
const new_color = balloon_colors[vehicle.color_index];
3641+
3642+
// Recolor the lines on the map to the new color
3643+
vehicle.prediction_launch_polyline.setStyle({color: new_color});
3644+
vehicle.prediction_polyline.setStyle({color: new_color});
3645+
vehicle.polyline[0].setStyle({color: new_color});
3646+
3647+
// Recolor the prediction target
3648+
const targetIcon = vehicle.prediction_target.getIcon().options.iconUrl;
3649+
const recoloredTarget = recolorSVG(targetIcon, new_color);
3650+
const newTarget = L.icon( // Create new icon which inherits properties from the existing one
3651+
Object.assign({}, vehicle.prediction_target.options.icon.options, {
3652+
iconUrl: recoloredTarget // just with a new icon
3653+
})
3654+
);
3655+
vehicle.prediction_target.setIcon(newTarget);
3656+
3657+
// Recolor the balloon/parachute/whatever
3658+
const balloon = vehicle.marker.getIcon().options.iconUrl;
3659+
const recoloredBalloon = recolorSVG(balloon, new_color);
3660+
const newBalloon = L.icon(
3661+
Object.assign({}, vehicle.marker.options.icon.options, {
3662+
iconUrl: recoloredBalloon
3663+
})
3664+
);
3665+
vehicle.marker.setIcon(newBalloon);
3666+
vehicle.img_src = newBalloon;
3667+
}
3668+
36203669
// Graph Stuff
36213670

36223671
var graph_fields = ['altitude', 'pred.alt', 'batt', 'humidity', 'pressure', 'sats', 'temperature_external', 'oif411_O3_partial_pressure'];

0 commit comments

Comments
 (0)