wordcloud2.js icon indicating copy to clipboard operation
wordcloud2.js copied to clipboard

Crashes on large word sizes

Open lafncow opened this issue 11 years ago • 12 comments

If the size for a word is too large, the script will crash on line 509 of the getTextInfo method, ie:

// Get the pixels of the text
var imageData = fctx.getImageData(0, 0, width, height).data;

...giving this error:

Cannot read property 'data' of null

It can be duplicated by running this on a 800px x 1200px canvas:

WordCloud( myCanvas, { list : [["my",173],["spoon",283],["is",173],["too",283],["big",6283]] });

I may be mistaken, but my expected behavior was that the entire cloud would scale down so that the extra large word(s) fit.

This happens in Chrome, I have not tested other browsers.

lafncow avatar Jul 09 '13 22:07 lafncow

Thanks for filing!

timdream avatar Jul 10 '13 01:07 timdream

Oh crap, I can actually crash the tab on Chrome with your code here ...

timdream avatar Jul 16 '13 11:07 timdream

... and Firefox hangs w/o any way to recover. I would have to kill the browser.

timdream avatar Jul 16 '13 11:07 timdream

I now concluded this is a bug of the browser, or even the canvas API, but not on the library side. The browsers died at getImageData() call presumably the large TypedArray data flood into the memory.

I'll file bugs on Firefox and Chrome.

There is no way in JavaScript to know the size limit of the array, or canvas. The way to fix this here would be expose a config allowing people to set a size limit, but again, deciding that limit will be a problem.

timdream avatar Jul 24 '13 05:07 timdream

Sorry for the delay, Firefox bug filed https://bugzilla.mozilla.org/show_bug.cgi?id=952909

timdream avatar Dec 23 '13 08:12 timdream

+1 what's the safe way to work around this? I get the same error even with small values, if they come after an even smaller value. e.g., [..., ['Foo', 10], ['Bar', 11]]

speg avatar May 26 '14 19:05 speg

Maybe your canvas is too big so the resulting pixel size of word to draw need to be big? Maybe your weightFactor is set too big?

I don't have a way to workaround this, as I previously stated.

timdream avatar May 27 '14 07:05 timdream

I personally have not run into this issue with count values as small as the ones in speg's comment, but have run into it with much larger values. The way I safely got around this was to pick a max count value that would not cause the browser to crash and normalize all count values based on the max value.

As tim suggested, I would definitely make sure your weight factor is not too big as well.

JacquiManzi avatar May 27 '14 19:05 JacquiManzi

Is this still an issue? My wordcloud crashes servers (i deployed to a meteor server and it crashed that server). I thought it crashed when the result set was too large, not necessarily because of word size... i have a formula set so the size of the word doesn't get too large. And i only implented that because when the words were too large they would write over the whole screen (behind other dom elements, kinda looked like a nice background feature)

but nevertheless... i'm wondering if large word sets are causing this to crash?

Kiel-H-Byrne avatar Feb 25 '16 21:02 Kiel-H-Byrne

I don't know how meteor renders canvases on server (maybe w/ a headless WebKit/Chromium?), but I would imagine this issue is still valid.

timdream avatar Feb 26 '16 05:02 timdream

An easy way to fix this issue is to 'normalize' the array of word's weights. So that the range of weight can only be 10-20, 10-50, etc.. So your not plugging in raw weights, that may cause the browser to crash or have that particular line return 'null'.

Would be nice if the library normalized the weights for you. ;)

ajm113 avatar Mar 18 '16 22:03 ajm113

@ajm113 you're a genious! It worked for me :-)

jctelo avatar Apr 14 '16 12:04 jctelo