image source
Repository: Ionic Github Repository
Software Requirements:
Visual Studio Code(Or any preferred code editor)
What you will learn:
In this tutorial you would learn to how to
- Build better animated effects in you ionic application
- Understand and use the
domWrite
in you application.
Difficulty: Intermediate
Tutorial
In this tutorial, i would be trying to help you understand what causes a lag in ionic applications when you try anything involving an animated effect.
In Ionic 3, a new function was introduced which is called domWrite
. When modifying components live in ionic(In other terms, making animations). You are changing the root elements within your webview and on a device which does not provide much graphic memory for the webview of an application, it is very common to see a laggy effect in your application.
In the last expandable header bar i build, i was dissatisfied with the output on my device because it was extremely laggy and was making my GUI very scrappy. I did some research and found out the reasons for this and found ways to reduce, if not eliminate the lag depending on what device you are running your application on.
So firstly i would try to explain why hybrid applications lag
Reason
A hybrid application will lag whenever you try to recolor(change the color or opacity) of an element and will lag even more when you try to reflow(rescale a component). Ionic found a partial remedy to this which was the domWrite
which optimizes how this reflow or recoloring happens and makes the gui flow smoothly. Regardless of this, no matter how much it tries, this will still lag on devices with little rams and processing speed that dont give a lot of graphic memory to the webview in applications.
For example
This is the code for my laggy header bar.
Follow through the code to see the areas i commented on
import { Component, Input, ElementRef, NgZone, Renderer2 } from '@angular/core';
@Component({
selector: 'expandable',
templateUrl: 'expandable.html'
})
export class ExpandableComponent {
@Input('scrollArea') scrollArea: any;//This references to the ion content within whatever component you choose to use this custom component in
@Input('headerHeight') headerHeight: number;//This sets the height you would choose to use
newHeaderHeight: any;//This is a variable for the new header height you would like to give whenever there is a scroll event
constructor(public element: ElementRef, public renderer: Renderer2, public zone: NgZone) {
}
ngOnInit(){
this.renderer.setStyle(this.element.nativeElement, 'height', this.headerHeight + 'px');//This sets the header height at the startup. We aren't using domWrite because this is when the application loads
this.scrollArea.ionScroll.subscribe((ev) => {
this.resizeHeader(ev);
this.scrollArea.resize();
});
}
resizeHeader(ev){
ev.domWrite(() => {
this.newHeaderHeight = this.headerHeight - ev.scrollTop;//Whenever there is a scroll event, the header height changes.
if(this.newHeaderHeight < 0){
this.newHeaderHeight = 0;
}
this.renderer.setStyle(this.element.nativeElement, 'height', this.newHeaderHeight + 'px');
});
}
}
Why does this lag? and how can we change it
The reason for the lag can be summarized in this line of code
this.newHeaderHeight = this.headerHeight - ev.scrollTop;
Scroll events give of responses on the slightest motion within the scroll area. Meaning that a simple flick may give up to 10 or more scroll event. So talk more of a scroll event that goes in opposite areas rapidly. For a browser on a laptop. This would be smooth because the ram can take way more and even on devices with rams more than 4gigg and nice processors we will not see a lag but what of other devices. The browser simply cant compute this fast enough and render the template in enough time.
How to fix it
As much as possible create less logic involving your view. In other words look for landmarks, look for events that are triggered at certain points and use if
block of code to change the view whenever these events are triggered. Below is a fix to this header bar.
import { Component, Input, ElementRef, NgZone, Renderer2 } from '@angular/core';
@Component({
selector: 'expandable',
templateUrl: 'expandable.html'
})
export class ExpandableComponent {
@Input('scrollArea') scrollArea: any;
@Input('headerHeight') headerHeight: number;
newHeaderHeight: any;
constructor(public element: ElementRef, public renderer: Renderer2, public zone: NgZone) {
}
ngOnInit(){
this.renderer.setStyle(this.element.nativeElement, 'height', this.headerHeight + 'px');
this.scrollArea.ionScroll.subscribe((ev) => {
this.resizeHeader(ev);
this.scrollArea.resize();
});
}
resizeHeader(ev){
ev.domWrite(() => {
// this.newHeaderHeight = this.headerHeight - ev.scrollTop;
if(ev.deltaY > 0){//Use Delta events rather than scroll events to detect the direction of scroll
this.newHeaderHeight -= 20;
}
if(ev.deltaY > 20){
this.newHeaderHeight = 0
}
if(ev.deltaY < 0){
this.newHeaderHeight = (this.headerHeight/2);
}
if(ev.deltaY < -20){
this.newHeaderHeight = this.headerHeight;
}
if(this.newHeaderHeight < 0){
this.newHeaderHeight = 0;
}
this.renderer.setStyle(this.element.nativeElement, 'height', this.newHeaderHeight + 'px');
});
}
}
In this fix, we are using deltaY
which is an event that simply detects changes in the direction of the scroll event and not the exact position which the page is being scrolled to. This in turn would make the bar retract regardless of where the scroll position is but you could still use a combination of both the scroll position and the direction of change if you're a bit more ambitious like this
if(deltaY > 0 && scrollTop > 180){
this.newHeaderheight = 0
}
This is a gif but show how the header is actually working in realtime
This would create a way better animated effect without any lag.
If you're lost on how i created this animated header. Here is a detailed seperate tutorial by joshMorony. Which should get you back on track.
You could see the code for this in my Github Page