Skip to content

Commit

Permalink
Merge pull request ryuKKu-#5 from benoitbzl/master
Browse files Browse the repository at this point in the history
Cancel animation when the directive is destroyed.
  • Loading branch information
ryuKKu- authored Oct 10, 2017
2 parents 5073dc2 + b2d47f3 commit b385a3f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/lib/CanvasManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ export class CanvasManager {
particles.line_linked.color_rgb_line = hexToRgb(particles.line_linked.color);
}

public cancelAnimation(): void {
if (!this._tmpParams.drawAnimFrame) {
return;
}
cancelAnimationFrame(this._tmpParams.drawAnimFrame);
this._tmpParams.drawAnimFrame = null;
}

public draw(): void {
let { particles } = this._params;
Expand Down Expand Up @@ -145,4 +152,4 @@ export class CanvasManager {

this._densityAutoParticles();
}
}
}
13 changes: 10 additions & 3 deletions src/particles.directive.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Directive, ElementRef, AfterViewInit, HostListener, Input } from "@angular/core";
import { Directive, ElementRef, AfterViewInit, HostListener, Input, OnDestroy } from "@angular/core";
import { CanvasManager, ICanvasParams, IParams, ITmpParams, getDefaultParams, isInArray, deepExtend, loadImg } from './lib/index';

@Directive({
selector: '[d-particles]'
})
export class ParticlesDirective implements AfterViewInit {
export class ParticlesDirective implements AfterViewInit, OnDestroy {
@Input() set params(value: IParams) {
let defaultParams: IParams = getDefaultParams();
this._params = deepExtend(defaultParams, value);
Expand All @@ -17,6 +17,13 @@ export class ParticlesDirective implements AfterViewInit {
private _tmpParams: ITmpParams = {};
private _canvasManager: CanvasManager;

ngOnDestroy(): void {
if (!this._canvasManager) {
return;
}
this._canvasManager.cancelAnimation();
}

ngAfterViewInit(): void {
this._canvasParams = {
el: this.el.nativeElement,
Expand Down Expand Up @@ -145,4 +152,4 @@ export class ParticlesDirective implements AfterViewInit {
}
}
}
}
}

0 comments on commit b385a3f

Please sign in to comment.