canvas-hypertxt
canvas-hypertxt copied to clipboard
Weird cache behavior
What happend:
After a few runs split()
function stops working.
With this little snippet written in TS we can reproduce the problem:
const testValue1 = `LAJDLKAJDLKAJSLDJALSJDLKASJDLKASJDLKAJSLDJALSKJDKLAJDLKAJDLKJASLDJLAKSJDLKASJDLKAJSLDJALSDJLAKSD
ALSKDJALSJDKLSJALKDJASLKDJLKAJDLKAJSDLKJASKDJALSKJDLASJDLKASJLDJASLDJALSKJD`;
export const debug = () => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
if (!ctx) {
return;
}
ctx.font = `16px Manrope`;
const results = new Array(50).fill(null).map((_, index) => {
return split(ctx, testValue1 + ``.padStart(index, '1'), `16px Manrope`, 240, false);
});
results.forEach((it) => console.log(it.length));
};
Expected behavior: split()
function still splits text by given width. (At least 8 rows, since we're adding little extra to the given string each iteration)
Current behavior: on second run function starts to return text splitted only by "\n" characters
Promlem solves if we run clearCache()
before each iteration.
Environment:
MacOS 14.1.1, Chrome 124.0.6367.208