【问题标题】:Why the wordcloud in the canvas is blurry?为什么画布上的文字云是模糊的?
【发布时间】:2017-10-30 07:33:22
【问题描述】:

这是我的代码:

$(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>

不知何故,输出非常模糊:

这里的输出模糊的因素是什么?

我正在使用wordcloud2.js来生成词云。

我尝试增加网格大小,但没有帮助。我想我在这里不知所措,因为我不明白options 如何与画布一起使用。

你也可以在这个codepen中找到上面的代码:https://codepen.io/kongakong/pen/jaEMXG

【问题讨论】:

    标签: javascript canvas word-cloud


    【解决方案1】:

    在元素中添加width="600"height="300"(或者在渲染wordcloud之前用JS设置——$canvas.attr('width', '600').attr('height', '300'))。

    这与只在 CSS 中设置不同,详情请看: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>

    这当然会使单词有点小,因为您的options 中的weightFactor 很小。 6-7 左右的值在 60​​0x300 尺寸下效果很好。您还可以根据画布大小和字重动态计算因子,例如:

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

    [取自wordcloud2 website(“配置”选项卡)]

    【讨论】:

      猜你喜欢
      • 2018-06-14
      • 2013-10-22
      • 2016-10-29
      • 2020-10-04
      • 2014-10-20
      • 1970-01-01
      • 2021-04-07
      • 1970-01-01
      • 2023-03-20
      相关资源
      最近更新 更多