0

Here is my code in question:

$(document).ready(render)


function render() {

  wl = [['hello', 12], ['dear', 10], ['a', 9], ['Joe', 5], ['8', 2]];

  $canvas = $('.wordcloud-canvas');
  options = {
    list           : wl,
    fontFamily     : 'Times, serif',
    weightFactor   : 2,
    color          : '#f02222',
    rotateRatio    : 0,
    rotationSteps  : 0,
    shuffle        : false,
    backgroundColor: 'white',
    drawOutOfBound : false,
    gridSize       : 32
  };
    
  window.WordCloud($canvas[0], options);
  
}
.wordcloud-view {
  display: block;
  margin: 01px 0;
    width: 100%;
    height: 100%;
}
.wordcloud-container {
  border: 1px blue solid;
  padding: 2px;
  width: 600px;
  height: 300px;
  margin: 0px;
}

.wordcloud-canvas {
  width: 600px;
  height: 300px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/wordcloud2.js/1.0.6/wordcloud2.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wordcloud-view">
  <div class="wordcloud-container">
   <canvas id="wordcloud-canvas" class="wordcloud-canvas"></canvas>
   <div class="user-input"></div>
  <div></div>
</div></div>

Somehow the output is very blurry:

enter image description here

What factors are blurring the output here?

I am using wordcloud2.js to generate the word cloud.

I have tried increasing the grid size but it does not help. I think I am at loss here because I do not understand how the options works with the canvas.

You can also find the above code in this codepen: https://codepen.io/kongakong/pen/jaEMXG

Anthony Kong
  • 37,791
  • 46
  • 172
  • 304

1 Answers1

1

Add width="600" and height="300" to the element (or set it with JS before rendering the wordcloud – $canvas.attr('width', '600').attr('height', '300')).

It's not same as setting it in CSS only, details here: stackoverflow.com/a/43364730/3776299

$(document).ready(render);


function render() {
  var wl = [['hello', 12], ['dear', 10], ['a', 9], ['Joe', 5], ['8', 2]];

  var $canvas = $('.wordcloud-canvas');
  // this line is the only change from your snippet:
  $canvas.attr('width', '600').attr('height', '300');
  var options = {
    list           : wl,
    fontFamily     : 'Times, serif',
    weightFactor   : 2,
    color          : '#f02222',
    rotateRatio    : 0,
    rotationSteps  : 0,
    shuffle        : false,
    backgroundColor: 'white',
    drawOutOfBound : false,
    gridSize       : 32
  };
    
  window.WordCloud($canvas[0], options);
  
}
.wordcloud-canvas {
  width: 600px;
  height: 300px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/wordcloud2.js/1.0.6/wordcloud2.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<canvas id="wordcloud-canvas" class="wordcloud-canvas"></canvas>

That of course leaves the words a bit undersized, because of the small weightFactor in your options. Values around 6-7 work pretty well with the 600x300 size. You can also calculate the factor based on the canvas size and word weight dynamically, for example:

weightFactor: function(size) {
    return Math.pow(size, 2.3) * $canvas.width() / 1024;
},

[taken & adjusted from the wordcloud2 website("Configuration" tab)]

enter image description here

helb
  • 3,154
  • 15
  • 21