2
0

fix(canvasDirective): tidying up and removing unneccessary code

This commit is contained in:
2018-05-19 20:52:55 +02:00
parent cef5274b85
commit 6c09fb512d
2 changed files with 22 additions and 47 deletions

View File

@@ -1,5 +1,4 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
// import { FormsModule } from '@angular/forms';
import { Param } from './models/param.model'; import { Param } from './models/param.model';
import { Config } from './models/config.model'; import { Config } from './models/config.model';
@@ -26,29 +25,12 @@ export class AppComponent {
y: 0.4 y: 0.4
}, },
stroke: { stroke: {
width: 2 width: 0.2
}, },
spread: 80, spread: 80,
showGrid: true showGrid: false
}; };
}
/**
* A test of canvasParam.margin change and binding to canvas.directive
*/
let x = 1;
let intrvlId = setInterval(() => {
if (x < 5) {
this.canvasParam.margin.x += 0.2;
x += 1;
console.log("canvas param ", this.canvasParam.margin.x);
} else {
clearInterval(intrvlId);
}
}, 1000);
/**
*
*/
};
public updateCanvasConfig(config): void { public updateCanvasConfig(config): void {
this.canvasConfig = config; this.canvasConfig = config;

View File

@@ -1,4 +1,4 @@
import { ElementRef, HostListener, Output, EventEmitter, OnInit, Input, OnChanges, SimpleChanges, Directive } from '@angular/core'; import { ElementRef, HostListener, Output, EventEmitter, OnInit, Input, Directive, DoCheck } from '@angular/core';
import * as Selection from 'd3-selection'; import * as Selection from 'd3-selection';
import * as Shape from 'd3-shape'; import * as Shape from 'd3-shape';
import * as Random from 'd3-random'; import * as Random from 'd3-random';
@@ -11,7 +11,7 @@ import { Param } from './../models/param.model';
@Directive({ @Directive({
selector: '[appCanvas]' selector: '[appCanvas]'
}) })
export class CanvasDirective implements OnInit, OnChanges { export class CanvasDirective implements OnInit, DoCheck {
private canvas: any; private canvas: any;
private defs: any; private defs: any;
private gradient: any; private gradient: any;
@@ -24,7 +24,6 @@ export class CanvasDirective implements OnInit, OnChanges {
@Input() @Input()
public param: Param; public param: Param;
public marginX: Param['margin']['x']; public marginX: Param['margin']['x'];
@Output() @Output()
private emitConfig: EventEmitter<Config>; private emitConfig: EventEmitter<Config>;
@@ -33,7 +32,7 @@ export class CanvasDirective implements OnInit, OnChanges {
private onResize(event) { private onResize(event) {
this.resetLines(); this.resetLines();
this.resetPoints(); this.resetPoints();
this.init(); this.init();
} }
constructor( constructor(
@@ -43,18 +42,14 @@ export class CanvasDirective implements OnInit, OnChanges {
this.emitConfig = new EventEmitter(); this.emitConfig = new EventEmitter();
} }
ngOnChanges(changes: SimpleChanges) { ngDoCheck() {
console.log("ch ch ch chaaaangesss ",changes.param);
}
ngDoCheck(){
this.paramAdjustment(); this.paramAdjustment();
} }
paramAdjustment(){ paramAdjustment() {
this.resetLines(); this.resetLines();
this.resetPoints(); this.resetPoints();
this.init(); this.init();
} }
ngOnInit() { ngOnInit() {
@@ -94,7 +89,8 @@ export class CanvasDirective implements OnInit, OnChanges {
} }
private resetPoints(): void { private resetPoints(): void {
Selection.selectAll('circle').remove(); Selection.selectAll('g#spread-points').remove();
Selection.selectAll('g.points').remove();
} }
private drawPoints(points: Point[]) { private drawPoints(points: Point[]) {
@@ -125,7 +121,7 @@ export class CanvasDirective implements OnInit, OnChanges {
private expandPoints(points: Point[]) { private expandPoints(points: Point[]) {
const newPoints: Point[] = []; const newPoints: Point[] = [];
const matrix = { const matrix = {
min: { min: {
x: points.reduce((a, b) => a.x < b.x ? a : b).x, x: points.reduce((a, b) => a.x < b.x ? a : b).x,
@@ -135,8 +131,8 @@ export class CanvasDirective implements OnInit, OnChanges {
x: points.reduce((a, b) => a.x > b.x ? a : b).x, x: points.reduce((a, b) => a.x > b.x ? a : b).x,
y: points.reduce((a, b) => a.y > b.y ? a : b).y, y: points.reduce((a, b) => a.y > b.y ? a : b).y,
} }
} };
points.splice(1, 0, { points.splice(1, 0, {
x: this.config.end.x, x: this.config.end.x,
y: this.config.height - this.config.height * this.param.margin.y y: this.config.height - this.config.height * this.param.margin.y
@@ -154,14 +150,13 @@ export class CanvasDirective implements OnInit, OnChanges {
} }
private get getExpandedPoints() { private get getExpandedPoints() {
const group = Selection.select('g.points'); const group = Selection.select('g.points');
const points = []; const points = [];
const that = this; const that = this;
let point = null; let point = null;
if (group.size()<=1) { if (group.size() <= 1) {
return this.expandPoints([ return this.expandPoints([
this.config.start, this.config.start,
this.config.end this.config.end
@@ -170,7 +165,7 @@ export class CanvasDirective implements OnInit, OnChanges {
group.selectAll('circle').each(function() { group.selectAll('circle').each(function() {
point = Selection.select(this); point = Selection.select(this);
points.push({ points.push({
x: point.attr('cx'), x: point.attr('cx'),
y: point.attr('cy'), y: point.attr('cy'),
@@ -180,9 +175,8 @@ export class CanvasDirective implements OnInit, OnChanges {
return points; return points;
} }
private generateRandomPoint(matrix) { private generateRandomPoint(matrix) {
return {
return {
x: Random.randomUniform(matrix.min.x, matrix.max.x)(), x: Random.randomUniform(matrix.min.x, matrix.max.x)(),
y: Random.randomUniform(matrix.min.y, matrix.max.y)() y: Random.randomUniform(matrix.min.y, matrix.max.y)()
}; };
@@ -264,7 +258,6 @@ export class CanvasDirective implements OnInit, OnChanges {
* @param b * @param b
*/ */
private Δ(a: Point, b: Point) { private Δ(a: Point, b: Point) {
return Math.pow(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2), 0.5); return Math.pow(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2), 0.5);
} }
@@ -302,8 +295,8 @@ export class CanvasDirective implements OnInit, OnChanges {
* Update Config Parameters and emit to parent component. * Update Config Parameters and emit to parent component.
*/ */
private updateConfig(): void { private updateConfig(): void {
const margin = this.canvas.clientWidth * this.param.margin.x; const margin = this.canvas.clientWidth * this.param.margin.x;
this.config = { this.config = {
width: this.canvas.clientWidth, width: this.canvas.clientWidth,
height: this.canvas.clientHeight, height: this.canvas.clientHeight,
@@ -319,7 +312,7 @@ export class CanvasDirective implements OnInit, OnChanges {
}, },
drag: this.drag drag: this.drag
}; };
// Emit Canvas Config to parent Component. // Emit Canvas Config to parent Component.
this.emitConfig.next(this.config); this.emitConfig.next(this.config);
} }