t-mullen / video-stream-merger

Merge multiple video MediaStreams into one composite.
https://t-mullen.github.io/video-stream-merger/
MIT License
356 stars 81 forks source link

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

Open Iulian33 opened 3 years ago

Iulian33 commented 3 years ago

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 commented 3 years ago

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

Iulian33 commented 3 years ago

@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 commented 3 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

@Iulian33 you have access to individual draw stream, by providing draw option on addStream like @mustafa-toptal solution here https://github.com/t-mullen/video-stream-merger/issues/77#issuecomment-1100644598

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.