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

Trying to add corner radius on addStream() camera #77

Open
Iulian33 opened this issue Jul 7, 2021 · 6 comments
Open

Trying to add corner radius on addStream() camera #77

Iulian33 opened this issue Jul 7, 2021 · 6 comments

Comments

@Iulian33
Copy link

Iulian33 commented Jul 7, 2021

I am looking or a way to add corner radius for my added media stream webCamera from corner related in image below

Screenshot 2021-07-07 at 09 54 49

@hthetiot
Copy link
Contributor

hthetiot commented Jul 7, 2021

@Iulian33 Did you try to Google: "canvas image rounded corners"

@Iulian33
Copy link
Author

Iulian33 commented Aug 5, 2021

@hthetiot we have access to method getCanvasContext() to get merged canvas already, but we do not have an option to get canvas for added stream in my case the webCamera from corner or to send on option to add corner radius to that added stream camera from corner.

@no-1ne
Copy link

no-1ne commented Nov 12, 2021

There seem to be option to override draw method for each stream passed, that may work?

https://github.com/t-mullen/video-stream-merger#custom-draw-function

@ChristianMatthias
Copy link

I made it work by adding the following code to the _drawVideo() function inside the Library.
The borderRadius is a custom added attribute to the stream object which makes the corners round by the value in px.

this._ctx?.save(); this._ctx?.beginPath(); this._ctx?.moveTo(positionX + stream.borderRadius, positionY); this._ctx?.lineTo(positionX + width - stream.borderRadius, positionY); this._ctx?.quadraticCurveTo(positionX + width, positionY, positionX + width, positionY + stream.borderRadius); this._ctx?.lineTo(positionX + width, positionY + height - stream.borderRadius); this._ctx?.quadraticCurveTo(positionX + width, positionY + height, positionX + width - stream.borderRadius, positionY + height); this._ctx?.lineTo(positionX + stream.borderRadius, positionY + height); this._ctx?.quadraticCurveTo(positionX, positionY + height, positionX, positionY + height - stream.borderRadius); this._ctx?.lineTo(positionX, positionY + stream.borderRadius); this._ctx?.quadraticCurveTo(positionX, positionY, positionX + stream.borderRadius, positionY); this._ctx?.closePath(); this._ctx?.clip(); this._ctx?.drawImage(element, positionX, positionY, width, height); this._ctx?.restore();

@mustafa-toptal
Copy link

mustafa-toptal commented Apr 16, 2022

This is how I rounded the corners of my stream

function roundedImage(ctx, x, y, width, height, radius) {
    ctx.beginPath();
    ctx.moveTo(x + radius, y);
    ctx.lineTo(x + width - radius, y);
    ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
    ctx.lineTo(x + width, y + height - radius);
    ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
    ctx.lineTo(x + radius, y + height);
    ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
    ctx.lineTo(x, y + radius);
    ctx.quadraticCurveTo(x, y, x + radius, y);
    ctx.closePath();
  }

merger.addStream(cameraStream, {
        draw: (ctx, frame, done) => {
          const x = 20;
          const y = merger.height - 180;
          const width = 100;
          const height = 150;
          ctx.save();
          roundedImage(ctx, x, y, width, height, 10);
          ctx.strokeStyle = "#fff";
          ctx.stroke();
          ctx.clip();
          ctx.drawImage(frame, x, y, width, height);
          ctx.restore();
          done();
        },
        x: 20,
        y: merger.height - 180,
        width: 100,
        height: 150,
        mute: false,
      });

Screenshot from 2022-04-16 16-58-33

@hthetiot
Copy link
Contributor

@Iulian33 you have access to individual draw stream, by providing draw option on addStream like @mustafa-toptal solution here #77 (comment)

I think we can add his solution in the documentation as an example to solve this issue and provide an example usage of custom draw.

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

No branches or pull requests

5 participants