【发布时间】:2017-05-01 00:01:06
【问题描述】:
注意画布底部的空白行。它的 不可能在那条线上画出来。
如何使用整个画布进行绘图(全分辨率)?
浏览器:54.0.2840.99(官方版本)m(32 位)/Windows 7
在整页中运行:
var el = document.getElementsByTagName("canvas")[0];
var ctx = el.getContext("2d");
el.width = el.clientWidth;
el.height = el.clientHeight;
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(el.width, el.height);
ctx.lineTo(0, el.height);
ctx.fill();
ctx.closePath();
html {
height: 100%;
}
body {
display: flex;
flex-direction: column;
height: 100%;
width: 667.19px; /* = `80vh` in viewport of 834 px height */
}
.bar {
flex: auto;
background: black;
height: 20px;
}
#display {
position: relative;
width: 100%;
height: 0;
padding-bottom: 100%;
}
canvas {
position: absolute;
width: 100%;
height: 100%;
}
<div class="bar"></div>
<div id="display">
<canvas></canvas>
</div>
<div class="bar"></div>
明确一点:上面的 JavaScript 代码调整了画布的像素尺寸,使其与画布元素的像素尺寸相匹配。这个问题是关于画布上无法绘制的区域。
【问题讨论】:
-
如果我尝试使用Stack Snippets 对上述代码进行演示,以便于调试,您会非常介意吗?
-
@Daedalus 一点也不。随意这样做!
-
@Κ3Ν 我在问题的最后加了一段,给那些懒得看源码的人。解释是这个问题不是问题Canvas width and height in HTML5的重复。
-
尝试在sn-p中重现问题后,找到了原因;您的浏览器已缩小到 90%。
-
@Daedalus Zoom 达到 100%。
标签: css google-chrome html5-canvas flexbox