Skip to content

Commit

Permalink
Fix use of shrinkToFit together with drawOutOfBounds false
Browse files Browse the repository at this point in the history
When having big words on a small grid, using shrinkToFit: true should,
together with drawOutOfBounds: false, ensure, that the words are all rendered
within the available space. This should be done by shrinking the words
as much as needed to fit the available space.

However, when drawOutOfBounds is set to false, the code before this commit
simply did not render the word at all if it did not fit the grid (anymore).

This commit fixes the behaviour so that when setting shrinkToFit to true and
drawOutOfBounds to false will correctly render the words on the available space.

Co-authored-by: Arne <[email protected]>
  • Loading branch information
FlorianSW and shaguarger committed Sep 11, 2020
1 parent 28e2169 commit 8316204
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/wordcloud2.js
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ if (!window.clearImmediate) {
// If drawOutOfBound is set to false,
// skip the loop if we have already know the bounding box of
// word is larger than the canvas.
if (!settings.drawOutOfBound) {
if (!settings.drawOutOfBound && !settings.shrinkToFit) {
var bounds = info.bounds;
if ((bounds[1] - bounds[3] + 1) > ngx ||
(bounds[2] - bounds[0] + 1) > ngy) {
Expand Down
31 changes: 31 additions & 0 deletions test/unit/basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module('Basics');

var testElement;

test('Test runs without any extra parameters.', function() {
var options = getTestOptions();
WordCloud(setupTest('default-testing-config'), options);
Expand All @@ -13,3 +15,32 @@ test('Empty list results no output.', function() {

WordCloud(setupTest('empty'), options);
});

function createTestHtmlElement() {
if (testElement) {
document.getElementsByTagName('body')[0].removeChild(testElement);
}
testElement = document.createElement('div');
testElement.id = 'test-span';
testElement.style.width = '300px';
testElement.style.height = '300px';
document.getElementsByTagName('body')[0].appendChild(testElement);

return testElement;
}

test('shrinkToFit shrinks words to fit grid', function() {
var testElement = createTestHtmlElement();
var options = getTestOptions();
options.drawOutOfBound = false;
options.shrinkToFit = true;
options.weightFactor = 15;

testElement.addEventListener('wordcloudstop', function () {
ok(testElement.innerText.indexOf(options.list[0][0]) !== -1, 'Word ' + options.list[0][0] + ' should be rendered.');
start();
});

stop();
WordCloud(testElement, options);
});

0 comments on commit 8316204

Please sign in to comment.