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 (
<a href=
"https://michael.orlitzky.com/articles/charmbypass_pt._1%3A_introduction.xhtml#daily-security-code">optional
</a>):
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 (
<a href=
"https://michael.orlitzky.com/articles/charmbypass_pt._1%3A_introduction.xhtml#daily-security-code">optional
</a>):
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 (
<a href=
"https://michael.orlitzky.com/articles/charmbypass_pt._1%3A_introduction.xhtml#daily-security-code">optional
</a>):
302 pattern=
"[a-zA-Z0-9]*" />
307 <input type=
"radio" required checked
311 <label for=
"origin_BAL">Baltimore/Penn (Penn Line)
</label>
314 <input type=
"radio" required
318 <label for=
"origin_BCA">Baltimore/Camden (Camden Line)
</label>
321 <input type=
"radio" required
325 <label for=
"origin_BWE">Bowie State (Penn Line)
</label>
328 <input type=
"radio" required
332 <label for=
"origin_BWI">BWI Airport (Penn Line)
</label>
335 <input type=
"radio" required
339 <label for=
"origin_CPK">College Park (Camden Line)
</label>
342 <input type=
"radio" required
346 <label for=
"origin_HAE">Halethorpe (Penn Line)
</label>
349 <input type=
"radio" required
353 <label for=
"origin_SEB">Seabrook (Penn Line)
</label>
356 <input type=
"radio" required
360 <label for=
"origin_WAS">Washington D.C.
</label>
363 <input type=
"radio" required
367 <label for=
"origin_WBL">West Baltimore (Penn Line)
</label>
372 <input type=
"radio" required
376 <label for=
"destination_BAL">Baltimore/Penn (Penn Line)
</label>
379 <input type=
"radio" required
383 <label for=
"destination_BCA">Baltimore/Camden (Camden Line)
</label>
386 <input type=
"radio" required
390 <label for=
"destination_BWE">Bowie State (Penn Line)
</label>
393 <input type=
"radio" required
397 <label for=
"destination_BWI">BWI Airport (Penn Line)
</label>
400 <input type=
"radio" required
404 <label for=
"destination_CPK">College Park (Camden Line)
</label>
407 <input type=
"radio" required
411 <label for=
"destination_HAE">Halethorpe (Penn Line)
</label>
414 <input type=
"radio" required
418 <label for=
"destination_SEB">Seabrook (Penn Line)
</label>
421 <input type=
"radio" required checked
425 <label for=
"destination_WAS">Washington D.C.
</label>
428 <input type=
"radio" required
432 <label for=
"destination_WBL">West Baltimore (Penn Line)
</label>
435 <p id=
"marc-form-errors">OK
</p>
437 <input type=
"hidden" name=
"serviceid" value=
"R" />
438 <input type=
"hidden" name=
"servicename" value=
"MARC Train" />
442 value=
"Generate Ticket" />
451 <a href=
"https://michael.orlitzky.com/articles/charmbypass_pt._1%3A_introduction.xhtml">
456 <a href=
"https://michael.orlitzky.com/articles/charmbypass_pt._2%3A_analysis.xhtml">
468 * Center the ticket within the browser by translating the SVG
469 * until the ticket and the viewport centerlines coincide.
471 function center_ticket() {
472 /* We're relying on the SVG being the full height of the
473 * viewport already, and on the aspect ratio being
474 * preserved. First, find the center of the ticket. */
475 const r = document.getElementById("ticket").getBoundingClientRect();
476 const c = r.left + (r.width /
2);
478 /* That's the center-line of the ticket. We want to move it to
479 * the center-line of the viewport. */
480 const vc = document.documentElement.clientWidth /
2;
482 /* This is how much we need to translate the SVG */
483 const hdelta = vc - c;
485 /* But before we can set the absolute left-coordinate of the
486 * SVG, we need to know where it is now. Note: without the
487 * "px" this doesn't default to pixels like CSS does. */
488 const svg = document.querySelector("svg");
489 svg.style.left = (svg.getBoundingClientRect().left + hdelta) + "px";
494 * Set the service identifier from the querystring if it's there.
495 * Otherwise, leave it at the default of "F".
497 function set_service_id() {
498 const sid = document.getElementById("serviceid");
500 /* Get the "serviceid" from the querystring if it's there */
501 const params = new URLSearchParams(document.location.search);
502 if (params.get("serviceid")) {
503 sid.textContent = params.get("serviceid");
509 * Set the service name from the querystring if it's there.
510 * Otherwise, leave it at the default of "BaltimoreLink".
512 function set_service_name() {
513 const sid = document.getElementById("servicename");
515 /* Get the "servicename" from the querystring if it's there */
516 const params = new URLSearchParams(document.location.search);
517 if (params.get("servicename")) {
518 sid.textContent = params.get("servicename");
524 * Set the zone from the given "zone" parameter and then unhide it.
526 function set_zone(zone) {
527 const z = document.getElementById("zone");
529 z.textContent = zone;
530 z.style.display = "block"; /* hidden by default */
535 * Resize the ticket background based on the service name.
536 * The BaltimoreLink, Commuter Bus, and MARC Train tickets
537 * are all different heights and are arranged vertically a
540 * Rather than design three completelty separate tickets and
541 * then have to keep track of which one we're using, I have
542 * instead decided to use one ticket and to reposition it
543 * on-the-fly based on the service name. This is necessarily
544 * a bit ugly because it involves a lot of magic numbers that
545 * can only be explained if you open up inkscape with a CharmPass
546 * screenshot to see where things belong and how to get them there.
548 * The SVG was designed with BaltimoreLink in mind, so this
549 * is a no-op if the service is BaltimoreLink.
551 function resize_ticket() {
552 /* Get the "servicename" from the querystring if it's there */
553 const params = new URLSearchParams(document.location.search);
554 const tbg = document.getElementById("ticketbg");
555 const t = document.getElementById("ticket");
556 const sn = document.getElementById("servicename");
558 if (params.get("servicename") === "Commuter Bus") {
559 /* The top of the background is initially at y=
246.859, and
560 * we scale it by a factor of
1.12 to y=
276.482 for a change
561 * of
29.623. So after we scale it, we translate it upwards
562 * by that amount to put it back where it started. */
563 tbg.setAttribute("transform", "translate(
0 -
29.623) scale(
1 1.12)");
565 /* Now translate the entire ticket up by the magic amount,
1/
5
566 * of the size change we made to the background. This ratio
567 * was found by measuring pixels in side-by-side screenshots
568 * of BaltimoreLink and Commuter Bus tickets. */
569 t.setAttribute("transform", "translate(
0 -
9.33)");
571 /* More magic numbers discovered by comparing the two
572 * tickets overlayed in inkscape */
573 sn.setAttribute("transform", "translate(
0 64.28)");
575 else if (params.get("servicename") === "MARC Train") {
576 /* insane tricks are explained above */
577 tbg.setAttribute("transform",
578 "translate(
0 -
72.378) scale(
1 1.2932)");
579 t.setAttribute("transform", "translate(
0 -
67.17)");
580 sn.setAttribute("transform", "translate(
0 131.0)");
586 * Set the security code from the querystring if it was given;
587 * otherwise generate a random code.
589 function set_code() {
590 const ct = document.getElementById("codetext");
592 /* Get the "code" from the querystring if it's there */
593 const params = new URLSearchParams(document.location.search);
594 if (params.get("code")) {
595 ct.textContent = params.get("code").toUpperCase();
598 /* Otherwise, use a random code */
599 const bucket = ["
0","
1","
2","
3","
4","
5","
6","
7","
8","
9",
600 "A","B","C","D","E","F","G","H","I","J",
601 "K","L","M","N","O","P","Q","R","S","T",
602 "U","V","W","X","Y","Z"];
604 /* Two random ints between
0 and
35 */
605 const i1 = Math.floor(Math.random() *
36);
606 const i2 = Math.floor(Math.random() *
36);
607 const d1 = bucket[i1];
608 const d2 = bucket[i2];
609 ct.textContent = d1 + d2;
615 * Center the security code within its container.
617 * Some codes like "II" and "WW" can take up wildly different
618 * amounts of horizonetal space, but they should always be
619 * centered inside their little red box. This turns out to be
620 * harder than it sounds because we can only find the width of
621 * the code in browser coordinates, whereas its "x" coordinate
622 * is in SVG coordinates. Anyway, we do it.
624 function center_code() {
625 /* Center the security code inside its red box */
626 const ct = document.getElementById("codetext");
627 const bg = document.getElementById("codebg");
629 /* First, find the center of the red box */
630 const r1 = bg.getBoundingClientRect();
631 const c1 = r1.left + (r1.width /
2);
633 /* Now the center of the code text */
634 const r2 = ct.getBoundingClientRect();
635 const c2 = r2.left + (r2.width /
2);
637 /* What do we add to c2 to make it equal to c1? */
638 const hdelta = c1 - c2;
640 /* We've measured everything so far in "client rect"
641 * coordinates, because that's the only available measurement
642 * we have for the width of the
<text> element. But when we
643 * reposition that
<text> element, it will be by adjusting its
644 * "x" attribute, and that attribute uses a different coordinate
645 * system than the client rect does. Specifically, "x" refers to
646 * an offset within the SVG's coordinate system, and the client
647 * rect coordinates are pixels on-screen. To convert between the
648 * two, we can take the "width" attribute of the background
649 * element and compare it to the width of the background
650 * element's client rect. Since the size of the background is
651 * fixed, this should give us a multiplier that turns client rect
652 * distances (what we have) into SVG distances (what we want) */
653 const client_to_svg = parseFloat(bg.getAttribute("width"))/r1.width;
655 /* Convert hdelta from client rect to SVG coordinates */
656 const svg_hdelta = hdelta * client_to_svg;
658 /* Since this
<text> element has an "x" attribute it's easier for
659 * us to shift that than it is to mess with the "left" style. */
660 ct.setAttribute("x", parseFloat(ct.getAttribute("x")) + svg_hdelta);
664 * Set the ticket's expiration date and time.
666 * BaltimoreLink and MARC Train tickets expire after
90 minutes;
667 * while Commuter Bus tickets expire after
10 minutes.
669 function set_ticket_expiry() {
670 /* There are two parameters, time and date, that we store in one
671 * underlying "date" variable. */
672 const date = new Date();
674 /* BaltimoreLink and MARC Train */
676 const params = new URLSearchParams(document.location.search);
677 if (params.get("servicename") === "Commuter Bus") {
678 /* Commuter bus tickets are only valid for ten minutes */
682 /* We use the low-level get/setTime to change the number of
683 * milliseconds since the epoch that this date represents
684 * Obviously correct, and avoids all suspicious corner cases
685 * for a few more decades. */
686 date.setTime(date.getTime() + (minutes*
60*
1000));
688 tt = document.getElementById("tickettime");
689 tt.textContent = date.toLocaleTimeString();
691 const td = document.getElementById("ticketdate");
697 td.textContent = date.toLocaleDateString("en-US", dateopts);
702 * Swap the day/night sky colors.
704 * We use CSS classes to keep track of the current state because
705 * it's a tiny bit cleaner than a global variable, but for some
706 * reason we can't use those same classes to actually change the
707 * color. (The classes, change, but the color doesn't.) Rather
708 * than waste time trying to explain this, we just set the "fill"
709 * attribute ourselves whenever we swap classes.
711 function swap_day_night() {
712 const sky = document.getElementById("sky");
714 if (sky.getAttribute("class") === "night") {
715 sky.setAttribute("fill", "#efb02f");
716 sky.setAttribute("class", "day");
719 /* Put this case second so that the first time the
720 * screen is tapped (when there are no classes on
721 * the sky element) the color still changes. */
722 sky.setAttribute("fill", "#
143b66");
723 sky.setAttribute("class", "night");
729 * Compute the zone (string) for the given origin/destination pair.
731 * If we don't know it or if you chose in invalid pair (destination
732 * not on the same line as your origin?) then null is returned.
734 function compute_marc_zone(src, dest) {
736 /* Sorted on the first component, then the second.
771 /* Forward direction key for zone_map */
772 const fwd = src +
"_" + dest;
774 /* Reverse direction key for zone_map. The zone_map only
775 * has them listed in one direction, so we check both
776 * directions here. */
777 const rev = dest +
"_" + src;
779 /* The default. Obviously wrong for when we don't
780 * have the necessary data. */
784 zone = zone_map[fwd];
786 else if (zone_map[rev]) {
787 zone = zone_map[rev];
790 /* Convert the number to a string */
807 * Compute and set the zone.
809 * We can be given a zone in two ways. First, on Commuter Bus
810 * tickets, it is given explicitly via the querystring. But
811 * It can also be specified implicitly via the origin and
812 * destination on a MARC Train ticket. Here we try both and
813 * then call set_zone() with the result if something worked.
815 function compute_and_set_zone() {
816 const params = new URLSearchParams(document.location.search);
817 const src = params.get(
"origin");
818 const dest = params.get(
"destination");
821 /* MARC Train. We can assume that compute_marc_zone() doesn't
822 * return null because that's part of our form validation. */
823 const zone = compute_marc_zone(src, dest);
826 else if (params.get(
"zone")) {
828 set_zone(params.get(
"zone"));
834 * Set the origin and destination for the MARC Train if they
835 * were provided, and unhide them if so.
837 function set_marc_origin_destination() {
838 const params = new URLSearchParams(document.location.search);
839 if (!params.get(
"origin") || !params.get(
"destination")) {
843 const src = params.get(
"origin");
844 const dest = params.get(
"destination");
846 /* origindest contains both the origin and destination */
847 const origindest = document.getElementById(
"origindest");
849 const origin = document.getElementById(
"origin");
850 const destination = document.getElementById(
"destination");
852 origin.textContent = params.get(
"origin");
853 destination.textContent = params.get(
"destination");
855 origindest.style.display =
"block"; /* hidden by default */
860 * Hide the menu and display the ticket. This is what happens
861 * when you submit the form.
864 /* To create our
"window" onto the scene, we're going to slide the
865 * SVG off the left-hand side of the screen, and we don't want
866 * scroll bars to appear. */
867 document.body.style.overflow =
"hidden";
868 document.body.style.margin =
"0";
869 document.body.style.padding =
"0";
871 const svg = document.querySelector(
"svg");
872 const menu = document.getElementById(
"menu");
873 svg.style.display =
"initial";
874 menu.style.display =
"none";
878 * Determine if the user agent is mobile Firefox.
880 function ua_is_mobile_ff() {
881 const ua = navigator.userAgent.toLowerCase();
882 return (ua.includes(
"firefox") && ua.includes(
"mobile"));
886 * Validate the MARC form's origin/destination.
888 * We don't want the user to be able to choose a pair of stops that
889 * aren't actually connected by the same MARC line. If we don't have
890 * zone information for the (origin,destination) pair, that indicates
891 * that it's probably not a valid choice; otherwise I would have
892 * filled in the information from the CharmPass app already.
894 * All browsers except mobile Firefox let us call setCustomValidity()
895 * to provide an error message that is displayed if the user tries
896 * to submit invalid choices. But amazingly, mobile Firefox does not:
898 * https://bugzilla.mozilla.org/show_bug.cgi?id=
1510450
900 * Instead we have to work around it (in that one browser) by
901 * showing/hiding a paragraph that we fill with the errors.
903 function validate_origin_destination(event) {
904 const origins = document.getElementsByName(
"origin");
905 const destinations = document.getElementsByName(
"destination");
906 const mfe = document.getElementById(
"marc-form-errors");
907 const marcsubmit = document.getElementById(
"marc-submit");
909 if (ua_is_mobile_ff()) {
910 /* Even though this is only for one browser, empty paragraphs
911 * are handled inconsistently and should be avoided as a rule.
912 * So, we make it say
"OK" before we hide it. */
913 mfe.textContent =
"OK";
914 mfe.style.visibility =
"hidden";
915 marcsubmit.disabled = false;
920 origins.forEach((x) =
> { if (x.checked) src = x; })
921 destinations.forEach((x) =
> {
922 if (x.checked) dest = x;
924 /* clear all errors before possibly setting one */
925 x.setCustomValidity('');
928 if (src.value === dest.value) {
929 let err = "Origin and destination are the same";
930 dest.setCustomValidity(err);
932 if (ua_is_mobile_ff()) {
933 mfe.textContent = err;
934 mfe.style.visibility = "visible";
935 marcsubmit.disabled = true;
938 else if (compute_marc_zone(src.value, dest.value) === null) {
939 let err = "Origin and destination are on different lines";
940 dest.setCustomValidity(err);
942 if (ua_is_mobile_ff()) {
943 mfe.textContent = err;
944 mfe.style.visibility = "visible";
945 marcsubmit.disabled = true;
950 /*****************************************************/
951 /* Add event handlers for all of the functions above */
952 /*****************************************************/
954 const params = new URLSearchParams(document.location.search);
955 if (params.get("go")) {
956 /* First unhide the SVG (swap it with the form) */
957 window.addEventListener("load", go);
959 /* Center the ticket once when the page has loaded */
960 window.addEventListener("load", center_ticket);
962 /* Re-center the ticket when the window is resized */
963 window.addEventListener("resize", center_ticket);
965 /* Set the service identifier when the page has loaded */
966 window.addEventListener("load", set_service_id);
968 /* Set the service name when the page has loaded */
969 window.addEventListener("load", set_service_name);
971 /* Resize the ticket background if necessary */
972 window.addEventListener("load", resize_ticket);
974 /* Set the security code text when the page has loaded */
975 window.addEventListener("load", set_code);
977 /* Center the security code text when the page has loaded; in
978 * particular, after we set the code. */
979 window.addEventListener("load", center_code);
981 /* Set the ticket expiration date/time upon page load */
982 window.addEventListener("load", set_ticket_expiry);
984 /* Set the MARC Train origin and destination, if applicable */
985 window.addEventListener("load", set_marc_origin_destination);
987 /* Compute and set the zone, if applicable */
988 window.addEventListener("load", compute_and_set_zone);
990 /* Swap colors when the screen is tapped */
991 document.body.addEventListener("click", swap_day_night);
994 /* If we haven't submitted the form yet, set up change handlers
995 * for the origin/destination radio buttons that validate that
996 * the origin and destination are on the same line. */
997 document.getElementsByName("origin").forEach(
998 (x) =
> x.addEventListener("change", validate_origin_destination)
1000 document.getElementsByName("destination").forEach(
1001 (x) =
> x.addEventListener("change", validate_origin_destination)
1004 /* Also do it when the page loads, because firefox likes to
1005 * remember your selection even after the page reloads. */
1006 window.addEventListener("load", validate_origin_destination);
1008 /* Finally, we have to babysit mobile Firefox, who doesn't
1009 * support HTML5 form validation going into
2024. Turn on
1010 * the little form errors paragraph so we can toggle its
1011 * visibility (and make it display the error) when the user
1012 * makes an invalid selection. */
1013 window.addEventListener("load", () =
> {
1014 if (ua_is_mobile_ff()) {
1015 const mfe = document.getElementById("marc-form-errors");
1016 mfe.style.display = "block";