Canvas အချိန်ရောင်း အချက်

第二部分 - 绘制钟面

时钟需要钟面。创建一个 JavaScript 函数来绘制钟面:

JavaScript:

function drawClock() {
  drawFace(ctx, radius);
}
function drawFace(ctx, radius) {
  const 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.beginPath();
  ctx.arc(0, 0, radius, 0, 2 * Math.PI);
  ctx.fillStyle = 'white';
  ctx.fill();
  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;

ရေးဆွဲသည် အကွက် အနည်းငယ် ကို ဒီ ကြီးမားသော အကွက် အနည်းငယ် အား ကိုးကွယ်သည်:

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();

另请参阅:

CodeW3C.com 的完整 Canvas 参考手册