Skip to content

Repeating an element X times with a specific offset #728

Answered by aarthificial
pieterdd asked this question in Q&A
Discussion options

You must be logged in to vote

There is no Repeater node at the moment (although it would be nice to have)
Right now the preferred way of achieving this would be using range().
The example below will generate a 7x5 grid of rectangles, for example:

import {makeScene2D, Rect} from '@motion-canvas/2d';
import {waitFor, range} from '@motion-canvas/core';

export default makeScene2D(function* (view) {
  view.add(
    range(7).flatMap(x =>
      range(5).map(y => (
        <Rect
          x={(x - 3) * 240}
          y={(y - 2) * 240}
          size={180}
          fill={'white'}
        />
      )),
    ),
  );

  yield* waitFor(1);
});

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@pieterdd
Comment options

Answer selected by pieterdd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants