Workshop › Memory › Step 24 of 25
To fake a 3D turn in 2D we squeeze the card horizontally. As flip goes from 0 to 1, Math.abs(Math.cos(flip * π))
goes 1 → 0 → 1: full width, a thin line halfway (exactly when the sides swap), full width again.
```js
// Squeeze the card horizontally to fake a 3D turn: full width, a thin line halfway, full width again.
const width = CARD * Math.abs(Math.cos(card.flip * Math.PI))
const x = cardX(card) + (CARD - width) / 2
const y = cardY(card)
if (card.flip >= 0.5) {
ctx.fillStyle = card.matched ? '#bbf7d0' : '#f8fafc'
ctx.fillRect(x, y, width, CARD)
ctx.fillText(card.symbol, cardX(card) + CARD / 2, y + CARD / 2)
} else {
ctx.fillStyle = '#6366f1'
ctx.fillRect(x, y, width, CARD)
}
```Math.PI is π. Math.cos(flip * Math.PI) goes from 1 to -1 as flip goes 0 to 1; Math.abs drops the sign, so
the width goes 85 → 0 → 85.x moves right by half of what was squeezed off, so the card stays centered.cardX(card) + CARD / 2.In draw's card loop, add the comment and width line, change x, and use width in both fillRects and
cardX(card) in fillText.
Skills: The game loop, Drawing on a canvas
← Swap sides halfway Build it yourself: your best game →