HTML5 Canvas 100% height and width

前端 未结 2 1842
囚心锁ツ
囚心锁ツ 2021-02-08 11:20

I\'m trying to make this raindrop canvas script take up 100% width and height, but nothing I seem to do works. I tried changing the CSS, and height/width in the Canvas area, but

2条回答
  •  萌比男神i
    2021-02-08 11:57

    body, #canvasRain {width:100%; height:100%; margin:0px;} will set your size properly but your problem is that your canvas height/width you're using to do your drawing doesn't pick up the proper px values when setting them with %'s. And that's where the scaling comes in with the fuzzy flakes. It takes some default canvas size and stretches to 100% of the view. Adding something like

    bufferCanvas.width = canvas.width = window.innerWidth;
    bufferCanvas.height = canvas.height = window.innerHeight;
    

    seems to do the trick. And you might want/need to handle resize events to recalculate it. Here is a sample. I couldn't get jsFiddle to work for me, so it's just the whole thing.

提交回复
热议问题