Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: maxRadius to avoid overflow #183

Open
wants to merge 1 commit into
base: gh-pages
Choose a base branch
from

Conversation

Ovilia
Copy link

@Ovilia Ovilia commented Dec 20, 2021

This PR fixes maxRadius so that text does not overflows the shape.

Before

Take shape: 'circle' and 'diamond' for example, when text list contains too many text, it will overflows the shape.

circle before

diamod before

After

circle after

diamond after

Test case used:

<html>
  <head>
    <style>
      body {
        margin: 0;
      }
      #main {
        border: 1px solid red;
        width: 400px;
        height: 400px;
      }
      #circle {
        position: absolute;
        left: 0;
        top: 0;
        width: 400px;
        height: 400px;
        border-radius: 200px;
        border: 1px solid blue;
      }
      #diamond {
        position: absolute;
        left: 58px;
        top: 58px;
        width: 282.8px;
        height: 282.8px;
        transform: rotate(45deg);
        transform-origin: 50% 50%;
        border: 1px solid blue;
      }
    </style>
  </head>
  <body>
    <canvas id="main"></canvas>
    <!-- <div id="circle"></div> -->
    <div id="diamond"></div>
    <script src="./src/wordcloud2.js"></script>
    <script>
      var data = [];
      for (var i = 0; i < 400; i++) {
        data.push([
          'wordcloud'.slice(0, Math.ceil(Math.random() * 9)),
          Math.round(Math.random() * 40) + 5,
        ]);
      }

      var canvas = document.getElementById('main');
      canvas.width = 800;
      canvas.height = 800;

      WordCloud(canvas, {
        list: data,
        ellipticity: 1,
        gridSize: 4,
        shape: 'diamond',
      });
    </script>
  </body>
</html>

@timdream
Copy link
Owner

Thank for the contribution.

I don't think the fix is correct. Math.floor(Math.max(ngx, ngy) / 2) is the distance to the nearest edge from the center, right? First of all, center can be moved, so you would need consider that. Second of all, what if the shape is a square and we do want to fill all four corners of the canvas? The proposed maxRadius in that case will not be enough.

Feel free to propose better logic (preferably without yet another options — there are too many of these and they are conflicting sometimes, as evidenced by this case — sorry for the bad catch-all API design!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants