I'm trying to align text in a way that canvas context textBaseline property set to "alphabetic" does. I can't get exactly same effect with kineticjs.
var letters = [
{ symbol: "A", x: 3.0, size: 20 },
{ symbol: "B", x: 36.3, size: 30 },
{ symbol: "C", x: 86.3, size: 40 },
{ symbol: "D", x: 158.6, size: 50 },
{ symbol: "E", x: 248.9, size: 40 },
{ symbol: "F", x: 315.5, size: 30 },
{ symbol: "G", x: 361.3, size: 20 } ];
// How kineticjs renders the text
(function actual() {
var stage = new Kinetic.Stage({ container: "mycontainer", width: 400, height: 100 }),
layer = new Kinetic.Layer(),
baseline = 60;
letters.forEach(function(letter) {
layer.add(new Kinetic.Text({
x: letter.x,
y: baseline - letter.size,
text: letter.symbol,
fontSize: letter.size,
fill: 'black',
}));
});
// Baseline visualization
layer.add(new Kinetic.Line({
points: [0, baseline, 400, baseline ],
stroke: "red"
}));
stage.add(layer);
})();
// How I would like it to render the text
(function expected() {
var c = document.getElementById("mycanvas"),
ctx = c.getContext("2d"),
baseline = 60;
ctx.textBaseline = "alphabetic"; // redundant as it's actually default behaviour
letters.forEach(function(letter) {
ctx.font = letter.size + "px Arial";
ctx.fillText(letter.symbol, letter.x, baseline);
});
// Baseline visualization
ctx.strokeStyle = "red";
ctx.moveTo(0, baseline);
ctx.lineTo(400, baseline);
ctx.stroke();
})();
<script src="https://cdn.lukej.me/kineticjs/5.1.0/kinetic.min.js"></script>
<div id="mycontainer"></div>
<canvas id="mycanvas" width="400" height="150" style="position: absolute; left: 10px; top: 100px">
Same code on jsfiddle.
I'm aware of this question however I haven't found the right way of calculating the offset for alphabetic baseline.