کانواس کلچک انگل
ਚੌਥੀ ਹਿੱਸਾ - ਘੜੀ ਦੇ ਨੈੱਡਲ ਦਰਸਾਉਣਾ
ਘੜੀ ਨੂੰ ਨੈੱਡਲ ਦਰਸਾਉਣ ਲਈ ਇੱਕ JavaScript ਫੰਕਸ਼ਨ ਬਣਾਓ:
JavaScript:
function drawClock() { drawFace(ctx, radius); drawNumbers(ctx, radius); drawTime(ctx, radius); } function drawTime(ctx, radius) { const now = new Date(); let hour = now.getHours(); let minute = now.getMinutes(); let second = now.getSeconds(); // ਘੜੀ hour = hour%12; hour = (hour*Math.PI/6)+(minute*Math.PI/(6*60))+(second*Math.PI/(360*60)); drawHand(ctx, hour, radius*0.5, radius*0.07); // ਮਿੰਟ minute = (minute*Math.PI/30)+(second*Math.PI/(30*60)); drawHand(ctx, minute, radius*0.8, radius*0.07); // ਸੈਕੰਡ second = (second*Math.PI/30); drawHand(ctx, second, radius*0.9, radius*0.02); } function drawHand(ctx, pos, length, width) { ctx.beginPath(); ctx.lineWidth = width; ctx.lineCap = "round"; ctx.moveTo(0,0); ctx.rotate(pos); ctx.lineTo(0, -length); ctx.stroke(); ctx.rotate(-pos); }
ਕੋਡ ਵਿਸ਼ੇਸ਼ਤਾ
ਸਮੇਂ, ਮਿੰਟ ਅਤੇ ਸੈਕੰਡ ਪ੍ਰਾਪਤ ਕਰਨ ਲਈ ਇੱਕ Date ਆਬਜੈਕਟ ਬਣਾਓ:
const now = new Date(); let hour = now.getHours(); let minute = now.getMinutes(); let second = now.getSeconds();
ਘੜੀ ਦੇ ਕੋਣ ਨੂੰ ਹੱਲ ਕਰੋ ਅਤੇ ਇਸ ਦੀ ਲੰਬਾਈ (ਰੇਡੀਅਸ ਦਾ 50%) ਅਤੇ ਚੌਡਾਈ (ਰੇਡੀਅਸ ਦਾ 7%) ਨੂੰ ਦਰਸਾਓ:
hour = hour%12; hour = (hour*Math.PI/6)+(minute*Math.PI/(6*60))+(second*Math.PI/(360*60)); drawHand(ctx, hour, radius*0.5, radius*0.07);
ਇਸੇ ਤਕਨੀਕ ਨਾਲ ਘੜੀ ਅਤੇ ਸੈਕੰਡ ਦੀ ਨੈੱਡਲ ਦਰਸਾਉਣਾ ਹੈ。
drawHand() ਪ੍ਰੋਸੈੱਸ ਦੀ ਵਿਸ਼ੇਸ਼ਤਾ ਨੂੰ ਨਹੀਂ ਦੱਸਣਾ ਹੈ। ਇਹ ਦਿੱਤੇ ਲੰਬਾਈ ਅਤੇ ਚੌਡਾਈ ਦੇ ਰੇਖ ਨੂੰ ਦਰਸਾਉਂਦਾ ਹੈ。