2
0

fundamental animations working

This commit is contained in:
2018-08-08 19:52:42 +02:00
parent 7db484b55e
commit d8714d0606
6 changed files with 80 additions and 53 deletions

15
.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,15 @@
{
// Verwendet IntelliSense zum Ermitteln möglicher Attribute.
// Zeigen Sie auf vorhandene Attribute, um die zugehörigen Beschreibungen anzuzeigen.
// Weitere Informationen finden Sie unter https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Chrome gegen localhost starten",
"url": "http://localhost:4200",
"webRoot": "${workspaceFolder}"
}
]
}

View File

@@ -14,7 +14,7 @@
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/ */
import { ViewChild, Component, Input, Output, SimpleChanges, OnChanges, EventEmitter, OnInit } from '@angular/core'; import { ViewChild, Component, Input, Output, SimpleChanges, OnChanges, EventEmitter, OnInit, HostListener } from '@angular/core';
import { Observable, interval, Subscription } from 'rxjs'; import { Observable, interval, Subscription } from 'rxjs';
import * as Selection from 'd3-selection'; import * as Selection from 'd3-selection';
import * as Shape from 'd3-shape'; import * as Shape from 'd3-shape';
@@ -40,13 +40,15 @@ export class GraphsComponent implements OnChanges, OnInit {
public canvas: any | null; public canvas: any | null;
public matrix: any | null; public matrix: any | null;
public graphs: Graph[]; public graphs: Graph[];
public windowHeight: number | null;
public windowWidth: number | null;
private genShiftPoint: any | null; private genShiftPoint: any | null;
private genLoadedAllGraphs: any | null; private genLoadedAllGraphs: any | null;
private hash: string; private hash: string;
private animation: Observable<Graph[]>; private animation: Observable<Graph[]>;
private timer: Observable<number>; private timer: Observable<number>;
private animationSteps: Subscription; private animationSteps: any;
@Input() config: any; @Input() config: any;
@Input() restoredHistory: any; @Input() restoredHistory: any;
@@ -55,6 +57,11 @@ export class GraphsComponent implements OnChanges, OnInit {
@Output() graphChange = new EventEmitter(); @Output() graphChange = new EventEmitter();
@ViewChild('svg') svgElementRef; @ViewChild('svg') svgElementRef;
@HostListener('window:resize', ['$event'])
onResize(event) {
this.canvasService.adjustToWindow();
}
constructor( constructor(
private canvasService: CanvasService, private canvasService: CanvasService,
private historyService: HistoryService, private historyService: HistoryService,
@@ -74,6 +81,7 @@ export class GraphsComponent implements OnChanges, OnInit {
this.updateMatrix(); this.updateMatrix();
if (changes.config && !changes.config.firstChange) { if (changes.config && !changes.config.firstChange) {
// Config changes must not trigger any other events
this.updateGraphs(); this.updateGraphs();
return; return;
} }
@@ -85,22 +93,20 @@ export class GraphsComponent implements OnChanges, OnInit {
if (changes.animationActive) { if (changes.animationActive) {
if (this.animationActive) { if (this.animationActive) {
this.animationSteps = this.timer.subscribe(n => { this.animationSteps = setInterval(() => this.animateGraph(), 50);
console.log('Animation step', n);
this.graphs = this.animationService.animate(this.graphs);
// this.graphs = this.graphs;
this.hash = this.historyService.hash(this.graphs);
this.saveHistory();
});
} else { } else {
if (this.animationSteps) { if (this.animationSteps) {
this.animationSteps.unsubscribe(); clearInterval(this.animationSteps);
} }
} }
} }
} }
private animateGraph() {
this.graphs = this.animationService.animate(this.graphs);
}
private saveHistory() { private saveHistory() {
this.hash = this.historyService.hash(this.graphs);
this.historyService.save(this.graphs, this.config); this.historyService.save(this.graphs, this.config);
} }
@@ -154,6 +160,7 @@ export class GraphsComponent implements OnChanges, OnInit {
private updateCanvas(): void { private updateCanvas(): void {
this.canvas = this.svgElementRef.nativeElement; this.canvas = this.svgElementRef.nativeElement;
this.canvasService.set(this.canvas); this.canvasService.set(this.canvas);
this.canvasService.adjustToWindow();
} }
private updateMatrix() { private updateMatrix() {

View File

@@ -14,7 +14,7 @@
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/ */
import { ElementRef, HostListener, Output, EventEmitter, Input, Directive, OnInit, OnChanges, SimpleChanges } from '@angular/core'; import { ElementRef, HostListener, Output, EventEmitter, Input, Directive, OnChanges, SimpleChanges } 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';
@@ -52,6 +52,8 @@ export class GuillocheDirective implements OnChanges {
} }
ngOnChanges(changes: SimpleChanges) { ngOnChanges(changes: SimpleChanges) {
// console.log(this.graph);
const points = [ const points = [
this.graph.start.point, this.graph.start.point,
...this.graph.nodes, ...this.graph.nodes,

View File

@@ -19,6 +19,8 @@ import { interval, Observable } from 'rxjs';
import * as Selection from 'd3-selection'; import * as Selection from 'd3-selection';
import { Graph } from '../models/graph.model'; import { Graph } from '../models/graph.model';
import { ArithmeticService } from './arithmetic.service';
import { HistoryService } from './history.service';
@Injectable() @Injectable()
export class AnimationService { export class AnimationService {
@@ -26,45 +28,32 @@ export class AnimationService {
public graphs: Graph[]; public graphs: Graph[];
public speed: number; public speed: number;
public range: number; public range: number;
public genAnimation: any; // public genAnimation: any;
private timer: Observable<number>; // private timer: Observable<number>;
private subscribtion: any; // private subscribtion: any;
constructor() { constructor(
this.timer = interval(500); private arithmetics: ArithmeticService,
this.resetAnimation(); private historyService: HistoryService,
) {
} }
private resetAnimation() { public animate(initialGraphs: Graph[]) {
this.genAnimation = this.animateNextStep(); const newGraphs = initialGraphs.slice();
}
private *animateNextStep() { return newGraphs.map(graph => {
while (this.graphs) {
yield this.graphs = this.graphs.map(graph => { const newGraph = Object.assign({}, graph);
console.log(graph); const indexMiddle = Math.floor(newGraph.nodes.length * 0.5);
return graph; const pointMiddle = newGraph.nodes[indexMiddle];
newGraph.nodes.splice(indexMiddle, 1, {
x: pointMiddle.x - 1,
y: pointMiddle.y + 1,
});
return newGraph;
}); });
} }
} }
// public start(initialGraphs: Graph[]): Observable<Graph[]> {
// // this.graphs = initialGraphs.map(graph => {
// // console.log(graph);
// // return graph;
// // });
// // return this.timer.subscribe(n => this.graphs);
// }
// public animate(): Graph[] {
// return this.genAnimation.next().value;
// }
public animate(initialGraphs: Graph[]) {
this.graphs = initialGraphs;
return this.genAnimation.next().value;
}
}

View File

@@ -14,15 +14,19 @@
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/ */
import { Inject, Injectable, Optional, ViewChild } from '@angular/core'; import { Inject, Injectable, Renderer2, RendererFactory2 } from '@angular/core';
import * as Selection from 'd3-selection'; import * as Selection from 'd3-selection';
@Injectable() @Injectable()
export class CanvasService { export class CanvasService {
private renderer: Renderer2;
public canvas: any; public canvas: any;
constructor() { constructor(
private rendererFactory: RendererFactory2
) {
this.renderer = rendererFactory.createRenderer(null, null);
} }
public get get() { public get get() {
@@ -32,4 +36,19 @@ export class CanvasService {
public set(el) { public set(el) {
this.canvas = el; this.canvas = el;
} }
public adjustToWindow() {
if (this.canvas) {
this.renderer.setStyle(
this.canvas,
'width',
window.innerWidth
);
this.renderer.setStyle(
this.canvas,
'height',
window.innerHeight
);
}
}
} }

View File

@@ -38,16 +38,11 @@ export class HistoryService {
} }
public hash(graphs) { public hash(graphs) {
return btoa(JSON.stringify([graphs])); return btoa(JSON.stringify(graphs));
} }
public list() { public list() {
return this.history; return this.history;
} }
public restore(graphs: Graph[]) {
console.log(graphs);
}
} }