2
0

added gradient definitions

This commit is contained in:
2018-05-12 15:35:45 +02:00
parent 034f70c982
commit 2975a6c5de
6 changed files with 50 additions and 11 deletions

View File

@@ -1,6 +1,7 @@
<div class="canvas" appCanvas (emitConfig)="updateCanvasConfig($event)"></div> <div class="canvas" appCanvas (emitConfig)="updateCanvasConfig($event)" [param]="canvasParam"></div>
<ul *ngIf="canvasConfig"> <ul *ngIf="canvasConfig">
<li><pre>{{canvasConfig | json}}</pre></li> <li><pre>{{canvasConfig | json}}</pre></li>
<li><pre>{{canvasParam | json}}</pre></li>
</ul> </ul>

View File

@@ -9,6 +9,7 @@
ul { ul {
display: flex; display: flex;
flex-direction: column;
position: absolute; position: absolute;
top: 0; top: 0;
right: auto; right: auto;
@@ -23,7 +24,6 @@ ul {
li { li {
display: flex; display: flex;
justify-content: center;
align-items: center; align-items: center;
} }
} }

View File

@@ -1,4 +1,6 @@
import { Component, OnInit, ChangeDetectorRef } from '@angular/core'; import { Component, OnInit, ChangeDetectorRef } from '@angular/core';
import { Param } from './models/param.model';
import { Config } from './models/config.model'; import { Config } from './models/config.model';
@Component({ @Component({
@@ -7,7 +9,17 @@ import { Config } from './models/config.model';
styleUrls: ['./app.component.scss'] styleUrls: ['./app.component.scss']
}) })
export class AppComponent { export class AppComponent {
public canvasConfig: Config|null; public canvasConfig: Config;
public canvasParam: Param;
constructor() {
this.canvasParam = {
colors: {
start: '#cc0045',
end: '#0067cc'
}
};
}
public updateCanvasConfig(config): void { public updateCanvasConfig(config): void {
this.canvasConfig = config; this.canvasConfig = config;

View File

@@ -1,19 +1,25 @@
import { Directive, ElementRef, Renderer, AfterViewInit, HostListener, Output, EventEmitter, OnInit } from '@angular/core'; import { Directive, ElementRef, Renderer, AfterViewInit, HostListener, Output, EventEmitter, OnInit, Input } 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 { Config } from './../models/config.model'; import { Config } from './../models/config.model';
import { Point } from './../models/point.model'; import { Point } from './../models/point.model';
import { Param } from './../models/param.model';
@Directive({ @Directive({
selector: '[appCanvas]' selector: '[appCanvas]'
}) })
export class CanvasDirective implements OnInit { export class CanvasDirective implements OnInit {
private canvas: any; private canvas: any;
private defs: any;
private gradient: any;
private svg: any; private svg: any;
public config: Config; public config: Config;
@Input() param: Param;
@Output() emitConfig: EventEmitter<Config>; @Output() emitConfig: EventEmitter<Config>;
@HostListener('window:resize', ['$event']) @HostListener('window:resize', ['$event'])
private onResize(event) { private onResize(event) {
this.init(); this.init();
@@ -44,6 +50,17 @@ export class CanvasDirective implements OnInit {
this.svg this.svg
.attr('width', this.config.width) .attr('width', this.config.width)
.attr('height', this.config.height); .attr('height', this.config.height);
this.defs = this.svg.append('defs');
this.gradient = this.defs.append('linearGradient')
.attr('id', 'gradient');
this.gradient.append('stop')
.attr('stop-color', this.param.colors.end)
.attr('offset', '0%');
this.gradient.append('stop')
.attr('stop-color', this.param.colors.start)
.attr('offset', '100%');
} }
private render() { private render() {
@@ -69,15 +86,15 @@ export class CanvasDirective implements OnInit {
.attr('r', 50) .attr('r', 50)
.attr('cx', point.x) .attr('cx', point.x)
.attr('cy', point.y) .attr('cy', point.y)
.attr('fill', '#971240'); .attr('fill', point.color ? point.color : 'black');
}); });
} }
private drawLine(points: Point[]) { private drawLine(points: Point[]) {
return this.svg.append('path') return this.svg.append('path')
.attr('d', Shape.line().x(p => p.x).y(p => p.y)(points)) .attr('d', Shape.line().x(p => p.x).y(p => p.y)(points))
.attr('stroke', 'blue') .attr('stroke', 'url(#gradient)')
.attr('stroke-width', 2) .attr('stroke-width', 4)
.attr('fill', 'none'); .attr('fill', 'none');
} }
@@ -91,11 +108,13 @@ export class CanvasDirective implements OnInit {
height: this.canvas.clientHeight, height: this.canvas.clientHeight,
start: { start: {
x: this.canvas.clientWidth, x: this.canvas.clientWidth,
y: 0 y: 0,
color: this.param.colors.start
}, },
end: { end: {
x: 0, x: 0,
y: this.canvas.clientHeight y: this.canvas.clientHeight,
color: this.param.colors.end
}, },
}; };
// Emit Canvas Config to parent Component // Emit Canvas Config to parent Component

View File

@@ -0,0 +1,6 @@
export interface Param {
colors: {
start: string,
end: string
};
}

View File

@@ -1,4 +1,5 @@
export interface Point { export interface Point {
x: Number; x: number;
y: Number; y: number;
color?: string;
} }