2
0

added grid

This commit is contained in:
2018-05-15 01:40:06 +02:00
parent 3fb4bc505a
commit 0a5f9806f2
3 changed files with 38 additions and 11 deletions

View File

@@ -27,7 +27,8 @@ export class AppComponent {
}, },
stroke: { stroke: {
width: 2 width: 2
} },
showGrid: true
}; };
} }

View File

@@ -31,8 +31,6 @@ export class CanvasDirective implements OnInit, OnChanges {
private onResize(event) { private onResize(event) {
this.updateConfig(); this.updateConfig();
this.initSvg(); this.initSvg();
this.resetPoints();
this.resetLines();
this.render(); this.render();
} }
@@ -78,6 +76,9 @@ export class CanvasDirective implements OnInit, OnChanges {
private render() { private render() {
this.drawPoints(this.getExpandedPoints); this.drawPoints(this.getExpandedPoints);
this.drawLine(this.getExpandedPoints); this.drawLine(this.getExpandedPoints);
if (this.param.showGrid) {
this.renderGrid(this.getExpandedPoints);
}
} }
private resetPoints(): void { private resetPoints(): void {
@@ -86,7 +87,7 @@ export class CanvasDirective implements OnInit, OnChanges {
private drawPoints(points: Point[]) { private drawPoints(points: Point[]) {
const that = this; const that = this;
const group = this.svg.append('g'); const group = this.svg.append('g').attr('class', 'points');
points.forEach(point => { points.forEach(point => {
group.append('circle') group.append('circle')
@@ -96,6 +97,7 @@ export class CanvasDirective implements OnInit, OnChanges {
.attr('fill', point.color ? point.color : 'lightgray') .attr('fill', point.color ? point.color : 'lightgray')
.attr('stroke-width', !point.color ? 2 : 0) .attr('stroke-width', !point.color ? 2 : 0)
.attr('stroke', !point.color ? 'gray' : '') .attr('stroke', !point.color ? 'gray' : '')
.style('cursor', 'pointer')
.call(Drag.drag() .call(Drag.drag()
.on('drag', function() { .on('drag', function() {
Selection.select(this) Selection.select(this)
@@ -103,10 +105,7 @@ export class CanvasDirective implements OnInit, OnChanges {
.attr('cy', Selection.event.y); .attr('cy', Selection.event.y);
}) })
.on('end', () => { .on('end', () => {
// this.render();
this.drawLine(this.getExpandedPoints); this.drawLine(this.getExpandedPoints);
console.log('end');
delete this.drag;
})); }));
}); });
} }
@@ -156,20 +155,20 @@ export class CanvasDirective implements OnInit, OnChanges {
} }
private get getExpandedPoints() { private get getExpandedPoints() {
const circles = Selection.selectAll('circle'); const group = Selection.select('g.points');
const points = []; const points = [];
const that = this; const that = this;
let point = null; let point = null;
if (!circles.size()) { if (!group.size()) {
return this.expandPoints([ return this.expandPoints([
this.config.start, this.config.start,
this.config.end this.config.end
]); ]);
} }
circles.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'),
@@ -181,7 +180,7 @@ export class CanvasDirective implements OnInit, OnChanges {
return points; return points;
} }
private calcDelta(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);
} }
@@ -192,6 +191,32 @@ export class CanvasDirective implements OnInit, OnChanges {
}; };
} }
private renderGrid(points: Point[]) {
const startingPoints = [points.shift(), points.pop()];
// const startingPoints = [...points];
let group: any;
if (!Selection.select('g.grid').size()) {
group = this.svg.append('g').attr('class', 'grid');
} else {
group = Selection.select('g.grid');
}
points.forEach(p => {
startingPoints.forEach(s => {
group.append('circle')
.attr('cx', s.x)
.attr('cy', s.y)
.attr('r', this.Δ(p, s))
.attr('stroke-width', 1)
.attr('fill-opacity', 0)
.attr('stroke', 'gainsboro');
});
});
group.lower();
}
private updateConfig(): void { private updateConfig(): void {
const margin = this.canvas.clientWidth * this.param.margin.x; const margin = this.canvas.clientWidth * this.param.margin.x;

View File

@@ -11,4 +11,5 @@ export interface Param {
stroke?: { stroke?: {
width: number; width: number;
}; };
showGrid?: boolean;
} }