From 98540f855f6cbee37ed70ef7f74f368aff7b1057 Mon Sep 17 00:00:00 2001 From: Ovilia Date: Thu, 24 Nov 2022 15:46:55 +0800 Subject: [PATCH] fix: shrinkToFit should not make others invisible --- example/test.html | 66 +++++++++++++++++++++++++++++++++++++++++++++++ src/layout.js | 27 ++++++++++++------- 2 files changed, 84 insertions(+), 9 deletions(-) create mode 100644 example/test.html diff --git a/example/test.html b/example/test.html new file mode 100644 index 0000000..6ffcc21 --- /dev/null +++ b/example/test.html @@ -0,0 +1,66 @@ + + + + + + + + + +
+
+

Expect text with 0 to 9 to be displayed all

+ + + diff --git a/src/layout.js b/src/layout.js index 99cd2a7..e4ee4cf 100644 --- a/src/layout.js +++ b/src/layout.js @@ -452,6 +452,10 @@ var WordCloud = function WordCloud(elements, options) { var x = Math.floor((eventX * (canvas.width / rect.width || 1)) / g); var y = Math.floor((eventY * (canvas.height / rect.height || 1)) / g); + if (!infoGrid[x]) { + return null + } + return infoGrid[x][y]; }; @@ -978,7 +982,11 @@ var WordCloud = function WordCloud(elements, options) { /* putWord() processes each item on the list, calculate it's size and determine it's position, and actually put it on the canvas. */ - var putWord = function putWord(item) { + var putWord = function putWord(item, loopIndex) { + if (loopIndex > 20) { + return null; + } + var word, weight, attributes; if (Array.isArray(item)) { word = item[0]; @@ -1078,16 +1086,17 @@ var WordCloud = function WordCloud(elements, options) { // // leave putWord() and return true // return true; // } + } - if (settings.shrinkToFit) { - if (Array.isArray(item)) { - item[1] = (item[1] * 3) / 4; - } else { - item.weight = (item.weight * 3) / 4; - } - return putWord(item); + if (settings.shrinkToFit) { + if (Array.isArray(item)) { + item[1] = (item[1] * 3) / 4; + } else { + item.weight = (item.weight * 3) / 4; } + return putWord(item, loopIndex + 1); } + // we tried all distances but text won't fit, return null return null; }; @@ -1300,7 +1309,7 @@ var WordCloud = function WordCloud(elements, options) { return; } escapeTime = new Date().getTime(); - var drawn = putWord(settings.list[i]); + var drawn = putWord(settings.list[i], 0); var canceled = !sendEvent('wordclouddrawn', true, { item: settings.list[i], drawn: drawn