2016-10-05 5 views
2

https://jsfiddle.net/awguo/uw1x8m3y/このコードのキャンバスサイズが期待どおりでないのはなぜですか?

ここは詳細なデモです。

私はdiv#ラッパーにimgを入れ、そのラッパーの中にこのimgのキャンバスオーバーラップを入れます。 imgとcssの幅と高さを両方とも100%に設定しました。次に、キャンバスの幅*高さを追跡します。予想通り(350x350)ではなく350x354です。なぜ "4px"以上のものがあるのですか?私はまた、親のdivの幅に応じて延伸されてもよい画像とまったく同じキャンバスサイズを(作るにはどうすればよいですか?

ありがとう!

$(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 
 
}
<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>

答えて

3

メイク画像の表示inline-blockblock。デフォルトではinlineあるとbaselinevertical-align編を取得し、それは余分な狂気0.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>

enter image description here

+0

OMG、その答えは非常に高速です。ありがとう、私は試してみるよ! –

+0

'inline'を意味すると思います、' baseline'は 'display'のための有効な値ではありません – Kaiido

+0

@Kaiido私は' vertical-align'値を意味しました。混乱させて申し訳ありません。 –

関連する問題