为什么此代码中的画布大小不符合预期?

时间:2016-10-05 12:24:22

标签: html css canvas

https://jsfiddle.net/awguo/uw1x8m3y/

这是详细的演示。

我将一个img放在div#wrapper中,然后在该包装器内的img上放置一个画布重叠。我将img和css的宽度和高度都设置为100%。然后我跟踪画布的宽度*高度。它不是预期的(350x350)而是350x354。为什么还有“4px”?我应该怎么做才能使画布大小与图像完全相同(也可以根据父div的宽度进行拉伸?

谢谢!

?p=2
$(function() {
  // Why it's 350x354, not 350x350?
  var output = 'canvas size: ' + ($('#overlap').css('width') + ' x ' + $('#overlap').css('height'));
  $('#output').val(output);
});
#wrapper {
  position: relative;
  width: 350px;
}
#wrapper img {
  width: 100%;
}
#overlap {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%
}
#output {
  width: 350px
}

1 个答案:

答案 0 :(得分:3)

制作图片显示inline-blockblock。默认情况下,inlinevertical-alignbaseline0.5em,因此会增加额外的$(function() { // Why it's 350x354, not 350x350? var output = 'canvas size: ' + ($('#overlap').css('width') + ' x ' + $('#overlap').css('height')); $('#output').val(output); });高度。



#wrapper {
  position: relative;
  width: 350px;
}
#wrapper img {
  width: 100%;
  display: block;
}
#overlap {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  display: block;
}
#output {
  width: 350px
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrapper">
  <img src="http://www.awflasher.com/300x300_demo.png" />
  <canvas id="overlap"></canvas>
</div>

<h3>
  Output:
</h3>
<input id="output" type="text" />

<p>
  Why the output is 350x354, not 350x350 as expected?
</p>
&#13;
@FindBy(how = How.CSS, using = "[type='radio']")
private List<WebElement> radioButtons;
&#13;
&#13;
&#13;

enter image description here