Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8568799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:11:25+00:00 2026-06-11T18:11:25+00:00

I am using Raphael.js to create polygons, (or actually closed paths) over an image.

  • 0

I am using Raphael.js to create polygons, (or actually closed paths) over an image. I want to put text inside the polygons. Is there any way to create a text element and then adjust its size and position so that the whole thing will be within the bounds of a polygon?

Are there at least any algorithms that can give me good guesses that I could manually adjust?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:11:26+00:00Added an answer on June 11, 2026 at 6:11 pm

    Okay, I gave this some thought over the course of the day, and finally had the chance to throw together a little demo code. It is glaringly imperfect in many ways but may form a good starting place, and does seem to work well with most of my randomly generated polygons.

    Part 1: Centering the text over the polygon. Using the bounding box proved wildly unsatisfactory, so instead of I devised a technique to average all of the points in a given path container:

                //  Determine the *weighted* center of this figure
                var segments = Raphael.parsePathString( container.attr( 'path' ) );
                var count = 0, sum_x = 0, sum_y = 0;
                for ( var i = 0; i < segments.length; i++ )
                {
                    if ( segments[i][0] == 'L' || segments[i][0] == 'M' )
                    {
                        count++;
                        sum_x += segments[i][1];
                        sum_y += segments[i][2];
                    }
                }
    
                var cx = sum_x / count, cy = sum_y / count;
    

    Part 2: Collision detection. I needed a mechanism to check if a given object is inside the current shape. Being lazy, I simply extended the Raphael element object prior to creating my canvas.

            Raphael.el.isObjectInside = function( obj )
            {
                var box = obj.getBBox();
                return this.isPointInside( box.x, box.y ) 
                            &&
                        this.isPointInside( box.x2, box.y )
                            &&
                        this.isPointInside( box.x, box.y2 )
                            &&
                        this.isPointInside( box.x2, box.y2 );
            }
    

    Part 3: Iterative sizing. Basically, we use the coordinates produced above and apply them to the generated label text. We reduce the scale once each iteration until we go beneath a certain threshold, at which point MY code simply gives up (yours probably shouldn’t):

                var s = 1.0;
                if ( ibox.width >= cbox.width * 0.7 )
                {
                    s = ( cbox.width * 0.7 ) / ibox.width;
                }
    
                while ( s > 0.1 )
                {
                    insert.attr( { transform: [ "S", s, s, ibox.x + ibox.width / 2, ibox.y + ibox.height / 2, "T", 
                    if ( container.isObjectInside( insert ) )
                        return;
                    s *= 0.65;
                }
    
                console.log("Warning: NOT a clean fit" );
    

    And the result looks well for most of the purely random polygons I threw at it. I’m sure it’s far more computationally intensive than it needs to be. My code is staged here — just click on the polygon to generate a new one.

    Happy coding.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an image animation using Raphael JS. I want the effect
I am using Raphael and I want to create a rect and control the
So I have been using raphael to create a user interface and want it
Are there any examples to create a process-Map or Mind-Mapping style visualizations using Javascript
I'm using Raphael js framework to create interactive svg image on client: var paper
I am using Raphael to draw some paths. Each path has an associated rectangle
I have a png graphic that is put in the web page using raphael
I'm using the Raphael library to create SVG objects. To initialise the SVG shape
I'm using the Raphaël—JavaScript Library to create some pie chart graphics, but I want
Using the example from How to achieve donut holes with paths in raphael (

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.