Why the wordcloud in the canvas is blurry?

吃可爱长大的小学妹 提交于 2020-01-03 05:37:09

问题


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:

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


回答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)]



来源:https://stackoverflow.com/questions/47010056/why-the-wordcloud-in-the-canvas-is-blurry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!