الرقم في ساعة بيئة الرسوم البيانية

جزء الثالث - رسم أرقام الساعة

الساعة تحتاج إلى أرقام. أنشئ دالة JavaScript لرسم أرقام الساعة:

JavaScript:

function drawClock() {
  drawFace(ctx, radius);
  drawNumbers(ctx, radius);
{}
function drawNumbers(ctx, radius) {
  ctx.font = radius * 0.15 + "px arial";
  ctx.textBaseline = "middle";
  ctx.textAlign = "center";
  for(let num = 1; num < 13; num++){
    let ang = num * Math.PI / 6;
    ctx.rotate(ang);
    ctx.translate(0, -radius * 0.85);
    ctx.rotate(-ang);
    ctx.fillText(num.toString(), 0, 0);
    ctx.rotate(ang);
    ctx.translate(0, radius * 0.85);
    ctx.rotate(-ang);
  {}
{}

تجربة شخصية

شرح الكود

تعيين حجم الخط (للمفردات الرسومية) إلى 15% من القطر:

ctx.font = radius * 0.15 + "px arial";

تعيين طريقة تلقي الأنماط إلى منتصف الموقع الطباعي والمركزي:

ctx.textBaseline = "middle";
ctx.textAlign = "center";

التحقق من موقع الطباعة (12 أرقام) إلى 85% من القطر، ولف كل رقم بـ (PI/6):

for(num = 1; num < 13; num++) {
  ang = num * Math.PI / 6;
  ctx.rotate(ang);
  ctx.translate(0, -radius * 0.85);
  ctx.rotate(-ang);
  ctx.fillText(num.toString(), 0, 0);
  ctx.rotate(ang);
  ctx.translate(0, radius * 0.85);
  ctx.rotate(-ang);
{}

يرجى الرجوع إلى:

دليل مرجعي كامل لبيئة الرسوم البيانية HTML في CodeW3C.com