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;

그리기 객체의 선 너비를 정의하세요(반지름의 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();

추가로 참조하십시오:

CodeW3C.com의 완전한 Canvas 참조 가이드