HTML5: crazy canvas drawImage sizing

前端 未结 3 630
故里飘歌
故里飘歌 2021-01-03 10:19

I have the next html template:

3条回答
  •  隐瞒了意图╮
    2021-01-03 10:53

    Those magic coefficients correspond to the ratio between the video object size and the size of canvas.

    Suppose, your video size is (400 x 300) and you want to show it on the canvas with size (200 x 150). It can be done just by:

    context.drawImage(video,0,0,200,150);
    

    It will resize full video to fit the canvas.

    However, you are using clipping version of drawImage(). The first four coordinate arguments describe clipping parameters. For example in the following case it takes quarter of full video:

    context.drawImage(video,0,0,200,150,0,0,200,150);
    

    Edit according to updated question

    The image is clipped, since properties canvas.clientWidth and canvas.clientHeight are larger than canvas.width and canvas.height. That happens because of CSS display: flex;. To show full image use:

    context.drawImage(video, 0, 0, canvas.width, canvas.height);
    

提交回复
热议问题