Saturday, September 16, 2023

Cropping Images in JavaScript | Crop an Image in JavaScript With HTML Canvas

Digital images are composed of a two dimensional array as well as grid of vertical and horizontal pixels, small color squares each called pixel. Image cropping is a way of image editing that involves selecting a portion of an image, hence reducing the number of pixels and changing the aspect ratio as well as size.
A canvas is a html element which is a white region in which you can display or draw graphical elements in modern web browsers. A common way to crop an image in JavaScript is with the HTML5 <canvas> element and then transform the images by calling the drawImage() function respectively.
Step 1: Create a Canvas in HTML

Create a <canvas> element in your HTML document using below code:
<canvas id="mycanvas" width="800px" height="400px"></canvas>
Step 2: Create a JavaScript File and a Crop Function

Define a function named cropImage(), which you can call from within your code when an image loaded from some source or from base64 string.

Add the onload() function to the Image object so that cropImage() runs only after an image is fully loaded on the viewer’s browser.

onload() gets the <canvas> element from your HTML and then will prepare a 2D context object for the canvas.
const canvas = document.getElementById('mycanvas');
const context = canvas.getContext('2d');
Step 3: Load the Image

Create an image object from any source you want and load an image with the src property from a local drive or the internet or base64 string as per your requirement.
var image = new Image();
image.src = "https://static.image.com/img.jpg";
Step 4: Call the drawImage() Function

Here’s the syntax of the drawImage() function when Image object loaded:

drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight);

Crop operations require all nine parameters described below:
  • image, sx, sy — The image object, the x-coordinate, and the y-coordinate from which to start cropping the image.
  • sWidth, sHeight — The width of the cropped version, starting from sx and sy.
  • dx, dy — The point from which to start drawing the cropped version on the canvas.
  • dWidth, dHeight — The width and height of the cropped version to be displayed.
Here is how to call the drawImage() function along with the image and context you loaded previously as per our requirement:
image.onload = function() {
  context.drawImage(image, 100, 100, 200, 200, 50,50, 200, 200);
}

drawImage() performs two steps for further process:

  1. Crop a 200×200-pixel square that starts from the coordinate 100:100 pixels on the original image.
  2. Display the cropped version in the canvas with a top and left margin of 50 pixels.
Below is another solid example of how we can crop image:
const image = new Image();
image.crossOrigin='anonymous';
image.onload = () => {
    const canvas: any = document.createElement('canvas');
    const ctx: any = canvas.getContext('2d');
    canvas.height = image.naturalHeight - 25;
    // above line will crop out 25px from bottom 
    canvas.width = image.naturalWidth;
    ctx.drawImage(image, 0, 0);
    const dataUrl: any = canvas.toDataURL();
    // dataUrl is base64 encoded value of cropped image
};
image.onerror = function() {
    alert("Failed to process image");
};
image.src = "https://static.image.com/hello.png";

Thursday, September 7, 2023

Lazy Loading in Angular | Lazy-loading feature modules | Chunk Loading of Components

NgModules are eagerly loaded by default, which means that as soon as the application loads, so do all of the NgModules, whether or not they are immediately required.

Consider lazy loading in angular — a design strategy that loads NgModules as needed — for big apps with many routes. Lazy loading helps to keep initial bundle sizes smaller, which reduces load times.
As Angular generates a SPA (Single Page Application), all of its components are loaded at the same time. This implies that a large number of unneeded libraries or modules may also be loaded.

Lazy loading in angular is the process of loading website components, modules, or other assets when they are needed.
You can utilize lazy loading (or asynchronous loading) with the router if you're constructing your application and utilising feature modules to arrange code. This allows a whole module to load only when needed, reducing the file size of the core bundles and maybe limiting access to bundles to just those who are permitted to use it (like administrative modules).

Because there is no logical isolation if your application has not been split into various modules, lazily loading them into the application is not feasible. The core notion is that the Angular build process can examine code pathways and optimize code depending on how it's used to produce other files, but it relies heavily on Angular modules to know how code is connected.
Steps to Implement Lazy Loading

You need to create your first angular project, you can get help from here how-to-create-multi-layout-application.html

Create a directory under app directory named routes, then create a file under that directory lazy-users-routing.module.ts with below content:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

const routes: Routes = [];

@NgModule({
    imports: [RouterModule.forChild(routes)],
    exports: [RouterModule],
})
export class LazyUsersRoutingModule {}


Now create another file in same directory named lazy-user.module.ts with below content:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

@NgModule({
    declarations: [ ],
    imports: [
        CommonModule
    ]
})
export class LazyUserModule { }


I assume you have already several component, if not you can create components by your own. Now lets check how we implement Lazy Loading with loadChildren.
Our main routing file app-routing.module.ts will be look like below:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import {HomeComponent} from "./modules/home/home.component";
import {DefaultLayoutComponent} from "./layouts/default/default.component";
import {LoginComponent} from "./modules/login/login.component";
import {StaticLayoutComponent} from "./layouts/static/static.component";

const routes: Routes = [
    {
        path: '',
        component: DefaultLayoutComponent,
        children: [
            {
                path: '',
                component: HomeComponent,
            }, {
                path: 'users',
                //component: UsersComponent,
                loadChildren: () => import('./routes/lazy-user.module').then(m => m.LazyUserModule)
            }
        ]
    }, {
        path: '',
        component: StaticLayoutComponent,
        children: [
            {
                path: 'login',
                component: LoginComponent,
            }
        ]
    },
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }


This is the main part of app-routing.module.ts file where we enabled lazy loading of our components:
{
    path: 'users',
    //component: UsersComponent,
    loadChildren: () => import('./routes/lazy-user.module').then(m => m.LazyUserModule)
}


Now we will define UsersComponent in LazyUsersRoutingModule like below:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import {UsersComponent} from "../modules/users/users.component";

// This route is prefixed by /users
const routes: Routes = [
    {
        path: '', component: UsersComponent
    }
];

@NgModule({
    imports: [RouterModule.forChild(routes)],
    exports: [RouterModule],
})
export class LazyUsersRoutingModule {}


Our final lazy-user.module.ts is as below:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import {UsersComponent} from "../modules/users/users.component";
import {LazyUsersRoutingModule} from "./lazy-users-routing.module";

@NgModule({
    declarations: [
        UsersComponent,
    ],
    imports: [
        CommonModule,
        LazyUsersRoutingModule,
    ]
})
export class LazyUserModule { }


We have enabled lazy loading of component, if we check our network tab we have evidence of that:


GitHub link for this example is https://github.com/pritomkucse/angular-layout/tree/lazy-loading

Monday, September 4, 2023

How to Create Multi Layout Application with Angular | Angular - Reusable Layout Templates Using Angular Routing | Angular material layout

Angular material provides a layout to create our component inside it; Layout is basically a directive which helps to show or represent the layout for its children. We basically have two types of layouts: flex and grid; angular material provides us inbuild module and directive to create this layout in our application. We all know about the grid layout, and it is very easy to create as well, by the use of a few configurations. We also have few properties that are used to assign the Layout values, and it is mainly as ‘row’ and ‘column’; we can set this parameter some value. Also, we can make the layout responsive by using the Layout property which it is provided. In the coming section of the tutorial, we will have a closer look at the implementation and what changes need to be made in order to run our application properly with detailed examples for beginners.
First, install the Angular CLI, which enables us to download the required packages and library for our project. You can download it by typing the below command on your command make sure you have already installed node see below;
Run command: npm install -g @angular/cli
The above command will install the CLI globally in our system; hence we can use it globally when required.

Now, in this step, we will try to create the new angular project from scratch using routing module using below command

ng new ang1 --routing --defaults

Navigate to project directory then just to make sure, try one command which is mentioned below to install all the required library into our project
npm install

Will will run below command to run our first angular project:
ng serve

Out first project is running:
We will continue our second part where we will manage layouts for different types of page, suppose login page will be a clean page where home page page as well as other pages will have left menu enabled. So we will create first layouts here. 1. Default Layout and 2. Static Layout

To do so we will run below commands to create layouts, these 2 commands with create two layouts default and static:
ng g c layouts/default
ng g m layouts/default
ng g c layouts/static
ng g m layouts/static

Open layouts/static.module.ts and replace StaticModule with StaticLayoutModule, do same for layouts/default.module.ts, replace DefaultModule to DefaultLayoutModule

Open layouts/static.component.ts and replace StaticComponent with StaticLayoutComponent, do same for layouts/default.component.ts, replace DefaultComponent to DefaultLayoutComponent

We will create our componets home and login using below command:
ng g c modules/home
ng g c modules/login
We will keep app.module.ts as much as simple like below, there will be references to Layouts only rather than components:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import {DefaultLayoutModule} from "./layouts/default/default.module";
import {StaticLayoutModule} from "./layouts/static/static.module";

@NgModule({
    declarations: [
        AppComponent,
    ],
    imports: [
        BrowserModule,
        AppRoutingModule,

        DefaultLayoutModule,
        StaticLayoutModule,
    ],
    providers: [],
    bootstrap: [AppComponent]
})
export class AppModule { }

Contents of app/app.component.html:
<router-outlet></router-outlet>

Contents of app-routing.module.ts:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import {HomeComponent} from "./modules/home/home.component";
import {DefaultLayoutComponent} from "./layouts/default/default.component";
import {LoginComponent} from "./modules/login/login.component";
import {StaticLayoutComponent} from "./layouts/static/static.component";

const routes: Routes = [
    {
        path: '',
        component: DefaultLayoutComponent,
        children: [
            {
                path: '',
                component: HomeComponent,
            }
        ]
    }, {
        path: '',
        component: StaticLayoutComponent,
        children: [
            {
                path: 'login',
                component: LoginComponent,
            }
        ]
    },
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

We will create shared component header and footer so that we can use them from any of your component, use below command to create shared components:
ng g c shared/header
ng g c shared/footer

Shared components are created, now we will create shared module using below command:
ng g m shared/

We will modify our shared module as below (app/shared/shared.module.ts):
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import {HeaderComponent} from "./header/header.component";
import {FooterComponent} from "./footer/footer.component";
import {RouterModule} from "@angular/router";

@NgModule({
    declarations: [
        HeaderComponent,
        FooterComponent,
    ],
    imports: [
        CommonModule,
        RouterModule,
    ],
    exports: [
        HeaderComponent,
        FooterComponent,
    ]
})
export class SharedModule { }

Contents of app/shared/header/header.component.html:
<header>
    <div>
        <a routerLink="/">Home</a>
        <a routerLink="/login">Login</a>
    </div>
</header>

Contents of app/shared/footer/footer.component.html:
<footer>
    <p>footer works!</p>
</footer>
Lets open app/layouts/default/default.module.ts and keep as below where we import our components (for example home component):
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import {HomeComponent} from "../../modules/home/home.component";
import {BrowserModule} from "@angular/platform-browser";
import {FormsModule} from "@angular/forms";
import {RouterModule} from "@angular/router";
import {SharedModule} from "../../shared/shared.module";
import {DefaultLayoutComponent} from "./default.component";

@NgModule({
    declarations: [
        DefaultLayoutComponent,
        HomeComponent, //register home component will use this layout
    ],
    imports: [
        FormsModule,
        CommonModule,
        BrowserModule,
        RouterModule,
        SharedModule,//we will access header and footer via shared module
    ]
})
export class DefaultLayoutModule { }

Contents of default.component.html:
<div class="container-fluid">
    <div class="container-fluid">
        <header class="row">
            <app-header></app-header>
        </header>
        <div id="main" class="row">
            <router-outlet></router-outlet>
        </div>
        <footer class="row">
            <app-footer></app-footer>
        </footer>
    </div>
</div>


Lets open app/layouts/static/static.module.ts and keep as below where we import our components (for example login component):
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import {StaticLayoutComponent} from "./static.component";
import {LoginComponent} from "../../modules/login/login.component";
import {FormsModule} from "@angular/forms";
import {BrowserModule} from "@angular/platform-browser";
import {RouterModule} from "@angular/router";
import {SharedModule} from "../../shared/shared.module";

@NgModule({
    declarations: [
        StaticLayoutComponent,
        LoginComponent, //register login component will use this layout
    ],
    imports: [
        FormsModule,
        CommonModule,
        BrowserModule,
        RouterModule,
        SharedModule,//we will access header and footer via shared module
    ]
})
export class StaticLayoutModule { }

Contents of static.component.html where we would not include footer and that the way we can differ two layouts default and static:
<div class="container-fluid">
    <div class="container-fluid">
        <header class="row">
            <app-header></app-header>
        </header>
        <div id="main" class="row">
            <router-outlet></router-outlet>
        </div>
    </div>
</div>
Lets check what we did so far, run command ng serve to start application, below is screenshot of both home and login page where we can clearly seen two different layouts are using.

Home page using default layout where both header and footer available


Login page using static layout where only header available
This is github project link https://github.com/pritomkucse/angular-layout

Monday, August 28, 2023

Grails / GORM apply criteria query with hasmany String for filter

I have a domain object (User) like this:
class User {
   String name

   static hasMany = [
      permissions: String
   ]
}
And I am trying to query all the User with certain permissions. So I have to join and check permissions property. Here how we can do this -
User.withCriteria {
    createAlias('permissions', 'n') 
    ilike 'n.elements', '%permission_1%'
}
The trick is to use 'n.elements'

Friday, July 28, 2023

Bootstrap 5 Multiple Modal Overlay | How to use multiple modal in bootstrap?

Let me explain my problem, I am using bootstrap modal for my project. That is fine but I get stucked when another modal open over first modal. To solve this problem I added below javascript code to my project and it get solved.
$(document).on('show.bs.modal', '.modal', function() {
    const zIndex = 1040 + 10 * $('.modal:visible').length;
    $(this).css('z-index', zIndex);
    setTimeout(() => $('.modal-backdrop').not('.modal-stack').css('z-index', zIndex - 1).addClass('modal-stack'));
});
Now my modals are looking like below:

Friday, March 24, 2023

Foods That Will Not Affet Diabetics

Diabetics should avoid fruits with a high GI or eat them in moderation so that their blood sugar levels do not spike abruptly. Pineapple, watermelon, mango, lychee, and banana have a high GI, so these are the worst fruits if you are diabetic.

Below foods are good to continue even you have diabetic

1. Oranges
2. Chickpeas (Garbanzo Beans + Chola Chana) or Chickpea flour as well
3. Green Vegetables
4. Nuts and Peanut Butter
5. Probiotics
6. Cinnamon
7. It is normally recommended to consume 6-8 ALMONDS a day but for a diabetic patient, the quantity should be more
8. Cashew nuts (Kaju Badam) contain high levels of beneficial fats, the consumption of which can raise the good cholesterol and reduce the bad cholesterol levels, and thereby reduce the risk of heart disease. Undoubtedly, they are one of the best nuts for diabetes.
9. Green Apples, Grapes are not alarming to diabetics.
10. Custard apple (Sitaphal or Sharifa) has a low glycemic index of 54 that makes it a good choice for diabetes. Custard apple is good for digestion and boosts immunity for a diabetic. It aids in better vision and prevents high blood pressure or hypertension. It is recommended that a diabetic consumes 1 full custard apple every alternate day. read more here

Some additions:

Ginger is incredibly versatile and a staple in alternative medicine. People have used it for centuries to improve many aspects of heart health, including circulation, cholesterol levels, and blood pressure. Both human and animal studies have shown that taking ginger reduces blood pressure in several ways.
Coconut is excellent for diabetics as it helps regulate blood sugar levels as it has a lot of fiber. It has a low carbohydrate content but a high fiber and fat content instead.

Thursday, March 16, 2023

How to Disable the Built-In Laptop Keyboard in Windows

Disable your Windows laptop's keyboard and migrate to a bigger one with these tips.
Sometimes you don't want your laptop's keyboard to take inputs. This is usually because you're plugging in an external keyboard, either because the built-in one is broken or you just want a larger typing space with a full-sized keyboard.
However, the keyboard being an integral part of your portable computer, disabling its primary input method is a little tricky. Here, we show you how to permanently disable the laptop keyboard in Windows.
For this, you’ll need to identify the integrated keyboard in Device Manager. Since Device Manager will list all the recognized keyboards, including external keyboards, here’s how you can identify your laptop keyboard from the list.
1. Press Win + R to open Run.

2. Type devmgmt.msc and click OK to open Device Manager.

3. In Device Manager, expand the Keyboards section.

4. From this step you will get the keyboard driver id as red marked in picture.

5. Now Press the Win key and type cmd in the Windows search bar.

6. Right-click on Command Prompt and select Run as Administrator. Click Yes when the UAC prompt appears.

7. In the Command Prompt window, type the following command and press Enter:

sc config i8042prt start= disabled

8. When the success message appears, close the Command Prompt, and restart your PC. After the restart, your laptop keyboard will stop registering any inputs.

9. If you change your mind and want to re-enable the keyboard, you can use the following command

sc config i8042prt start= auto