وجه ساعة قماش
الجزء الثاني - ارسم وجه الساعة
الساعة تحتاج إلى وجه ساعة. قم بإنشاء وظيفة 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');
توقف اللون ينشئ تأثيرًا ثلاثي الأبعاد.
حدد التدرج اللوني على أنه نمط ضربة الفرشاة للكائن الرسومي:
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();