أرقام ساعة الكanvas

جزء ثاني - رسم وجه الساعة

الساعة تحتاج إلى وجه الساعة. إنشاء وظيفة 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');

إشارة: يمكن لهذه الألوان الثلاثة إنتاج تأثير ثلاثي الأبعاد.

تعريف التدرج كنمط الفرشاة للرسام:

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

انظر أيضًا:

دليل مرجعي كامل لـ Canvas في CodeW3C.com