Commit 825cd36e authored by David Schnur's avatar David Schnur

Pre-compute coordinates for each line of text.

The values don't change, so there's no reason to repeat those
calculations on every redraw.  The resulting code is not just faster,
but also smaller and simpler, and we no longer need to store halign in
the text info object.
parent f99a225f
...@@ -98,11 +98,6 @@ browser, but needs to redraw with canvas text when exporting as an image. ...@@ -98,11 +98,6 @@ browser, but needs to redraw with canvas text when exporting as an image.
continue; continue;
} }
var x = info.x,
y = info.y,
lines = info.lines,
halign = info.halign;
// Since every element at this level of the cache have the // Since every element at this level of the cache have the
// same font and fill styles, we can just change them once // same font and fill styles, we can just change them once
// using the values from the first element. // using the values from the first element.
...@@ -113,34 +108,10 @@ browser, but needs to redraw with canvas text when exporting as an image. ...@@ -113,34 +108,10 @@ browser, but needs to redraw with canvas text when exporting as an image.
updateStyles = false; updateStyles = false;
} }
var lines = info.lines;
for (var i = 0; i < lines.length; ++i) { for (var i = 0; i < lines.length; ++i) {
var line = lines[i];
var line = lines[i], context.fillText(line.text, line.x, line.y);
linex = x;
// Apply horizontal alignment per-line
if (halign == "center") {
linex -= line.width / 2;
} else if (halign == "right") {
linex -= line.width;
}
// FIXME: LEGACY BROWSER FIX
// AFFECTS: Opera < 12.00
// Round the coordinates, since Opera otherwise
// switches to uglier (probably non-hinted) rendering.
// Also offset the y coordinate, since Opera is off
// pretty consistently compared to the other browsers.
if (!!(window.opera && window.opera.version().split(".")[0] < 12)) {
linex = Math.floor(linex);
y = Math.ceil(y - 2);
}
context.fillText(line.text, Math.round(linex), Math.round(y));
y += line.height;
} }
} }
} }
...@@ -247,8 +218,6 @@ browser, but needs to redraw with canvas text when exporting as an image. ...@@ -247,8 +218,6 @@ browser, but needs to redraw with canvas text when exporting as an image.
// zero so we can count them up line-by-line. // zero so we can count them up line-by-line.
info = styleCache[text] = { info = styleCache[text] = {
x: null,
y: null,
width: 0, width: 0,
height: 0, height: 0,
active: false, active: false,
...@@ -317,19 +286,41 @@ browser, but needs to redraw with canvas text when exporting as an image. ...@@ -317,19 +286,41 @@ browser, but needs to redraw with canvas text when exporting as an image.
info.active = true; info.active = true;
// Save horizontal alignment for later; we'll apply it per-line
info.x = x;
info.halign = halign;
// Tweak the initial y-position to match vertical alignment // Tweak the initial y-position to match vertical alignment
if (valign == "middle") { if (valign == "middle") {
info.y = y - info.height / 2; y = Math.round(y - info.height / 2);
} else if (valign == "bottom") { } else if (valign == "bottom") {
info.y = y - info.height; y = Math.round(y - info.height);
} else { } else {
info.y = y; y = Math.round(y);
}
// FIXME: LEGACY BROWSER FIX
// AFFECTS: Opera < 12.00
// Offset the y coordinate, since Opera is off pretty
// consistently compared to the other browsers.
if (!!(window.opera && window.opera.version().split(".")[0] < 12)) {
y -= 2;
}
// Fill in the x & y positions of each line, adjusting them
// individually for horizontal alignment.
var lines = info.lines;
for (var i = 0; i < lines.length; ++i) {
var line = lines[i];
line.y = y;
y += line.height;
if (halign == "center") {
line.x = Math.round(x - line.width / 2);
} else if (halign == "right") {
line.x = Math.round(x - line.width);
} else {
line.x = Math.round(x);
}
} }
}; };
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment