3.3.1_水平与垂直定位
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>水平与垂直定位</title>
<style>
body{
background: #eee;
}
#canvas{
background: #fff;
cursor: pointer;
margin-left: 10px;
margin-top: 10px;
box-shadow: 4px 4px 8px rgba(0,0,0,0.5);
-webkit-box-shadow: 4px 4px 8px rgba(0,0,0,0.5);
-moz-box-shadow: 4px 4px 8px rgba(0,0,0,0.5);
}
</style>
</head>
<body>
<canvas id="canvas" width="1000" height="600"></canvas>
</body>
<script>
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var fontHeight = 24;
var alignValues = ['start','center','end'];
var baselineValues = ['top','middle','bottom','alphabetic','ideographic','hanging'];
var x;
var y;
context.font = 'oblique normal bold 24px palatino';
for(var align = 0;align<alignValues.length;align++){
for(var baseline = 0; baseline<baselineValues.length;baseline++){
x = 20+align*fontHeight*15;
y = 20 +baseline*fontHeight*3;
drawText(alignValues[align]+'/'+baselineValues[baseline],alignValues[align],baselineValues[baseline])
drawTextMarker();
drawTextLine();
}
}
function drawText(text,textAlign,textBaseline){
if(textAlign){
context.textAlign = textAlign;
}
if(textBaseline){
context.textBaseline = textBaseline;
}
context.fillStyle = 'cornflowerblue';
context.fillText(text,x,y);
}
function drawTextMarker(){
context.save();
context.fillStyle = 'yellow';
context.fillRect(x,y,7,7);
context.strokeRect(x,y,7,7);
context.restore();
}
function drawTextLine(){
context.save();
context.strokeStyle = 'gray';
context.beginPath();
context.moveTo(0,y);
context.lineTo(canvas.width,y);
context.stroke();
context.restore();
}
</script>
</html>