Centering a canvas

后端 未结 11 1530
野的像风
野的像风 2021-01-30 10:00

How do I markup a page with an HTML5 canvas such that the canvas

  1. Takes up 80% of the width

  2. Has a corresponding pixel h

11条回答
  •  我在风中等你
    2021-01-30 10:58

    in order to center the canvas within the window +"px" should be added to el.style.top and el.style.left.

    el.style.top = (viewportHeight - canvasHeight) / 2 +"px";
    el.style.left = (viewportWidth - canvasWidth) / 2 +"px";
    

提交回复
热议问题