hhenrichsen / canvas-commons

Hunter's Motion Canvas Utilities
MIT License
29 stars 2 forks source link
motion-canvas

Canvas Commons

npm GitHub Static Badge

Warning ⚠️ This library is still under construction. Breaking changes are possible until I release version 1.0.0. Update versions with caution and only after reading the commit log. ⚠️

If you use this in your videos, I would appreciate credit via a link to this repo, or a mention by name. I would also love to see them; feel free to show me on the motion canvas discord (I'm @Hunter on there).

If you want to support the development of this and other libraries, feel free to donate on Ko-fi.

Preview

Code for this GIF can be found here

Using this library

From git

  1. Clone this repo.
  2. Run npm install <path to this repo> in your motion canvas project

From npm

  1. Run npm install @hhenrichsen/canvas-commons

Components

Scrollable

The Scrollable node is a custom component designed to allow for scrolling within a container. Its size represents the viewports size, and it can be scrolled to any position within its content.

Props

Example

import {Scrollable} from '@hhenrichsen/canvas-commons';
import {makeScene2D, Rect} from '@motion-canvas/2d';
import {createRef, waitFor} from '@motion-canvas/core';

export default makeScene2D(function* (view) {
  const scrollable = createRef<Scrollable>();
  const rect = createRef<Rect>();
  view.add(
    <Scrollable ref={scrollable}>
      <Rect fill={'blue'} radius={5} ref={rect} size={40}></Rect>
    </Scrollable>,
  );

  yield* scrollable().scrollTo([150, 150], 2);
  yield* scrollable().scrollToLeft(1);
  yield* scrollable().scrollToTop(1);
  yield* scrollable().scrollTo(0, 1);
  yield* waitFor(1);

  yield rect().fill('seagreen', 1);
  yield* rect().size(600, 2);
  yield* waitFor(1);

  yield* scrollable().scrollToBottom(1);
  yield* scrollable().scrollToRight(1);
  yield* scrollable().scrollBy(-100, 1);
  yield* waitFor(5);
});

Window

The Window node is custom component designed to look like a window on either a MacOS system or a Windows 98 system.

Props

Example

import {Window, Scrollable, WindowStyle} from '@hhenrichsen/canvas-commons';
import {makeScene2D, Rect} from '@motion-canvas/2d';
import {createRef, waitFor} from '@motion-canvas/core';

export default makeScene2D(function* (view) {
  const window = createRef<Window>();
  const rect = createRef<Rect>();
  view.add(
    <>
      <Window windowStyle={WindowStyle.Windows98} ref={window}>
        <Rect fill={'blue'} radius={5} ref={rect} size={40}></Rect>
      </Window>
    </>,
  );

  yield* window.open(view, 1);
  yield* waitFor(1);
});

FileTree

The FileTree node is a custom component designed to look like a file tree. It supports highlighting and selection of files and folders.

Props

Example

import {FileTree, FileType} from '@hhenrichsen/canvas-commons';
import {makeScene2D} from '@motion-canvas/2d';
import {createRef, waitFor} from '@motion-canvas/core';

export default makeScene2D(function* (view) {
  const fileStructure = createRef<FileTree>();
  view.add(
    <>
      <FileTree
        rowSize={50}
        ref={fileStructure}
        structure={{
          name: '/',
          type: FileType.Folder,
          children: [
            {
              name: 'src',
              type: FileType.Folder,
              children: [
                {
                  name: 'data',
                  id: 'db',
                  type: FileType.Folder,
                  children: [
                    {
                      name: 'queries',
                      type: FileType.Folder,
                      children: [
                        {
                          name: 'userQueries.ts',
                          type: FileType.File,
                        },
                        {
                          name: 'postQueries.ts',
                          type: FileType.File,
                        },
                      ],
                    },
                    {
                      name: 'connection.ts',
                      type: FileType.File,
                    },
                  ],
                },
                {
                  name: 'model',
                  id: 'model',
                  type: FileType.Folder,
                  children: [
                    {
                      name: 'user.ts',
                      type: FileType.File,
                    },
                    {
                      name: 'post.ts',
                      type: FileType.File,
                    },
                  ],
                },
                {
                  name: 'view',
                  id: 'view',
                  type: FileType.Folder,
                  children: [
                    {
                      name: 'home.component.ts',
                      type: FileType.File,
                    },
                    {
                      name: 'profile.component.ts',
                      type: FileType.File,
                    },
                  ],
                },
              ],
            },
          ],
        }}
      ></FileTree>
    </>,
  );

  yield* fileStructure().emphasize('db', 1);
});

Functional Components

I also have a collection of functional components that I use to automate using some of these components: