5 <meta name=
"viewport" content=
"width=device-width, initial-scale=1" />
8 href=
"data:image/svg+xml;base64,@FAVICON@" />
11 CharmBypass: got that transit equity
16 /* For consistency with the SVG */
17 font-family: sans-serif;
34 /* Display form errors for the one browser that doesn't support
35 * them natively, mobile Firefox. We use "visibility" to toggle
36 * it on and off, but "display" to hide it completely in non-
37 * stupid web browsers. */
45 /* Set the height to
100% of the screen, which we'll keep; and the
46 * initial position to (
0,
0), which we're going to change
47 * every time the window is resized, because the exact amount
48 * that we have to slide the SVG to the left to get the ticket
49 * into the center will change. */
55 /* Hide everything by default. We only show it once the user has
56 * submitted the menu form */
60 /* The blinking fade in/out animation for the ticket date and time */
67 #tickettime, #ticketdate {
68 /*
300 two-second blinks is ten minutes */
69 animation: blink
2s linear
300;
72 /* Define, load, and specify the custom font we use for the ticket
73 * date, time, and service name. */
75 font-family: "CharmBypass Regular";
77 url("data:font/woff2;base64,@CBPREGULAR@") format("woff2")
80 #origindest, #servicename, #tickettime, #ticketdate, #codetext, #zone {
81 font-family: "CharmBypass Regular", sans-serif;
85 font-family: "CharmBypass Bold";
87 url("data:font/woff2;base64,@CBPBOLD@") format("woff2")
91 font-family: "CharmBypass Bold", sans-serif;
94 /************************/
95 /* Scrolling animations */
96 /************************/
100 from { transform: translateX(
0%); }
101 to { transform: translateX(-
100%); }
105 animation: busroll
15s linear infinite;
110 @keyframes tramroll {
111 from { transform: translateX(
0%); }
112 to { transform: translateX(
100%); }
116 animation: tramroll
15s linear infinite;
121 @keyframes trainroll {
122 from { transform: translateX(
0%); }
123 to { transform: translateX(
100%); }
127 animation: trainroll
10s linear infinite;
132 @keyframes cloudsfloat {
133 from { transform: translateX(
0%); }
134 to { transform: translateX(-
50%); }
138 animation: cloudsfloat
25s linear infinite;
141 @keyframes cloudscopyfloat {
142 from { transform: translateX(
0%); }
143 to { transform: translateX(-
50%); }
147 animation: cloudscopyfloat
25s linear infinite;
152 @keyframes treespass {
153 from { transform: translateX(
0%); }
154 to { transform: translateX(-
50%); }
158 /* The trees move a little faster than the clouds */
159 animation: treespass
16s linear infinite;
162 @keyframes treescopypass {
163 from { transform: translateX(
0%); }
164 to { transform: translateX(-
50%); }
168 /* The trees move a little faster than the clouds */
169 animation: treescopypass
16s linear infinite;
174 @keyframes cityscroll {
175 from { transform: translateX(
0%); }
176 to { transform: translateX(-
50%); }
180 /* The city moves faster than the clouds but slower
182 animation: cityscroll
20s linear infinite;
185 @keyframes citycopyscroll {
186 from { transform: translateX(
0%); }
187 to { transform: translateX(-
50%); }
191 /* The city moves faster than the clouds but slower
193 animation: citycopyscroll
20s linear infinite;
201 <p><strong>got that
<em>transit equity
</em></strong></p>
205 <legend>Local Bus, Light Rail, or Metro
</legend>
208 Daily security code (optional):
216 pattern=
"[a-zA-Z0-9]*" />
218 <input type=
"hidden" name=
"servicename" value=
"BaltimoreLink" />
219 <input type=
"submit" name=
"go" value=
"Generate Ticket" />
224 <legend>Commuter Bus
</legend>
225 <input type=
"hidden" name=
"serviceid" value=
"R" />
226 <input type=
"hidden" name=
"servicename" value=
"Commuter Bus" />
230 Daily security code (optional):
238 pattern=
"[a-zA-Z0-9]*" />
242 Zone
<sup>†</sup>:
245 <input type=
"radio" required
249 <label for=
"zone1">Zone
1</label>
252 <input type=
"radio" required
256 <label for=
"zone2">Zone
2</label>
259 <input type=
"radio" required checked
263 <label for=
"zone3">Zone
3</label>
266 <input type=
"radio" required
270 <label for=
"zone4">Zone
4</label>
273 <input type=
"radio" required
277 <label for=
"zone5">Zone
5</label>
280 <input type=
"submit" name=
"go" value=
"Generate Ticket" />
284 On the MTA's PDF schedule for your route
290 <legend>MARC Train
</legend>
294 Daily security code (optional):
302 pattern=
"[a-zA-Z0-9]*" />
307 <input type=
"radio" required checked
311 <label for=
"origin1">Baltimore/Penn (Penn Line)
</label>
314 <input type=
"radio" required
318 <label for=
"origin2">Baltimore/Camden (Camden Line)
</label>
321 <input type=
"radio" required
325 <label for=
"origin3">Bowie State (Penn Line)
</label>
328 <input type=
"radio" required
332 <label for=
"origin4">BWI Airport (Penn Line)
</label>
335 <input type=
"radio" required
339 <label for=
"origin5">College Park (Camden Line)
</label>
342 <input type=
"radio" required
346 <label for=
"origin6">Seabrook (Penn Line)
</label>
349 <input type=
"radio" required
353 <label for=
"origin7">Washington D.C.
</label>
356 <input type=
"radio" required
360 <label for=
"origin8">West Baltimore (Penn Line)
</label>
365 <input type=
"radio" required
369 <label for=
"destination1">Baltimore/Penn (Penn Line)
</label>
372 <input type=
"radio" required
376 <label for=
"destination2">Baltimore/Camden (Camden Line)
</label>
379 <input type=
"radio" required
383 <label for=
"destination3">Bowie State (Penn Line)
</label>
386 <input type=
"radio" required
390 <label for=
"destination4">BWI Airport (Penn Line)
</label>
393 <input type=
"radio" required
397 <label for=
"destination5">College Park (Camden Line)
</label>
400 <input type=
"radio" required
404 <label for=
"destination6">Seabrook (Penn Line)
</label>
407 <input type=
"radio" required checked
411 <label for=
"destination7">Washington D.C.
</label>
414 <input type=
"radio" required
418 <label for=
"destination8">West Baltimore (Penn Line)
</label>
421 <p id=
"marc-form-errors">OK
</p>
423 <input type=
"hidden" name=
"serviceid" value=
"R" />
424 <input type=
"hidden" name=
"servicename" value=
"MARC Train" />
428 value=
"Generate Ticket" />
437 <a href=
"https://michael.orlitzky.com/articles/charmbypass_pt._1%3A_introduction.xhtml">
442 <a href=
"https://michael.orlitzky.com/articles/charmbypass_pt._2%3A_analysis.xhtml">
454 * Center the ticket within the browser by translating the SVG
455 * until the ticket and the viewport centerlines coincide.
457 function center_ticket() {
458 /* We're relying on the SVG being the full height of the
459 * viewport already, and on the aspect ratio being
460 * preserved. First, find the center of the ticket. */
461 const r = document.getElementById("ticket").getBoundingClientRect();
462 const c = r.left + (r.width /
2);
464 /* That's the center-line of the ticket. We want to move it to
465 * the center-line of the viewport. */
466 const vc = document.documentElement.clientWidth /
2;
468 /* This is how much we need to translate the SVG */
469 const hdelta = vc - c;
471 /* But before we can set the absolute left-coordinate of the
472 * SVG, we need to know where it is now. Note: without the
473 * "px" this doesn't default to pixels like CSS does. */
474 const svg = document.querySelector("svg");
475 svg.style.left = (svg.getBoundingClientRect().left + hdelta) + "px";
480 * Set the service identifier from the querystring if it's there.
481 * Otherwise, leave it at the default of "F".
483 function set_service_id() {
484 const sid = document.getElementById("serviceid");
486 /* Get the "serviceid" from the querystring if it's there */
487 const params = new URLSearchParams(document.location.search);
488 if (params.get("serviceid")) {
489 sid.textContent = params.get("serviceid");
495 * Set the service name from the querystring if it's there.
496 * Otherwise, leave it at the default of "BaltimoreLink".
498 function set_service_name() {
499 const sid = document.getElementById("servicename");
501 /* Get the "servicename" from the querystring if it's there */
502 const params = new URLSearchParams(document.location.search);
503 if (params.get("servicename")) {
504 sid.textContent = params.get("servicename");
510 * Set the zone from the given "zone" parameter and then unhide it.
512 function set_zone(zone) {
513 const z = document.getElementById("zone");
515 z.textContent = zone;
516 z.style.display = "block"; /* hidden by default */
521 * Resize the ticket background based on the service name.
522 * The BaltimoreLink, Commuter Bus, and MARC Train tickets
523 * are all different heights and are arranged vertically a
526 * Rather than design three completelty separate tickets and
527 * then have to keep track of which one we're using, I have
528 * instead decided to use one ticket and to reposition it
529 * on-the-fly based on the service name. This is necessarily
530 * a bit ugly because it involves a lot of magic numbers that
531 * can only be explained if you open up inkscape with a CharmPass
532 * screenshot to see where things belong and how to get them there.
534 * The SVG was designed with BaltimoreLink in mind, so this
535 * is a no-op if the service is BaltimoreLink.
537 function resize_ticket() {
538 /* Get the "servicename" from the querystring if it's there */
539 const params = new URLSearchParams(document.location.search);
540 const tbg = document.getElementById("ticketbg");
541 const t = document.getElementById("ticket");
542 const sn = document.getElementById("servicename");
544 if (params.get("servicename") === "Commuter Bus") {
545 /* The top of the background is initially at y=
246.859, and
546 * we scale it by a factor of
1.12 to y=
276.482 for a change
547 * of
29.623. So after we scale it, we translate it upwards
548 * by that amount to put it back where it started. */
549 tbg.setAttribute("transform", "translate(
0 -
29.623) scale(
1 1.12)");
551 /* Now translate the entire ticket up by the magic amount,
1/
5
552 * of the size change we made to the background. This ratio
553 * was found by measuring pixels in side-by-side screenshots
554 * of BaltimoreLink and Commuter Bus tickets. */
555 t.setAttribute("transform", "translate(
0 -
9.33)");
557 /* More magic numbers discovered by comparing the two
558 * tickets overlayed in inkscape */
559 sn.setAttribute("transform", "translate(
0 64.28)");
561 else if (params.get("servicename") === "MARC Train") {
562 /* insane tricks are explained above */
563 tbg.setAttribute("transform",
564 "translate(
0 -
72.378) scale(
1 1.2932)");
565 t.setAttribute("transform", "translate(
0 -
67.17)");
566 sn.setAttribute("transform", "translate(
0 131.0)");
572 * Set the security code from the querystring if it was given;
573 * otherwise generate a random code.
575 function set_code() {
576 const ct = document.getElementById("codetext");
578 /* Get the "code" from the querystring if it's there */
579 const params = new URLSearchParams(document.location.search);
580 if (params.get("code")) {
581 ct.textContent = params.get("code").toUpperCase();
584 /* Otherwise, use a random code */
585 const bucket = ["
0","
1","
2","
3","
4","
5","
6","
7","
8","
9",
586 "A","B","C","D","E","F","G","H","I","J",
587 "K","L","M","N","O","P","Q","R","S","T",
588 "U","V","W","X","Y","Z"];
590 /* Two random ints between
0 and
35 */
591 const i1 = Math.floor(Math.random() *
36);
592 const i2 = Math.floor(Math.random() *
36);
593 const d1 = bucket[i1];
594 const d2 = bucket[i2];
595 ct.textContent = d1 + d2;
601 * Center the security code within its container.
603 * Some codes like "II" and "WW" can take up wildly different
604 * amounts of horizonetal space, but they should always be
605 * centered inside their little red box. This turns out to be
606 * harder than it sounds because we can only find the width of
607 * the code in browser coordinates, whereas its "x" coordinate
608 * is in SVG coordinates. Anyway, we do it.
610 function center_code() {
611 /* Center the security code inside its red box */
612 const ct = document.getElementById("codetext");
613 const bg = document.getElementById("codebg");
615 /* First, find the center of the red box */
616 const r1 = bg.getBoundingClientRect();
617 const c1 = r1.left + (r1.width /
2);
619 /* Now the center of the code text */
620 const r2 = ct.getBoundingClientRect();
621 const c2 = r2.left + (r2.width /
2);
623 /* What do we add to c2 to make it equal to c1? */
624 const hdelta = c1 - c2;
626 /* We've measured everything so far in "client rect"
627 * coordinates, because that's the only available measurement
628 * we have for the width of the
<text> element. But when we
629 * reposition that
<text> element, it will be by adjusting its
630 * "x" attribute, and that attribute uses a different coordinate
631 * system than the client rect does. Specifically, "x" refers to
632 * an offset within the SVG's coordinate system, and the client
633 * rect coordinates are pixels on-screen. To convert between the
634 * two, we can take the "width" attribute of the background
635 * element and compare it to the width of the background
636 * element's client rect. Since the size of the background is
637 * fixed, this should give us a multiplier that turns client rect
638 * distances (what we have) into SVG distances (what we want) */
639 const client_to_svg = parseFloat(bg.getAttribute("width"))/r1.width;
641 /* Convert hdelta from client rect to SVG coordinates */
642 const svg_hdelta = hdelta * client_to_svg;
644 /* Since this
<text> element has an "x" attribute it's easier for
645 * us to shift that than it is to mess with the "left" style. */
646 ct.setAttribute("x", parseFloat(ct.getAttribute("x")) + svg_hdelta);
650 * Set the ticket's expiration date and time.
652 * BaltimoreLink and MARC Train tickets expire after
90 minutes;
653 * while Commuter Bus tickets expire after
10 minutes.
655 function set_ticket_expiry() {
656 /* There are two parameters, time and date, that we store in one
657 * underlying "date" variable. */
658 const date = new Date();
660 /* BaltimoreLink and MARC Train */
662 const params = new URLSearchParams(document.location.search);
663 if (params.get("servicename") === "Commuter Bus") {
664 /* Commuter bus tickets are only valid for ten minutes */
668 /* We use the low-level get/setTime to change the number of
669 * milliseconds since the epoch that this date represents
670 * Obviously correct, and avoids all suspicious corner cases
671 * for a few more decades. */
672 date.setTime(date.getTime() + (minutes*
60*
1000));
674 tt = document.getElementById("tickettime");
675 tt.textContent = date.toLocaleTimeString();
677 const td = document.getElementById("ticketdate");
683 td.textContent = date.toLocaleDateString("en-US", dateopts);
688 * Swap the day/night sky colors.
690 * We use CSS classes to keep track of the current state because
691 * it's a tiny bit cleaner than a global variable, but for some
692 * reason we can't use those same classes to actually change the
693 * color. (The classes, change, but the color doesn't.) Rather
694 * than waste time trying to explain this, we just set the "fill"
695 * attribute ourselves whenever we swap classes.
697 function swap_day_night() {
698 const sky = document.getElementById("sky");
700 if (sky.getAttribute("class") === "night") {
701 sky.setAttribute("fill", "#efb02f");
702 sky.setAttribute("class", "day");
705 /* Put this case second so that the first time the
706 * screen is tapped (when there are no classes on
707 * the sky element) the color still changes. */
708 sky.setAttribute("fill", "#
143b66");
709 sky.setAttribute("class", "night");
715 * Compute the zone (string) for the given origin/destination pair.
717 * If we don't know it or if you chose in invalid pair (destination
718 * not on the same line as your origin?) then null is returned.
720 function compute_marc_zone(src, dest) {
722 /* Sorted on the first component, then the second.
757 /* Forward direction key for zone_map */
758 const fwd = src +
"_" + dest;
760 /* Reverse direction key for zone_map. The zone_map only
761 * has them listed in one direction, so we check both
762 * directions here. */
763 const rev = dest +
"_" + src;
765 /* The default. Obviously wrong for when we don't
766 * have the necessary data. */
770 zone = zone_map[fwd];
772 else if (zone_map[rev]) {
773 zone = zone_map[rev];
776 /* Convert the number to a string */
793 * Compute and set the zone.
795 * We can be given a zone in two ways. First, on Commuter Bus
796 * tickets, it is given explicitly via the querystring. But
797 * It can also be specified implicitly via the origin and
798 * destination on a MARC Train ticket. Here we try both and
799 * then call set_zone() with the result if something worked.
801 function compute_and_set_zone() {
802 const params = new URLSearchParams(document.location.search);
803 const src = params.get(
"origin");
804 const dest = params.get(
"destination");
807 /* MARC Train. We can assume that compute_marc_zone() doesn't
808 * return null because that's part of our form validation. */
809 const zone = compute_marc_zone(src, dest);
812 else if (params.get(
"zone")) {
814 set_zone(params.get(
"zone"));
820 * Set the origin and destination for the MARC Train if they
821 * were provided, and unhide them if so.
823 function set_marc_origin_destination() {
824 const params = new URLSearchParams(document.location.search);
825 if (!params.get(
"origin") || !params.get(
"destination")) {
829 const src = params.get(
"origin");
830 const dest = params.get(
"destination");
832 /* origindest contains both the origin and destination */
833 const origindest = document.getElementById(
"origindest");
835 const origin = document.getElementById(
"origin");
836 const destination = document.getElementById(
"destination");
838 origin.textContent = params.get(
"origin");
839 destination.textContent = params.get(
"destination");
841 origindest.style.display =
"block"; /* hidden by default */
846 * Hide the menu and display the ticket. This is what happens
847 * when you submit the form.
850 /* To create our
"window" onto the scene, we're going to slide the
851 * SVG off the left-hand side of the screen, and we don't want
852 * scroll bars to appear. */
853 document.body.style.overflow =
"hidden";
854 document.body.style.margin =
"0";
855 document.body.style.padding =
"0";
857 const svg = document.querySelector(
"svg");
858 const menu = document.getElementById(
"menu");
859 svg.style.display =
"initial";
860 menu.style.display =
"none";
864 * Determine if the user agent is mobile Firefox.
866 function ua_is_mobile_ff() {
867 const ua = navigator.userAgent.toLowerCase();
868 return (ua.includes(
"firefox") && ua.includes(
"mobile"));
872 * Validate the MARC form's origin/destination.
874 * We don't want the user to be able to choose a pair of stops that
875 * aren't actually connected by the same MARC line. If we don't have
876 * zone information for the (origin,destination) pair, that indicates
877 * that it's probably not a valid choice; otherwise I would have
878 * filled in the information from the CharmPass app already.
880 * All browsers except mobile Firefox let us call setCustomValidity()
881 * to provide an error message that is displayed if the user tries
882 * to submit invalid choices. But amazingly, mobile Firefox does not:
884 * https://bugzilla.mozilla.org/show_bug.cgi?id=
1510450
886 * Instead we have to work around it (in that one browser) by
887 * showing/hiding a paragraph that we fill with the errors.
889 function validate_origin_destination(event) {
890 const origins = document.getElementsByName(
"origin");
891 const destinations = document.getElementsByName(
"destination");
892 const mfe = document.getElementById(
"marc-form-errors");
893 const marcsubmit = document.getElementById(
"marc-submit");
895 if (ua_is_mobile_ff()) {
896 /* Even though this is only for one browser, empty paragraphs
897 * are handled inconsistently and should be avoided as a rule.
898 * So, we make it say
"OK" before we hide it. */
899 mfe.textContent =
"OK";
900 mfe.style.visibility =
"hidden";
901 marcsubmit.disabled = false;
906 origins.forEach((x) =
> { if (x.checked) src = x; })
907 destinations.forEach((x) =
> {
908 if (x.checked) dest = x;
910 /* clear all errors before possibly setting one */
911 x.setCustomValidity('');
914 if (src.value === dest.value) {
915 let err = "Origin and destination are the same";
916 dest.setCustomValidity(err);
918 if (ua_is_mobile_ff()) {
919 mfe.textContent = err;
920 mfe.style.visibility = "visible";
921 marcsubmit.disabled = true;
924 else if (compute_marc_zone(src.value, dest.value) === null) {
925 let err = "Origin and destination are on different lines";
926 dest.setCustomValidity(err);
928 if (ua_is_mobile_ff()) {
929 mfe.textContent = err;
930 mfe.style.visibility = "visible";
931 marcsubmit.disabled = true;
936 /*****************************************************/
937 /* Add event handlers for all of the functions above */
938 /*****************************************************/
940 const params = new URLSearchParams(document.location.search);
941 if (params.get("go")) {
942 /* First unhide the SVG (swap it with the form) */
943 window.addEventListener("load", go);
945 /* Center the ticket once when the page has loaded */
946 window.addEventListener("load", center_ticket);
948 /* Re-center the ticket when the window is resized */
949 window.addEventListener("resize", center_ticket);
951 /* Set the service identifier when the page has loaded */
952 window.addEventListener("load", set_service_id);
954 /* Set the service name when the page has loaded */
955 window.addEventListener("load", set_service_name);
957 /* Resize the ticket background if necessary */
958 window.addEventListener("load", resize_ticket);
960 /* Set the security code text when the page has loaded */
961 window.addEventListener("load", set_code);
963 /* Center the security code text when the page has loaded; in
964 * particular, after we set the code. */
965 window.addEventListener("load", center_code);
967 /* Set the ticket expiration date/time upon page load */
968 window.addEventListener("load", set_ticket_expiry);
970 /* Set the MARC Train origin and destination, if applicable */
971 window.addEventListener("load", set_marc_origin_destination);
973 /* Compute and set the zone, if applicable */
974 window.addEventListener("load", compute_and_set_zone);
976 /* Swap colors when the screen is tapped */
977 document.body.addEventListener("click", swap_day_night);
980 /* If we haven't submitted the form yet, set up change handlers
981 * for the origin/destination radio buttons that validate that
982 * the origin and destination are on the same line. */
983 document.getElementsByName("origin").forEach(
984 (x) =
> x.addEventListener("change", validate_origin_destination)
986 document.getElementsByName("destination").forEach(
987 (x) =
> x.addEventListener("change", validate_origin_destination)
990 /* Also do it when the page loads, because firefox likes to
991 * remember your selection even after the page reloads. */
992 window.addEventListener("load", validate_origin_destination);
994 /* Finally, we have to babysit mobile Firefox, who doesn't
995 * support HTML5 form validation going into
2024. Turn on
996 * the little form errors paragraph so we can toggle its
997 * visibility (and make it display the error) when the user
998 * makes an invalid selection. */
999 window.addEventListener("load", () =
> {
1000 if (ua_is_mobile_ff()) {
1001 const mfe = document.getElementById("marc-form-errors");
1002 mfe.style.display = "block";