Skip to content

Commit

Permalink
Use bg-color at the text centre instead of origin
Browse files Browse the repository at this point in the history
This likely gives a more accurate measure of the text's new background.
  • Loading branch information
shivaprsd committed Oct 14, 2022
1 parent 56a349e commit 82f0db2
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions addon/doq.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ const DOQReader = {
);
if (isShape && !this.flags.shapesOn || !isColor)
return style;
const bg = isText && this.getCanvasColor(ctx, args[1], args[2]);
const bg = isText && this.getCanvasColor(ctx, ...args);
const key = style + (bg ? bg.toHex() : "");
let newStyle = this.styleCache.get(key);
if (!newStyle) {
Expand All @@ -163,11 +163,14 @@ const DOQReader = {
}
return newStyle;
},
getCanvasColor(ctx, tx, ty) {
getCanvasColor(ctx, text, tx, ty) {
if (!this.canvasData.has(ctx))
return null;
const mtr = ctx.measureText(text);
const dx = mtr.width / 2;
const dy = (mtr.actualBoundingBoxAscent - mtr.actualBoundingBoxDescent) / 2;
const tfm = ctx.getTransform();
let {x, y} = tfm.transformPoint({x: tx, y: ty});
let {x, y} = tfm.transformPoint({x: tx + dx, y: ty - dy});
[x, y] = [x, y].map(Math.round);
const canvasData = this.canvasData.get(ctx);
const i = (y * canvasData.width + x) * 4;
Expand Down

0 comments on commit 82f0db2

Please sign in to comment.