Introduction
In this article, I will explain about how to create a clock using Angular.
Prerequisites
In this article we will learn how to create a clock using canvas in Angular.
For this article, I have created an Angular project using Angular 7. For creating an Angular project, we need to follow the following steps:
Step 1
I have created a project using the following command in the Command Prompt.
Step 2
Open a project in Visual Studio Code using the following commands.
Step 3
Create a new component using the following commands.
Import the following image in Assets folder:
Import the following dependancy in clock.component.ts:
- import from '@angular/core';
- import from '@angular/core';
- import from 'rxjs';
- import from 'rxjs/operators';
- import from '../tdate';
- import from '@angular/common';
- import from '@angular/common';
clock.component.ts
Add the following code in clock.component.ts:
- import from '@angular/core';
- import from '@angular/core';
- import from 'rxjs';
- import from 'rxjs/operators';
- import from '../tdate';
- import from '@angular/common';
- import from '@angular/common';
- @Component(
-
-
-
-
- )
- export class ClockComponent implements OnInit, OnDestroy, AfterViewInit
- ) canvasRef: ElementRef;
- public tdate=new Date();
- @Input() public width = 100;
- @Input() public height = 100;
- canvasContext: CanvasRenderingContext2D;
- subscription: Subscription;
- constructor(private ngZone: NgZone,private datePipe: DatePipe)
-
-
- ngOnInit()
- ngAfterViewInit()
-
-
-
-
-
-
-
-
-
- ngOnChanges(change)
-
-
-
- if (change.tdate)
-
-
-
-
-
-
-
-
-
-
-
-
- }
- }
- ngOnDestroy()
-
- draw(innerRadius: number)
-
-
-
- drawBackground(canvasContext, innerRadius)
-
-
-
-
-
-
-
-
- else if(am_pm=='PM' && background.src != "/assets/clockPM.png")
-
-
-
- let $this = this;
- background.onload = function ()
-
-
-
-
-
-
-
- )
- )
- .subscribe(s=>
-
- );
- }
- }
- drawFace(ctx, radius)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- drawNumbers(ctx, radius)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
-
- drawTime(ctx, radius,date)
- = new TDate(date);
- const hourHand =
- (hours % 12) * Math.PI / 6 + minutes * Math.PI / (6 * 60) + seconds * Math.PI / (360 * 60);
-
- this.drawHourHand(ctx, date, radius * 0.6, radius * 0.05);
- const minuteHand = minutes * Math.PI / 30 + seconds * Math.PI / (30 * 60);
- this.drawMinuteHand(ctx, date, radius * 0.8, radius * 0.04);
-
- const secondHand = seconds * Math.PI / 30;
- this.drawSecondHand(ctx, date, radius * 0.9, radius * 0.03);
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
- degreesToRadians(degrees)
-
-
-
- drawHourHand(ctx, theDate, length, width)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- drawMinuteHand(ctx, theDate, length, width)
-
-
-
-
-
-
-
-
-
-
-
-
- drawHand(ctx, size, thickness, shadowOffset)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- drawSecondHand(ctx, theDate, length, width)
-
-
-
-
-
-
-
-
-
-
-
-
- }
App.module.ts
- import from '@angular/platform-browser';
- import from '@angular/core';
- import from '@angular/common';
- import from './app-routing.module';
- import from './app.component';
- import from './clock/clock.component';
-
- @NgModule(
-
-
-
-
-
-
-
-
-
-
-
- )
- export class AppModule
App.components.html
- <span height="70" align="right" class="float-right" id="canvasClock">
- <cs-canvas-clock></cs-canvas-clock>
- </span>
Now, run the project using the following command:
Summary
In this article, I have discussed how to create a clock using canvas in Angular.