Why rebuild a game mechanic on the web
Video games are full of interesting mechanics and satisfying interactions, and reimplementing a small piece of one on a web stack is a focused way to broaden your skills. You spend your time on an interesting problem instead of building an entire game, and even at that limited scope you tend to encounter technologies that push your knowledge further.
The card swipe from Among Us is a good example. The task itself is trivial, but enough players have failed at it that it has become a staple of streams and memes. The demo below repurposes the mechanic for the browser.
Dragging the card with pointer events
In the original, the card tracks your pointer horizontally while staying vertically aligned to the reader, its lateral travel is bounded on both sides of the reader, and it snaps back to its starting position when you release. Mouse and touch events cover all of this: a mouse-down/move/up trio for desktop, or touch-start/move/end on touchscreens. The skeleton looks like this:
const card = document.getElementById('card');
const reader = document.getElementById('reader');
let active = false;
let initialX;
// set event handlers
document.addEventListener('mousedown', dragStart);
document.addEventListener('mousemove', drag);
document.addEventListener('mouseup', dragEnd);
document.addEventListener('touchstart', dragStart);
document.addEventListener('touchmove', drag);
document.addEventListener('touchend', dragEnd);
function dragStart(e) {
// continue only if drag started on card
if (e.target !== card) return;
// get initial pointer position
if (e.type === 'touchstart') {
initialX = e.touches[0].clientX;
} else {
initialX = e.clientX;
}
active = true;
}
function drag(e) {
// continue only if drag started on card
if (!active) return;
e.preventDefault();
let x;
// get current pointer position
if (e.type === 'touchmove') {
x = e.touches[0].clientX - initialX;
} else {
x = e.clientX - initialX;
}
// update card position
setTranslate(x);
}
function dragEnd(e) {
// continue only if drag started on card
if (!active) return;
e.preventDefault();
let x;
// get final pointer position
if (e.type === 'touchend') {
x = e.touches[0].clientX - initialX;
} else {
x = e.clientX - initialX;
}
active = false;
// reset card position
setTranslate(0);
}
function setTranslate(x) {
// don't let card move too far left or right
if (x < 0) {
x = 0;
} else if (x > reader.offsetWidth) {
x = reader.offsetWidth;
}
// set card position on center instead of left edge
x -= (card.offsetWidth / 2);
card.style.transform = 'translateX(' + x + 'px)';
}
Deciding whether the swipe counts
A valid swipe must travel far enough across the reader and at the right speed; stopping short, racing, or dawdling all produce an invalid result. Whether the card went far enough is checked in dragEnd against the position of the reader's right edge:
let status;
// check if card wasn't swiped all the way
if (x < reader.offsetWidth) {
status = 'invalid';
}
setStatus(status);
Duration comes from performance.now() timestamps captured in dragStart and dragEnd:
function setStatus(status) {
// status is only set for incomplete swipes so far
if (typeof status === 'undefined') {
// timestamps taken at drag start and end using performance.now()
let duration = timeEnd - timeStart;
if (duration > 700) {
status = 'slow';
} else if (duration < 400) {
status = 'fast';
} else {
status = 'valid';
}
}
// set [data-status] attribute on reader
reader.dataset.status = status;
}
The outcome of each check is written to the reader's data-status attribute, and CSS handles the rest: the status message appears and a red or green light lights up.
#message:after {
content: "Please swipe card";
}
[data-status="invalid"] #message:after {
content: "Bad read. Try again.";
}
[data-status="slow"] #message:after {
content: "Too slow. Try again.";
}
[data-status="fast"] #message:after {
content: "Too fast. Try again.";
}
[data-status="valid"] #message:after {
content: "Accepted. Thank you.";
}
.red {
background-color: #f52818;
filter: saturate(0.6) brightness(0.7);
}
.green {
background-color: #3dd022;
filter: saturate(0.6) brightness(0.7);
}
[data-status="invalid"] .red,
[data-status="slow"] .red,
[data-status="fast"] .red,
[data-status="valid"] .green {
filter: none;
}
Font, animation and sound
Three additions push the demo closer to the original. First, the free DSEG font gives the segmented look of old LCDs, which only requires hosting the files and an @font-face declaration in CSS:
@font-face {
font-family: 'DSEG14Classic';
src: url('../fonts/DSEG14-Classic/DSEG14Classic-Regular.woff2') format('woff2'),
url('../fonts/DSEG14-Classic/DSEG14Classic-Regular.woff') format('woff'),
url('../fonts/DSEG14-Classic/DSEG14Classic-Regular.ttf') format('truetype');
}
Second, the text jitters like it does in the game. Game developers routinely add subtle motion to bring an element to life, and this is done with a CSS animation:
@keyframes jitter {
from {
transform: translateX(0);
}
to {
transform: translateX(5px);
}
}
Out of the box that animation glides smoothly back and forth; the goal is movement in five-pixel steps instead. The steps() function handles it:
#message {
animation: jitter 3s infinite steps(2);
}
Third, the same audio feedback as Among Us is played. Sound is often discouraged on the web, which makes a project like this a good excuse to use it freely.
let soundAccepted = new Audio('./audio/CardAccepted.mp3');
let soundDenied = new Audio('./audio/CardDenied.mp3');
if (status === 'valid') {
soundAccepted.play();
} else {
soundDenied.play();
}
Replicate a mechanic of your own
The web has become highly standardized in look and feel, which is precisely why lifting an element from a game works well as a stretch exercise. In this small demo, the author worked with web fonts and CSS animations, input events and audio in JavaScript, and an unconventional visual style. Survey the mechanics in your favorite games and try reproducing one; you may be surprised at what you pick up.



