WordCloud.isSupported
will evaluates to false
if the browser doesn't supply necessary functionalities for wordcloud2.js to run.
Some browsers come with restrictions on minimum font size preference on, and the preference will also impact canvas. wordcloud2.js works around it by scaling the canvas, but you may be interested to know value of the preference. The value detected is accessible at
WordCloud.minFontSize
Sometimes we need to stop wordcloud2.js renderring, to optimize the component renderring performance, especially in some FE libraries like 'React'. In this scenario, you can just call the function below
WordCloud.stop
useEffect(() => {
...
return () => {
// stop the renderring
WordCloud.stop();
};
}, [deps]);
WordCloud(elements, options);
elements
is the DOM Element of the canvas, i.e. document.getElementById('my_canvas')
or $('#my_canvas')[0]
in jQuery.
It can be also an array of DOM Elements. If a <canvas>
element is passed, Word Cloud would generate an image on it; if it's some other element, Word Cloud would create <span>
elements and fill it.
Depend on the application, you may want to create an image (high fidelity but interaction is limited) or create the "cloud" with DOM to do further styling.
Available options as the property of the options
object are:
list
: List of words/text to paint on the canvas in a 2-d array, in the form of[word, size]
.- e.g.
[['foo', 12], ['bar', 6]]
- Optionally, you can send additional data as array elements, in the form of
[word, size, data1, data2, ... ]
which can then be used in the callback functions ofclick
,hover
interactions and fontWeight, color and classes callbacks. - e.g.
[['foo', 12, 'http://google.com?q=foo'], ['bar', 6, 'http://google.com?q=bar']]
.
- e.g.
fontFamily
: font to use.fontWeight
: font weight to use, can be, as an example,normal
,bold
or600
or acallback(word, weight, fontSize, extraData)
specifies different font-weight for each item in the list.color
: color of the text, can be any CSS color, or acallback(word, weight, fontSize, distance, theta)
specifies different color for each item in the list. You may also specify colors with built-in keywords:random-dark
andrandom-light
. If this is a DOM cloud, color can also benull
to disable hardcoding of color into span elements (allowing you to customize at the class level).classes
: for DOM clouds, allows the user to define the class of the span elements. Can be a normal class string, applying the same class to every span or acallback(word, weight, fontSize, extraData)
for per-span class definition. In canvas clouds or if equalsnull
, this option has no effect.minSize
: minimum font size to draw on the canvas.weightFactor
: function to call or number to multiply forsize
of each word in the list.clearCanvas
: paint the entire canvas with background color and consider it empty before start.backgroundColor
: color of the background.
gridSize
: size of the grid in pixels for marking the availability of the canvas — the larger the grid size, the bigger the gap between words.origin
: origin of the “cloud” in[x, y]
.drawOutOfBound
: set totrue
to allow word being draw partly outside of the canvas. Allow word bigger than the size of the canvas to be drawn.shrinkToFit
: set totrue
to shrink the word so it will fit into canvas. Best ifdrawOutOfBound
is set tofalse
.⚠️ This word will now have lowerweight
.
drawMask
: visualize the grid by draw squares to mask the drawn areas.maskColor
: color of the mask squares.maskGapWidth
: width of the gaps between mask squares.
wait
: Wait for x milliseconds before start drawn the next item usingsetTimeout
.abortThreshold
: If the call with in the loop takes more than x milliseconds (and blocks the browser), abort immediately.abort
: callback function to call when abort.
minRotation
: If the word should rotate, the minimum rotation (in rad) the text should rotate.maxRotation
: If the word should rotate, the maximum rotation (in rad) the text should rotate. Set the two value equal to keep all text in one angle.rotationSteps
: Force the use of a defined number of angles. Set the value equal to 2 in a -90°/90° range means just -90, 0 or 90 will be used.
shuffle
: Shuffle the points to draw so the result will be different each time for the same list and settings.rotateRatio
: Probability for the word to rotate. Set the number to 1 to always rotate.
shape
: The shape of the "cloud" to draw. Can be any polar equation represented as a callback function, or a keyword present. Available presents arecircle
(default),cardioid
(apple or heart shape curve, the most known polar equation),diamond
,square
,triangle-forward
,triangle
, (alias oftriangle-upright
),pentagon
, andstar
.ellipticity
: degree of "flatness" of the shape wordcloud2.js should draw.
hover
: callback to call when the cursor enters or leaves a region occupied by a word. The callback will take argumentscallback(item, dimension, event)
, whereevent
is the originalmousemove
event.click
: callback to call when the user clicks on a word. The callback will take argumentscallback(item, dimension, event)
, whereevent
is the originalclick
event.
You can listen to those custom DOM events filed from the canvas element, instead of using callbacks for taking the appropriate action. Cancel the first two events causes the operation to stop immediately.
wordcloudstart
wordclouddrawn
wordcloudstop
wordcloudabort
wordcloud2.js itself will stop at wordcloudstart
event.