private generateSprites()

in src/WordCloud.ts [1090:1176]


    private generateSprites(
        context: CanvasRenderingContext2D,
        words: WordCloudDataPoint[],
        startIndex: number): void {

        context.clearRect(
            WordCloud.ContextStartPosition,
            WordCloud.ContextStartPosition,
            this.canvasViewport.width << WordCloud.WidthOffset,
            this.canvasViewport.height);

        let x: number = WordCloud.DefaultX,
            y: number = WordCloud.DefaultX,
            maxHeight: number = WordCloud.DefaultMaxHeightOfTheWord;

        for (let i: number = startIndex, length: number = words.length; i < length; i++) {
            let currentWordData: WordCloudDataPoint = words[i],
                widthOfWord: number = currentWordData.getWidthOfWord(),
                heightOfWord: number = currentWordData.size << WordCloud.PositionOffset;

            if (currentWordData.rotate) {
                const sr: number = Math.sin(currentWordData.rotate * WordCloud.Radians),
                    cr: number = Math.cos(currentWordData.rotate * WordCloud.Radians),
                    widthCr: number = widthOfWord * cr,
                    widthSr: number = widthOfWord * sr,
                    heightCr: number = heightOfWord * cr,
                    heightSr: number = heightOfWord * sr;

                widthOfWord = (Math.max(
                    Math.abs(widthCr + heightSr),
                    Math.abs(widthCr - heightSr)) + WordCloud.ByteMask) >> WordCloud.WidthOffset << WordCloud.WidthOffset;

                heightOfWord = Math.floor(Math.max(
                    Math.abs(widthSr + heightCr),
                    Math.abs(widthSr - heightCr)));
            } else {
                widthOfWord = (widthOfWord + WordCloud.ByteMask) >> WordCloud.WidthOffset << WordCloud.WidthOffset;
            }

            if (heightOfWord > maxHeight) {
                maxHeight = heightOfWord;
            }

            if (x + widthOfWord >= (this.canvasViewport.width << WordCloud.WidthOffset)) {
                x = 0;
                y += maxHeight;
                maxHeight = 0;
            }

            context.save();

            context.font = `normal normal ${currentWordData.size + WordCloud.AdditionalDataPointSize}px ${this.fontFamily}`;

            context.translate(
                (x + (widthOfWord >> WordCloud.PositionOffset)),
                (y + (heightOfWord >> WordCloud.PositionOffset)));

            if (currentWordData.rotate) {
                context.rotate(currentWordData.rotate * WordCloud.Radians);
            }

            context.fillText(currentWordData.text, 0, 0);

            if (currentWordData.padding) {
                context.lineWidth = WordCloud.LineWidthFactor * currentWordData.padding;
                context.strokeText(currentWordData.text, 0, 0);
            }

            context.restore();

            currentWordData.width = widthOfWord;
            currentWordData.height = heightOfWord;

            currentWordData.xOff = x;
            currentWordData.yOff = y;

            currentWordData.x1 = widthOfWord >> WordCloud.PositionOffset;
            currentWordData.y1 = heightOfWord >> WordCloud.PositionOffset;

            currentWordData.x0 = -currentWordData.x1;
            currentWordData.y0 = -currentWordData.y1;

            x += widthOfWord;
        }

        this.setSprites(context, words);
    }