Canvas rotated text artifacts

ぃ、小莉子 提交于 2019-12-01 18:16:29

I can't give a specific solution for processing but if you are ok with a generic answer you can do the following and then possibly integrate that with processing:

Chrome's text ability is currently poor (I believe a left-over from WebKit) not just with canvas but in general. This becomes especially visible when rotation is applied. The (backward) "ø" you see is in fact the e but due to the poor text engine and rounding errors the details melts together and it ends up looking like a Nordic char in this case.

A work-around is to draw the text you want to rotate to an off-screen canvas with no rotation applied, then use that canvas as an image that you draw to your main canvas to which rotation is applied.

This will produce a much better result and also perform better as normal image interpolation is used instead of the text engine.

An example can be:

/// draw text to off-screen canvas
osCtx.font = '12px arial';
osCtx.textBaseline = 'top';
osCtx.fillText(txt, 0, 0);

/// draw off-screen canvas rotated to main canvas
ctx.translate(w * 0.5, h * 0.5);
ctx.rotate(0.5);
ctx.drawImage(osCanvas, 0, 0);

Online demo here

Which will result in the same text looking like this instead:

versus drawn directly as text:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!