Help us understand the problem. What is going on with this article?

JavaScriptで円に沿って文字を並べる

More than 3 years have passed since last update.

スクリーンショット 2015-08-30 22.25.44.png

html
<div class="circle-text">あいうえお</div>

css
.circle-text{
  width: 300px;
  height: 300px;
  border-radius: 100%;
  background: blue;
  color: #fff;
  margin: 0 auto;
  position: relative;
  font-size: 2.5em;
}

javascript
var textbox = document.querySelector('.circle-text');

circleText(textbox,20);

function circleText (textElement,angle) {
  var parentR = textbox.offsetWidth/2;
  var text = textElement.textContent;  
  var chars = text.split('');
  if(chars.length%2 === 0){
    chars.splice(chars.length/2,0,' ');
  }
  textElement.innerHTML = null;

  chars.forEach(function (c, i, arr) {
    var span = document.createElement('span');
    span.textContent = c;
    span.style.display   = 'inline-block';
    span.style.position  = 'absolute';
    span.style.left = '50%';
    span.style.top = '50%';

    span.style.transformOrigin = 'left top';
    span.style.transform = 
      'rotate(' + (i*angle-angle*((arr.length/2)>>0)) + 'deg) ' +
      'translate(0%,40%) ' + 
      'translate(0, -' + (parentR) + 'px) ' +
      'translate(-50%,-50%)';
    textElement.appendChild(span);
  });
}

circleText(textbox,20) 引数はテキスト要素と文字の角度を渡しています。
文字数が偶数の場合は空白を追加して奇数になるようにしています。
スクリーンショット 2015-08-30 22.26.23.png

spanの配置はCSSのみで時計を作るを参考にしました。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした