2

I'd like to put

  1. a circle or
  2. a broken circle (like if I were to draw a circle freehand without completely closing it) or
  3. a square, or
  4. brackets

i.e.

       non-bracketed text. non-bracketed 
       text. non-bracketed text. non-
    +- bracketed text. Bracketed text    -+
    |  that can span several lines and    |
    +- more.  non-bracketed text.  non-  -+
       bracketed text. non-bracketed
       text.

I saw this: Easier way to create circle div than using an image? but text is in the top left hand corner if I were to draw a circle around it, which isn't what I want.

I saw this: css: how to draw circle with text in middle? but text is centred and I want the circle to go around the text, not move the text to be within the circle.


CLARIFICATION

The text is already aligned and I do not want it to be realigned so that the circle will go around it. The circle must move to compensate for the text, NOT the other way around.

Also, I need to be able to switch between the 4 different circles fairly easily. Preferable by just changing the class of the tag.

Here is a sample as to what I'm looking for:

sample

Question:

Can I please get people to vote this open so it can get this post off hold? This is quite specific and I would challenge anyone to say why it is not.

Answer:

With the help of dcclassics, asking of more questions and doing more searching, I've got a solution.

http://jsfiddle.net/adrianh/4SVHH/7/

function getCanvas(i, left, top, width, height)
{
    var canvas;
    var circles = document.getElementById("circles");
    if (typeof circles.childNodes[i] != 'undefined')
    {
        canvas = circles.childNodes[i];
        canvas.width=width;
        canvas.height=height;
        canvas.style.left=left+"px";
        canvas.style.top=top+"px";
    }
    else
    {
        canvas = "<canvas "+
        "width='"+width+"' "+
        "height='"+height+"' "+
        "style='"+
            "position:absolute;"+
            "z-index:0;"+
            "left:"+left+"px;"+
            "top:"+top+"px;"+
            "pointer-events:none;"+
            //"border:1px solid;"+
        "' />";
        circles.insertAdjacentHTML('beforeend', canvas);
    }
    return circles.childNodes[i];
}

function circleRect(i, rect)
{
    var diameter = Math.sqrt(rect.width*rect.width+rect.height*rect.height);
    var cx = (rect.right + rect.left)/2;
    var cy = (rect.top + rect.bottom)/2;
    var left = Math.floor(cx - diameter/2);
    var top  = Math.floor(cy - diameter/2);
    diameter = Math.floor(diameter);
    var canvas = getCanvas(i, left-1, top-1, diameter+2, diameter+2);
    var ctx=canvas.getContext("2d");
    ctx.beginPath();
    ctx.arc(diameter/2+1,diameter/2+1,diameter/2,0,2*Math.PI);
    ctx.lineWidth=2;
    ctx.strokeStyle = "red";
    ctx.stroke();
}

function rectRect(i, rect)
{
    var canvas = getCanvas(i, rect.left-1, rect.top-1, rect.width+2, rect.height+2);
    var ctx=canvas.getContext("2d");
    ctx.beginPath();
    ctx.moveTo(1, 1);
    ctx.lineTo(rect.width+1, 1);
    ctx.lineTo(rect.width+1, rect.height+1);
    ctx.lineTo(1, rect.height+1);
    ctx.lineTo(1, 1);
    ctx.strokeStyle = "red";
    ctx.lineWidth=2;
    ctx.stroke();
}

function drawCircles()
{
    $(".circled").each(function(i, obj) {
        var rect = obj.getBoundingClientRect();
        if (/\brect\b/.test(obj.className))
        {
            rectRect(i, rect);
        }
        else
        {
            circleRect(i, rect);
        }
    });
}
document.body.insertAdjacentHTML('afterbegin', '<div id="circles"></div>');
drawCircles();

window.onresize=drawCircles;

The solution presented shows only a circle and a rectangle, but this can be modified to be used for any type of circling method that I asked for. This doesn't use a svg file, but uses a canvas instead. This may limit it to more recent browsers, but that isn't a problem for me.

Community
  • 1
  • 1
Adrian
  • 10,246
  • 4
  • 44
  • 110
  • 2
    Is there a question here? – j08691 May 16 '14 at 20:01
  • 1
    Use an image. Scale it if necessary. – Ruud Helderman May 16 '14 at 20:03
  • @j08691 Yes, how would I circle around text in an HTML document. – Adrian May 16 '14 at 20:03
  • @Ruud, using CSS or an image (which I guess you mean by using it as a background?) this wouldn't work if this were a circle/oval. I don't see how I would get it to work if the text is to remain within the circle. Could you expand please? – Adrian May 16 '14 at 20:06
  • "I want the circle to go around the text", If you draw a circle around text which is aligned horizontally, obviously the text will be in the middle of the circle. So actually you want the text to be aligned in a circular manner, right..? – T J May 16 '14 at 20:12
  • @TJ I don't want the text to be moved, I want the circle to be around the already aligned text. – Adrian May 16 '14 at 20:18
  • It's easy to put brackets around text... am I missing something there? – andi May 16 '14 at 20:34
  • Yes, I know that it is easy to put brackets around the text, but this is to grab attention and different visual mechanisms are to be used in different instances. It is a requirement that I was given. – Adrian May 16 '14 at 20:36
  • How is this too broad? It is very specific. I'm not sure what is meant by "too broad". – Adrian May 16 '14 at 20:38
  • You can use an svg as background-image of your text, and add the property background-size:cover (or the width and height of the text container). For the brackets you can use css property 'content' used with selectors :before and :after – keypaul May 16 '14 at 20:39
  • @keypaul, can you post that as an answer with a sample? – Adrian May 16 '14 at 20:44
  • 3
    The question is too broad because you want someone to code up the entire solution for you. Do some work, get stuck (or not!) and then we can help you work out the kinks. But, at first glance, keypaul's `svg` suggestion is probably the most flexible. Ruud's image suggestion is the easiest. A css solution is likely to be limited, I don't know how you'd do a broken circle that way at all, and brackets would be kind of hacky. – Jason May 16 '14 at 21:43
  • @Jason, I've pretty much gotten as far as dcclacics had. Although I know some html and css, this is not my forte. I'm a C++ developer. Not only that, but too broad according to the SO closed questions page states `if your question could be answered by an entire book, or has many valid answers, it's probably too broad for our format`. This is definitely not the case. To mark it as such is an abuse of the system. – Adrian May 16 '14 at 21:56
  • It has many valid answers, by the nature that there are multiple potential solutions, there are multiple ways to accomplish those solutions, and you have posted none of your own code to guide us to a particular one. It's an abuse of the system to use us to get you up to speed on things that are not your forte without your posting even a line of code. I sympathize with your situation, but I think you've gotten enough here to do some more work on your own down your chosen path, and come back (with code!) if you get stuck again. This is the way SO works. – Jason May 19 '14 at 13:09
  • @Jason, I only saw the background image answer. However, I didn't really see any other answer. Even looking in to this further, I only found one other potential solution, which I have posted. Do you have any other potential solutions you can think of? – Adrian May 20 '14 at 21:07

1 Answers1

1

Here is a jsFiddle of a circle around text.

<div class="circle">Circle</div>

.circle {
    border-radius: 50%;
    width: 100px;
    height: 100px;
    border: 1px solid black;
    text-align: center;
    display: table-cell;
    vertical-align: middle;
}

http://jsfiddle.net/V9Gn5/ shows an elipse, not a circle, around some text, but without moving the text.

Edit: Here's as close as I could get right now, if someone else would like to play around with it. http://jsfiddle.net/V9Gn5/19/

dcclassics
  • 896
  • 1
  • 12
  • 38
  • Now change `text-align` to `left` and `vertical-align` to `top` and see what you get. That is what I don't want, which is what I said in the 3rd paragraph. – Adrian May 16 '14 at 20:16
  • @Adrian, so let's say the text is at the exact upper left corner of your document. How would you expect the circle to look? – andi May 16 '14 at 20:28
  • @andi With the text bounding box inscribed within the circle. – Adrian May 16 '14 at 20:29
  • but then the circle wouldn't even fit on the page - you would expect the circle to be chopped off at the top left? – andi May 16 '14 at 20:30
  • I dont think anyone understands really what you're looking for here. – dcclassics May 16 '14 at 21:00
  • Here a fiddle with some example http://jsfiddle.net/keypaul/g3YjT/28/ Maybe post an image or other so we can understand better what do you expect – keypaul May 16 '14 at 21:16
  • http://jsfiddle.net/V9Gn5/16/ Something like this is really the only other thing I could recommend. Unless you show us an image of your desired outcome. – dcclassics May 16 '14 at 21:33
  • Okay, that helps a lot. Give me a few minutes to mock something up. – dcclassics May 16 '14 at 21:36
  • Hmmm, I'd rather that (and the specs require that) the bounding box is inscribed within the circle (ellipse), even when spanning lines. As you can see [here](http://jsfiddle.net/cLfZV/), it is not. You may have to narrow your browser to see it span a line. – Adrian May 16 '14 at 21:45
  • http://jsfiddle.net/V9Gn5/19/ this is as close as I can get right now if someone wants to play with it. – dcclassics May 16 '14 at 21:46
  • 1
    Svg i think works for you, cause you can use it as image, also scaling, stretching without loose quality. You can use it as background of your text ( like the fiddle in my previous comment) – keypaul May 16 '14 at 23:17
  • http://jsfiddle.net/keypaul/RCkQ3/32/ here little fiddle of what i mean – keypaul May 17 '14 at 08:48
  • Have you solved your problem? – keypaul Jun 06 '14 at 20:50