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

Layer masking (AKA Clip) a video using canvas? #221

Open
Ivanca opened this issue Jan 20, 2023 · 4 comments
Open

Layer masking (AKA Clip) a video using canvas? #221

Ivanca opened this issue Jan 20, 2023 · 4 comments

Comments

@Ivanca
Copy link
Contributor

Ivanca commented Jan 20, 2023

I want to create a circle using canvas and clip a video so only it uses the canvas element as alpha channel, how can I do that with editly?

The equivalent in Photoshop (and similarly on other software) looks a like this:
image

Thank you very much for this library.

@mifi
Copy link
Owner

mifi commented Jan 20, 2023

Hi. I think what youre asking for is «masking». Not sure if canvas can do that

@Ivanca
Copy link
Contributor Author

Ivanca commented Jan 20, 2023

Yes it can, see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/clip
But how can I do it with this library using a video?

@Ivanca
Copy link
Contributor Author

Ivanca commented Jan 25, 2023

I created a PR #222 that adds a new (optional) callback so users can use it to modify the generated image (Fabric.Image) before being rendered; so stuff like masking like this ticket is about can be done using the fabric API with this bit of code:

await editly({
  outPath: './output.mp4', 
  clips: [{
    duration: 4, layers: [
      { type: 'video', path: './assets/lofoten.mp4', cutFrom: 0, cutTo: 4 },
      { 
        type: 'video', 
        path: './assets/hiking.mp4',
        cutFrom: 0, cutTo: 4, 
        resizeMode: 'cover',
        originX: 'center', originY: 'center',
        left: 0.5, top: 0.5, width: 0.5, height: 0.5,
        fabricImagePostProcessing: async ({image, progress, canvas})=> {
          const circleArgs = {
            radius:  Math.min(image.width, image.height) * 0.4,
            originX: 'center',
            originY: 'center',
            stroke: 'white',
            strokeWidth: 22,
          };
          image.setOptions({
            clipPath: new fabric.Circle(circleArgs)
          });
          canvas.add(new fabric.Circle({
            ...circleArgs,
            left: image.getCenterPoint().x,
            top: image.getCenterPoint().y
          }));   
        }
      }
    ]}
  ]
});

Example result:

alpha.mp4
@Ivanca
Copy link
Contributor Author

Ivanca commented Feb 4, 2023

@mifi Any intention to merge this or a similar API in the near future? Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants