var cnv = document.getElementById('canvas');
var ctx = cnv.getContext('2d');
ctx.width = ctx.width; // Clear the context
function demoBaseline(x, y, value) {
ctx.textBaseline = value;
ctx.fillText(value, x, y);
}
function demoAlign(x, y, value) {
ctx.textAlign = value;
ctx.fillText(value, x, y);
}
// Some example font styles.
ctx.font = "30pt helvetica";
ctx.fillText("Helvetica 30pt", 20, 40);
ctx.font = "20pt times bold";
ctx.strokeText("Times 20pt bold", 20, 70);
ctx.font = "12pt courier bold";
ctx.fillText("12pt courier bold", 20, 90);
// Demonstrate text baseline
var y = 130;
ctx.save();
// Draw a couple of baselines.
ctx.strokeStyle = "#00AAFF";
ctx.beginPath();
ctx.moveTo(20, y);
ctx.lineTo(300, y);
ctx.moveTo(20, y + 40);
ctx.lineTo(300, y + 40);
ctx.stroke();
// Render the basline types.
ctx.font = "15pt helvetica";
demoBaseline(20, y, 'top');
demoBaseline(120, y, 'hanging');
demoBaseline(240, y, 'middle');
demoBaseline(20, y + 40, 'alphabetic');
demoBaseline(120, y + 40, 'ideographic');
demoBaseline(240, y + 40, 'bottom');
ctx.restore();
// Demonstrate the alignments.
y = 210;
ctx.save();
// Draw a line marking the text start.
ctx.strokeStyle = "#00AAFF";
ctx.beginPath();
ctx.moveTo(160, y - 10);
ctx.lineTo(160, y + 120);
ctx.stroke();
// Render the alignment types
ctx.font = "15pt helvetica";
demoAlign(160, y + 20, 'start', 140);
demoAlign(160, y + 40, 'end', 140);
demoAlign(160, y + 60, 'left', 140);
demoAlign(160, y + 80, 'right', 140);
demoAlign(160, y + 100, 'center', 140);
ctx.restore();