Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bundling for browser canvas #443

Closed
imohanvadivel opened this issue Feb 28, 2023 · 8 comments
Closed

Bundling for browser canvas #443

imohanvadivel opened this issue Feb 28, 2023 · 8 comments
Labels
c-duplicate This issue or pull request already exists

Comments

@imohanvadivel
Copy link

Since motion canvas primarily uses canvas, I think we could leverage it to render animation on a webpage directly in a canvas element. But I'm not sure, how to bundle / compile the source for browser.

Could anyone help me with this.

@aarthificial
Copy link
Contributor

aarthificial commented Mar 1, 2023

This is still undocumented but you can already embed Motion Canvas animations on the web.
We do that on our documentation website, for example.

You can run the following command to build your project:

npm run build

This will produce a compiled project file in the dist/ directory.
On your website, you can then install our player package:

npm i @motion-canvas/player

It contains a Custom Element that can display the animation:

import '@motion-canvas/player'; // import the player somewhere on the website.
<motion-canvas-player
  src={`/project.js`} <!-- link to the statically hosted project file -->
/>

@StefanKreidel
Copy link

StefanKreidel commented Mar 21, 2023

@aarthificial I am assuming that the import of the player is intended for projects which are built before being shipped (e.g. vue or react)?

I wanted to integrate the player in an out-of-the-box jekyll project, ready to be hosted on GitHub. I installed the player as you described, then built it with webpack:

webpack build '@motion-canvas/player/dist/main.js' -o js/motion-canvas-player

and then included it in the jekyll page (post jekyll template shown):

<script src="/my-project/js/motion-canvas-player/main.js" type="text/javascript"></script>

What is the problem?
When including my already built animation with (post jekyll template again):

<motion-canvas-player src="/my-project/animation/test.js" auto="true"></motion-canvas-player>

I get an error

main.js:1 Uncaught (in promise) TypeError: r?.serialize is not a function
    at x.configure (main.js:1:10477)
    at rt.updateSettings (main.js:1:43326)
    at rt.updateSource (main.js:1:42519)

What am I even asking?

Am I missing something fundamental? Can I not build the player with webpack? If so, is there another way?

Any help would be much appreciated.

(motion-canvas and player version is 3.3.4)

@aarthificial
Copy link
Contributor

@StefanKreidel Can you make sure that your project was actually built using v3.3.4?
This issue should already be fixed by #555 which is out in that version.

@StefanKreidel
Copy link

Thanks for the fast feedback!

I just created a new project from scratch using v3.3.4. Turns out, using the quick-start example from the docs actually works. Just my somewhat large animation does not. Weirdly enough, it works perfectly fine when running npm run serve...

I'll investigate and try to figure out what is going on.
My source code is also on GitHub. It is however very basic. Just stumbled across motion-canvas 2 days ago.

@aarthificial
Copy link
Contributor

Theoretically, the contents of the scene shouldn't really matter.
This error is thrown here:

this.background =
typeof background === 'string'
? background
: background?.serialize() ?? null;

Because background is an instance of a wrong class that doesn't have the serialize method.
This is the project background configured in the Video Settings

@StefanKreidel
Copy link

First of all: thank you Jacob for not only responding so fast but for also investigation my issue! I really enjoy seeing someone being so dedicated to their project. Hugh compliment from me!

Now, I tried a few different things, including setting the background via view.fill(...) instead of the project settings. But nothing really worked. Sometimes the error disappeared but so did the entire player from my site.

The workaround described in #577 is the only thing currently working for me:

// import color specifically in your scene.tsx
import "@motion-canvas/core/lib/types/Color"
...

@aarthificial
Copy link
Contributor

Closing this in favor of #247

@aarthificial aarthificial closed this as not planned Won't fix, can't repro, duplicate, stale May 11, 2023
@aarthificial aarthificial removed their assignment May 11, 2023
@aarthificial aarthificial added the c-duplicate This issue or pull request already exists label May 11, 2023
@K0IN
Copy link

K0IN commented Dec 15, 2023

It would be sick if we can bind the canvas progession to a external source, i would really like to bind it to the scroll postion to use this as a type of scrollytelling tool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c-duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

4 participants