Canvas Has White Space At The Bottom And Scrolls Too Far
I'm using this answer https://stackoverflow.com/a/36233727/1350146 to scroll a canvas in a div. I'm also hiding the scrollbar. The problem is it appears to scroll too far, in this
Solution 1:
Make the canvas a block
element or use vertical-align:top
. By default, canvas is an inline element and it will behave similary to an img
; thus you will have the whitespace issue due to vertical alignement (Image inside div has extra space below the image)
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.fillStyle = '#00aa00'
ctx.fillRect(0, 0, c.width, c.height);
ctx.fillStyle = '#fff'
ctx.font='12pt A'
ctx.fillText("scroll here to see red from screen div", 30, 50);
.screen {
background: red;
height: 100px;
width: 300px;
overflow: auto;
border-radius: 20px;
}
canvas {
display:block;
}
::-webkit-scrollbar {
width: 0px;
height: 0px;
}
<divclass="screen"><canvasid="myCanvas"width="300"height="120"></canvas></div>
Solution 2:
Use a negative margin-bottom value for your canvas element. Anything between margin-bottom: -5px;
and margin-bottom: -105px;
will work for this example.
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.fillStyle = '#00aa00'
ctx.fillRect(0, 0, c.width, c.height);
ctx.fillStyle = '#fff'
ctx.font='12pt A'
ctx.fillText("scroll here to see red from screen div", 30, 50);
.screen {
background: red;
height: 100px;
width: 300px;
overflow: auto;
border-radius: 20px;
}
::-webkit-scrollbar {
width: 0px;
height: 0px;
}
canvas {
margin-bottom: -5px;
}
<divclass="screen"><canvasid="myCanvas"width="300"height="120"></canvas></div>
Post a Comment for "Canvas Has White Space At The Bottom And Scrolls Too Far"