A word cloud looks like a toy. Getting one to look good is a small but genuinely interesting engineering problem: you are packing differently-sized rectangles into a bounded region, in descending priority order, while keeping the result visually balanced.
Under the hood there are four steps β weighting, font scaling, spiral placement with collision detection, and shape masking. Here's how they fit together, with plain JavaScript you can paste into a canvas.
1. Turn text into weights
const STOP = new Set ( [ 'the' , 'and' , 'of' , 'to' , 'a' , 'in' , 'is' , 'it' , 'you' , 'that' , 'for' , 'on' , 'with' , 'as' , 'be' , 'at' , 'by' , 'this' , 'are' , 'or' , 'from' , 'but' , 'not' , 'we' , 'your' , 'can' , 'my' , 'all' , 'was' , 'one' ] ) ;
function weigh ( text ) { const counts = new Map ( ) ; const tokens = text . toLowerCase ( ) . match ( / [ \ p { L } \ p { N } ']+/gu) ?? []; for (const token of tokens) { if (token.length < 2 || STOP.has(token)) continue; counts.set(token, (counts.get(token) ??β¦