在火狐下和chrome测试下发现 当设置文字居中显示的时候 如果在 chrome下 设置textBaseline=‘top’的情况下 再进行居中 文本位置会偏下 在火狐下确是正常的 为了使文本在chrome下居中显示 我们只能用textBaseline=‘bottom’ 再在Y轴居中的情况下 oGc.fillText(str,(oC.width-w)/2,(oC.height-h)/2+h); 解决方案如下
<script type="text/javascript">
var oC=document.getElementById('c1');
oC.width=400;
oC.height=400;
var oGc=oC.getContext('2d');
var str='你好';
var h=60;
oGc.font=h+'px x';
var w=oGc.measureText(str).width;
oGc.textBaseline='bottom';
oGc.fillText(str,(oC.width-w)/2,(oC.height-h)/2+h);
//填充文字居中显示 //在chrome下只有这样才是居中
// oGc.textBaseline='top';
// oGc.fillText(str,(oC.width-w)/2,(oC.height-60)/2+);//在火狐下正常 在chrome下 有问题
</script>