Workshop › 15 Puzzle › Step 22 of 27

Smooth sliding

What we are doing

The sliding tile is not drawn on its square; it is drawn part of the way from its old square to its new one. t goes from 0 to 1 during the slide, and the position goes along with it.

```js
tiles.forEach((number, i) => {
  if (number === 0 || (slide && number === slide.tile)) return
  drawTile(number, squareX(i), squareY(i))
})
// The sliding tile is drawn part of the way from its old square to its new one.
if (slide) {
  const t = slide.frame / SLIDE_FRAMES
  const x = squareX(slide.from) + (squareX(slide.to) - squareX(slide.from)) * t
  const y = squareY(slide.from) + (squareY(slide.to) - squareY(slide.from)) * t
  drawTile(slide.tile, x, y)
}
```

What it means

Your turn

In draw, change the if inside forEach, and under the forEach block write the comment and the if (slide) block.

Skills: The game loop, Drawing on a canvas

All steps

  1. A board in one list
  2. The canvas and the pen
  3. Paint the table
  4. A function that draws
  5. Row and column
  6. Where each square is
  7. Draw one tile
  8. The number on the tile
  9. All the tiles
  10. Draw again and again
  11. The solved board from N
  12. Neighbours
  13. Slide a tile
  14. Click a tile
  15. Arrow keys
  16. Count the moves
  17. Shuffle with real slides
  18. Which boards can be solved?
  19. Is it solved?
  20. Remember the slide
  21. Count the frames
  22. Smooth sliding
  23. Solved!
  24. Show the win
  25. Play again
  26. A record to beat
  27. Build it yourself: a timer