Binding to Custom Events of Component in Angular

server.component.ts

import {Component, OnInit, EventEmitter, Output} from '@angular/core';

@Component({
  selector: 'app-server',
  templateUrl: './server.component.html',
  styleUrls: ['./server.component.css']
})
export class ServerComponent implements OnInit {

  // this is now an event
  @Output() statusChanged = new EventEmitter<{ serverId: number, time: Date }>();
  status = 'offline';

  constructor() {
    setTimeout(() => {
      this.status = 'online';

      // fire event
      this.statusChanged.emit({serverId: 10, time: new Date()});
    }, 5000);
  }

  ngOnInit() {
  }

}

server.component.html

<p>
  server is {{status}}
</p>

app.component.ts

import {Component} from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  serverId;
  time;

  /**
   * receive data from event
   */
  onServerStatusChanged(data: { serverId: number, time: Date }) {
    this.serverId = data.serverId;
    this.time = data.time;
  }
}

app.component.html

<app-server (statusChanged)="onServerStatusChanged($event)"></app-server>
<p>Server Id : {{serverId}}, Date : {{time}}</p>

Assigning an Alias to Custom Events

@Output('nicolas') nic  = new EventEmitter<any>();

References
https://github.com/mhdr/AngularSamples/tree/master/013/my-app

Binding to Custom Properties of Components in Angular

server.component.ts

import {Component, OnInit, Input} from '@angular/core';

@Component({
  selector: 'app-server',
  templateUrl: './server.component.html',
  styleUrls: ['./server.component.css']
})
export class ServerComponent implements OnInit {

  // Now this is a property for component
  @Input() firstName: string;

  constructor() {
  }

  ngOnInit() {
  }

}

server.component.html

<p>
  Hello {{firstName}}
</p>

app.component.html

<app-server [firstName]="'Mahmood'"></app-server>
<app-server [firstName]="'Mehdi'"></app-server>

Assigning an Alias to Custom Properties

@Input('selectedHero') hero: string;

References
https://github.com/mhdr/AngularSamples/tree/master/012/my-app

Outputting Lists with ngFor on Angular

server.component.html

<p>
  server {{serverId}} is {{status}}
</p>

server.component.ts

import {Component, OnInit} from '@angular/core';
import {el} from '@angular/platform-browser/testing/src/browser_util';

@Component({
  selector: 'app-server',
  templateUrl: './server.component.html',
  styleUrls: ['./server.component.css']
})
export class ServerComponent implements OnInit {

  serverId = 0;
  status = 'offline';

  constructor() {

    // choose a number between 0 and 100
    this.serverId = Math.floor((Math.random() * 100) + 1);

    const random2 = Math.random();

    if (random2 > 0.5) {
      this.status = 'online';
    } else {
      this.status = 'offline';
    }

  }

  ngOnInit() {
  }

}

app.component.html

<input type="button" value="Add Server" (click)="onButtonClick()"/>
<app-server *ngFor="let s of servers"></app-server>

app.component.ts

import {Component} from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  servers = [];

  onButtonClick() {
    // just push a date to servers
    this.servers.push(new Date());
  }
}

Getting the Index when using ngFor

<app-server *ngFor="let s of servers;let i=index" [ngClass]="{'even':i%2==0}"></app-server>

References
https://github.com/mhdr/AngularSamples/tree/master/011/my-app

Applying CSS Classes Dynamically with ngClass on Angular

app.component.html

<input type="button" value="Start" (click)="onButtonClick()"/>
<div [ngClass]="{name:isClicked}">Hello World 4</div>

app.component.css

.name {
  background-color: aquamarine;
}

app.component.ts

import {Component} from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  isClicked = false;

  onButtonClick() {
    this.isClicked = true;
  }
}

Method 2

<div [class.extra-sparkle]="isDelightful">	

Binds the presence of the CSS class extra-sparkle on the element to the truthiness of the expression isDelightful.

References
https://github.com/mhdr/AngularSamples/tree/master/010/my-app

Styling Elements Dynamically with ngStyle on Angular

app.component.html

<div [ngStyle]="{backgroundColor:'Red'}">Hello World</div>
<div [ngStyle]="{backgroundColor:backgroundColor}">Hello World 2</div>
<div [ngStyle]="{backgroundColor:getBackgroundColor()}">Hello World 3</div>

app.component.ts

import {Component} from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  backgroundColor = 'Blue';

  getBackgroundColor() {
    return 'Green';
  }
}

References
https://github.com/mhdr/AngularSamples/tree/master/010/my-app

Direct Drawing a rectangle on Images with a mouse with OpenCV Python

import cv2
import numpy as np

is_drawing = False
ix = -1
iy = -1

blank_image = np.zeros([512, 512, 3], dtype=np.uint8)


def draw_rectangle(event, x, y, flags, param):
    global is_drawing, ix, iy

    if event == cv2.EVENT_LBUTTONDOWN:
        is_drawing = True
        ix = x
        iy = y
    elif event == cv2.EVENT_LBUTTONUP:
        is_drawing = False
        cv2.rectangle(img=blank_image, pt1=(ix, iy), pt2=(x, y), color=(255, 0, 0), thickness=-1)
        ix = -1
        iy = -1
    elif event == cv2.EVENT_MOUSEMOVE:
        if is_drawing:
            cv2.rectangle(img=blank_image, pt1=(ix, iy), pt2=(x, y), color=(255, 0, 0), thickness=-1)


cv2.namedWindow("image")
cv2.setMouseCallback("image", draw_rectangle)

while True:
    cv2.imshow("image", blank_image)

    # wait 5 seconds then wait for ESC key
    if cv2.waitKey(5) & 0xFF == 27:
        break

cv2.destroyAllWindows()

References
https://github.com/mhdr/OpenCVSamples/tree/master/011

Direct Drawing on Images with a mouse with OpenCV Python

import cv2
import numpy as np

blank_image = np.zeros([512, 512, 3], dtype=np.uint8)


def draw_circle(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        cv2.circle(blank_image, center=(x, y), radius=20, color=(0, 0, 255), thickness=-1)
    elif event == cv2.EVENT_RBUTTONDOWN:
        cv2.circle(blank_image, center=(x, y), radius=20, color=(0, 255, 0), thickness=-1)


cv2.namedWindow("image")
cv2.setMouseCallback("image", draw_circle)

while True:
    cv2.imshow("image", blank_image)

    # wait 5 seconds then wait for ESC key
    if cv2.waitKey(5) & 0xFF == 27:
        break

cv2.destroyAllWindows()

References
https://github.com/mhdr/OpenCVSamples/tree/master/011

Create blank image using OpenCV Python

import cv2
import numpy as np

# black blank image
blank_image = np.zeros(shape=[512, 512, 3], dtype=np.uint8)
# print(blank_image.shape)
cv2.imshow("Black Blank", blank_image)

# white blank image
blank_image2 = 255 * np.ones(shape=[512, 512, 3], dtype=np.uint8)
cv2.imshow("White Blank", blank_image2)

cv2.waitKey(0)
cv2.destroyAllWindows()

References
https://stackoverflow.com/questions/10465747/how-to-create-a-white-image-in-python
https://github.com/mhdr/OpenCVSamples/tree/master/010

Flip image in OpenCV Python

import cv2
import numpy as np

image = cv2.imread("cow.jpg")
cv2.imshow("image", image)

# flip horizontally
flipped1 = cv2.flip(image, 0)
cv2.imshow("image1", flipped1)

# flip vertically
flipped2 = cv2.flip(image, 1)
cv2.imshow("image2", flipped2)

# flip both horizontally and vertically
flipped3 = cv2.flip(image, -1)
cv2.imshow("image3", flipped3)

cv2.waitKey(0)
cv2.destroyAllWindows()

References
https://github.com/mhdr/OpenCVSamples/tree/master/009