2015-12-21 9 views
5

私はキャンバス経由で線で接続したいと思っているたくさんのHTML要素を持っています。ここで私が達成しようとしているもののモックアップだ:現在2点間の中間点を探す

Mockup

、私はテキストのない、ラインを持っています。私は各行の中間にテキストを配置したいが、対角線であるので、どのように行うのか分からない。

現在のコード:

// 'connectors' is an array of points corresponding to 
// the middle of each big blue buttons' x-value 

ctx.clearRect(0,0,canvas.width,canvas.height); 
for(var i=0;i<connectors.length;i++){ 
    var wpoint = connectors[i]; 
    var pos1 = {w: wpoint, h: 0}; 
    var pos2 = {w: canvas.width/2, h: canvas.height}; 
    ctx.beginPath(); 
    ctx.moveTo(pos1.w,pos1.h); 
    ctx.lineTo(pos2.w,pos2.h); 
    ctx.stroke(); 

    // Write Text Halfway 
    ctx.fillStyle = "blue"; 
    ctx.font = "bold 16px Arial"; 
    ctx.fillText("2702", 100, canvas.height/2); 
    // No idea what to put as the x value here 

} 

これを達成するための最良の方法は何ですか?潜在的に線の半分を描き、テキストを書いて、残りの線を描くのですか?

EDIT:おそらく、より良いタイトル/質問は次のようになります。HTMLキャンバス内の任意の2つのポイントの中間点を見つけるにはどうすればよいですか?そこにテキストを描きたい。

+1

行がテキストではない、すべてではないでしょう傾斜しているので、ただ不思議を斜め線の真ん中が最も狭い線の真ん中と異なる高さになるので、同じレベルで表示されます –

+1

@ johnny5良い点 - 私はそう思います正確な 'x値 'を見つけ、' y値'をキャンバスの高さの半分にプロットすることです。 – Jascination

+1

それは、線を描き、同じy軸上の各点を見つけて、そのような部分を消去し、その上にテキストを描画する方が簡単かもしれません。 –

答えて

7

enter image description here

はここに方法は次のとおりです。

  • を計算し、ラインの中間点
  • 垂直いずれかを中心に水平&にその中間点
  • 知らせるキャンバスでラインを消去ライン
  • を描きます指定された[x、y]の周囲に描画されたテキスト
  • ここで中間点

でテキストを描画しますが、コードとデモ注釈を付けています:

var canvas=document.getElementById("canvas"); 
 
var ctx=canvas.getContext("2d"); 
 

 
var line={x0:20,y0:20,x1:150,y1:150}; 
 

 
textAtMidLine(line,'2702','verdana',14) 
 

 
function textAtMidLine(line,text,fontface,fontsize){ 
 

 
    // save the unmodified context state 
 
    ctx.save(); 
 

 
    // calc line's midpoint 
 
    var midX=line.x0+(line.x1-line.x0)*0.50; 
 
    var midY=line.y0+(line.y1-line.y0)*0.50; 
 

 
    // calc width of text 
 
    ctx.font=fontsize+'px '+fontface; 
 
    var textwidth=ctx.measureText(text).width; 
 

 
    // draw the line 
 
    ctx.beginPath(); 
 
    ctx.moveTo(line.x0,line.y0); 
 
    ctx.lineTo(line.x1,line.y1); 
 
    ctx.lineWidth=2; 
 
    ctx.strokeStyle='lightgray'; 
 
    ctx.stroke(); 
 

 
    // clear the line at the midpoint 
 
    ctx.globalCompositeOperation='destination-out'; // "erases" 
 
    ctx.fillRect(midX-textwidth/2,midY-fontsize/2,textwidth,fontsize*1.286); 
 
    ctx.globalCompositeOperation='source-over'; // reset to default 
 

 
    // tell canvas to horizontally & vertically center text around an [x,y] 
 
    ctx.textAlign='center'; 
 
    ctx.textBaseline='middle' 
 

 
    // draw text at the midpoint 
 
    ctx.fillText(text,midX,midY); 
 

 
    // restore the unmodified context state 
 
    ctx.restore(); 
 

 
}
body{ background-color: ivory; } 
 
#canvas{border:1px solid red; margin:0 auto; }
<canvas id="canvas" width=300 height=300></canvas>

+0

これは、ラインですか?私は、opがすべての行に翻訳された中間行の中点をcaculateすることを望んでいると思う。 –

+0

@ johnny5指定された各行の中点を見つける。すべての行に同じ 'y0'と' y1'(質問の例のイメージのように)がある場合、コードは変更されずに動作します。そして、はい、中間の 'y'を割り当てたい場合、すべての図面をその中間のyにクランプすることができます。その中間のyを垂直領域の中間点にしたい場合は、その垂直領域に対して同じmidXとmidYの計算を使用できます(x0 = 0とx1 = 0を使用して定義行を垂直にします)。 – markE

+1

私のために働く!あなたは一見シンプルな何かがJavascriptで簡単な方法を持っていると思うでしょう、悲しいことではありません。 – Jascination