1- import { Component , Inject } from '@angular/core' ;
1+ import { Component , ViewChild , ElementRef , AfterViewInit , Output , Inject } from '@angular/core' ;
22import { HttpClient } from '@angular/common/http' ;
3- import { send } from 'process' ;
43
54@Component ( {
65 selector : 'app-home' ,
76 templateUrl : './home.component.html' ,
87} )
9- export class HomeComponent {
8+ export class HomeComponent implements AfterViewInit {
9+ @ViewChild ( 'myCanvas' , { static : false } ) myCanvas : ElementRef ;
1010 socket : WebSocket ;
1111 frame : IDrawEntity [ ] = [ ] ;
1212 http : HttpClient ;
1313 baseUrl : string ;
1414 size : number [ ] = [ 2 , 2 ] ;
15- colors : number [ ] = [ 0 , 0 , 0 ] ;
15+ colors : number [ ] = [ 0 , 0 , 0 ] ;
16+ wrapperWidth : number = 600 ;
17+ connections : number = 0 ;
18+ collapse : boolean = false ;
19+ scale : boolean = false ;
1620
17- connections :number = 0 ;
21+ public canvasContext : CanvasRenderingContext2D ;
22+
23+ ngAfterViewInit ( ) : void {
24+ this . canvasContext = this . myCanvas . nativeElement . getContext ( '2d' ) ;
25+ }
1826
1927 constructor ( http : HttpClient , @Inject ( 'BASE_URL' ) baseUrl : string ) {
2028 this . http = http ;
2129 this . baseUrl = baseUrl ;
2230
23- this . http . get < number [ ] > ( this . baseUrl + 'api/Simulation/getSize' ) . subscribe ( this . setSize , error => console . error ( error ) ) ;
31+ this . requestSize ( ) ;
2432 this . initWebSocket ( new URL ( this . baseUrl ) . host ) ;
25-
26-
2733 }
2834
29- requestConnections = ( ) => this . http . get < number > ( this . baseUrl + `wsserver/getConnections` ) . subscribe ( this . setConnectionsCounter , error => console . error ( error ) ) ;
30- setConnectionsCounter = ( num :number ) => this . connections = num ;
35+ requestConnections = ( ) => this . http . get < number > ( this . baseUrl + `wsserver/getConnections` ) . subscribe ( this . setConnectionsCounter , error => console . error ( error ) ) ;
36+ setConnectionsCounter = ( num : number ) => this . connections = num ;
3137
32- clearCells = ( ) => {
33- this . http . get ( this . baseUrl + `api/Simulation/clearCells` ) . subscribe ( ) ;
34- }
38+ requestClearCells = ( ) => this . http . get ( this . baseUrl + `api/Simulation/clearCells` ) . subscribe ( ) ;
39+ requestGenerateCells = ( ) => this . http . get ( this . baseUrl + `api/Simulation/generateCells/${ 30 } ` ) . subscribe ( ) ;
3540
36- generateCells = ( ) => {
37- this . http . get ( this . baseUrl + `api/Simulation/generateCells/${ 30 } ` ) . subscribe ( ) ;
38- }
41+ requestSize = ( ) => this . http . get < number [ ] > ( this . baseUrl + 'api/Simulation/getSize' ) . subscribe ( this . setSize , error => console . error ( error ) ) ;
42+ setSize = ( result : number [ ] ) => this . size = result ;
3943
40- setSize = ( result : number [ ] ) => {
41- this . size = result ;
44+ draw ( ) {
45+ this . canvasContext . canvas . height = this . size [ 0 ] ;
46+ this . canvasContext . canvas . width = this . size [ 1 ] ;
47+ this . frame . forEach ( ( element ) => {
48+ this . canvasContext . strokeStyle = element . color ;
49+ this . canvasContext . fillStyle = element . color ;
50+ this . canvasContext . beginPath ( ) ;
51+ this . canvasContext . arc ( element . y , element . x , element . size / 2 , 0 , 2 * Math . PI , true ) ;
52+ this . canvasContext . fill ( ) ;
53+ } ) ;
4254 }
4355
4456 initWebSocket = ( host : string ) => {
4557 this . socket = new WebSocket ( "ws://" + host + "/ws" ) ;
46- this . socket . onopen = ( ) => { console . log ( "success" ) ; this . requestConnections ( ) ; } ;
58+ this . socket . onopen = ( ) => { console . log ( "success" ) ; this . requestConnections ( ) ; } ;
4759 this . socket . onmessage = ( msg ) => this . onUpdate ( msg ) ;
4860 this . socket . onclose = ( ) => { console . log ( "closed" ) ; } ;
4961 this . socket . onerror = ( ) => { console . log ( "error" ) ; } ;
@@ -53,16 +65,17 @@ export class HomeComponent {
5365 //console.log("income msg", msg.data);
5466 this . frame = this . parseDrawEntities ( msg . data ) ;
5567 this . calccolors ( ) ;
68+ this . draw ( ) ;
5669 }
5770
58- calccolors = ( ) => {
59- this . colors = [ 0 , 0 , 0 ] ;
71+ calccolors = ( ) => {
72+ this . colors = [ 0 , 0 , 0 ] ;
6073 this . frame . forEach ( element => {
61- if ( element . color == "Color [ Red]" ) {
74+ if ( element . color == " Red" ) {
6275 this . colors [ 0 ] ++ ;
63- } else if ( element . color == "Color [ Green]" ) {
76+ } else if ( element . color == " Green" ) {
6477 this . colors [ 1 ] ++ ;
65- } else if ( element . color == "Color [ Blue]" ) {
78+ } else if ( element . color == " Blue" ) {
6679 this . colors [ 2 ] ++ ;
6780 }
6881 } ) ;
@@ -71,6 +84,9 @@ export class HomeComponent {
7184 parseDrawEntities ( value : string ) : IDrawEntity [ ] {
7285 let container : IFrameContainer = JSON . parse ( value ) ;
7386 let frame : IDrawEntity [ ] = container . frame ;
87+ frame . forEach ( element => {
88+ element . color = element . color . substr ( 7 , element . color . length - 8 ) ;
89+ } ) ;
7490 return frame ;
7591 }
7692
0 commit comments