Annie Ottati and Sarah Taurchini in “To Tell My Story: A Hamlet Fanfic.” (Teresa Castracane)

Shakespeare never actually specifies that Hamlet has taken to eating lunch in a school bathroom. But it’s easy to imagine that the moody prince of Denmark — a guy who calls the world “stale, flat, and unprofitable” and wishes that his own flesh would melt — might be anti-social and depressed enough to dine in a toilet stall. Turn him into a modern teenage girl, and the probability rises further.

The Elsinore-prowling man in black becomes Elsie, an Internet-roaming teenage girl in black, in “To Tell My Story: A Hamlet Fan­fic ,” an ingenious, if sometimes strenuously jokey, new play by Alexandra Petri. Produced by the Welders, the D.C. playwrights’ collective, “To Tell My Story” teems with clever correlatives to Shakespeare’s tragedy. To start with, Hamlet’s near-mythic angst has an equivalent in the rebelliousness and restless creativity of Elsie, whose weekday survival strategies include scarfing lunch over a commode.

In director Megan Behm’s watchable production, the alienation of Elsie (an aptly intense Annie Ottati) and her sometime friend Ophelia (a suitably pensive Sarah Taurchini) makes for moments that are poignant as well as funny. Mostly, though, the play is just funny. It regales us, for instance, with snippets from the fan fiction that Elsie (screen name: InkyCloak31) writes and posts on the Internet — bursts of expression that correspond to Hamlet’s monologues. (Petri is a humor writer for The Washington Post.)

Avidly read by the play’s endearing narrator, a girl named Horatio (the charismatic Chloe Mikala), Elsie’s fiction chronicles love affairs between the likes of Abraham Lincoln and his friend Joshua Speed, Middle Earth and Starbucks-style coffee, and wizards resembling Harry Potter and Draco Malfoy. Choice excerpts play out regularly for us, with actors Shravan Amin and Colin Connor channeling the passion in deliberately soapy fashion. We even get to hear some of the hilariously bad prose. (“They leaned closer, like two poplars that had been planted incorrectly.”)

Chloe Mikala as Horatio in “To Tell My Story: A Hamlet Fanfic.” (Teresa Castracane)

While purporting to contemplate the modern phenomenon that is fan fiction, “To Tell My Story” is of course itself a fan fiction, riffing off Shakespearean characters and events. Instead of encountering Dad’s ghost, as Hamlet does, Elsie gets a posthumous Snapchat message from her father. Instead of bonding over a Wittenberg connection, Elsie recruits Horatio as a beta reader.

At times, “To Tell My Story” gets carried away by its enthusiasm for jokes: A “Hamilton” gag feels tacked on, for example. More satisfying are the moments when Petri fuses her sly “Hamlet” allusions to a portrait of loneliness, companionship and power dynamics in the Internet age. The needy, loyal, flawed relationships between Elsie, Ophelia and Horatio, and the emotion conveyed obliquely in the fan fiction, can be quite moving.

The roomy layout of Behm’s production seems right for a play that’s more or less set in cyberspace. Donning and doffing costumes pieces — a Harry Potter-style scarf, a stovepipe hat — the role-juggling Amin and Connor cavort in empty space at the front of the stage, in front of the separated, contrasting hangouts of Elsie (messy) and Horatio (tidy and cozy). Farther back, occasional projections — a Hogwarts-style skyline, a 19th-century Illinois store — help to establish locales. (Danielle Preston designed the costumes; Lauren Chilton, the props; and Veronica J. Lancaster, the projections and sound.)

The projections also evoke certain critical moments of online time. To post or not to post? For Elsie, that’s rarely even a question.

To Tell My Story: A Hamlet Fanfic by Alexandra Petri. Directed by Megan Behm; scenic coordinator, Lauren Chilton; technical director, Dean Leong; lighting, E-Hui Woo. 100 minutes. Tickets: $15-$30. Through July 30 at the Silver Spring Black Box Theatre, 8641 Colesville Rd., Silver Spring.