Workshop › Tetris-style Blocks › Step 31 of 32
A faint ghost at the bottom shows where the piece would land. ghostY finds that row without moving the piece, and
drawShape gets an optional color to draw it see-through.
```js
function ghostY() {
let y = piece.y
while (fits(piece.shape, piece.x, y + 1)) y++
return y
}
function drawShape(shape, x, y, color) {
if (value) drawCell(x + c, y + r, color || COLORS[value])
drawShape(piece.shape, piece.x, ghostY(), 'rgba(255, 255, 255, 0.15)')
```ghostY counts down its own copy y, never piece.y, and returns the lowest row where the piece fits.drawShape takes a fourth parameter color; color || COLORS[value] uses it if given, otherwise the block's own
color.ghostY under hardDrop.color to drawShape's parameters and use color || COLORS[value].draw, inside if (state === 'playing'), draw the ghost above the real piece's line.Skills: Drawing on a canvas, Loops
← Drop it Build it yourself: hold a piece →