キャンバスの時計の文字盤


パートII-文字盤を描く

時計には文字盤が必要です。時計の文字盤を描画するJavaScript関数を作成します。

JavaScript:

function drawClock() {
  drawFace(ctx, radius);
}

function drawFace(ctx, radius) {
  var grad;

  ctx.beginPath();
  ctx.arc(0, 0, radius, 0, 2 * Math.PI);
  ctx.fillStyle = 'white';
  ctx.fill();

  grad = ctx.createRadialGradient(0, 0 ,radius * 0.95, 0, 0, radius * 1.05);
  grad.addColorStop(0, '#333');
  grad.addColorStop(0.5, 'white');
  grad.addColorStop(1, '#333');
  ctx.strokeStyle = grad;
  ctx.lineWidth = radius*0.1;
  ctx.stroke();

  ctx.beginPath();
  ctx.arc(0, 0, radius * 0.1, 0, 2 * Math.PI);
  ctx.fillStyle = '#333';
  ctx.fill();
}


コードの説明

時計の文字盤を描画するためのdrawFace()関数を作成します。

function drawClock() {
  drawFace(ctx, radius);
}

function drawFace(ctx, radius) {
}

白い円を描きます:

ctx.beginPath();
ctx.arc(0, 0, radius, 0, 2 * Math.PI);
ctx.fillStyle = 'white';
ctx.fill();

放射状グラデーションを作成します(元のクロック半径の95%と105%):

grad = ctx.createRadialGradient(0, 0, radius * 0.95, 0, 0, radius * 1.05);

円弧の内側、中央、外側のエッジに対応する3つのカラーストップを作成します。

grad.addColorStop(0, '#333');
grad.addColorStop(0.5, 'white');
grad.addColorStop(1, '#333');

カラーストップは3D効果を作成します。

グラデーションを描画オブジェクトのストロークスタイルとして定義します。

ctx.strokeStyle = grad;

描画オブジェクトの線幅を定義します(半径の10%)。

ctx.lineWidth = radius * 0.1;

円を描く:

ctx.stroke();

時計の中心を描きます。

ctx.beginPath();
ctx.arc(0, 0, radius * 0.1, 0, 2 * Math.PI);
ctx.fillStyle = '#333';
ctx.fill();