This repository was archived by the owner on Feb 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathtimeflies.ts
100 lines (87 loc) · 2.53 KB
/
timeflies.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/// <reference path="../../typings/_custom.d.ts" />
// Angular 2
import {Component, ElementRef, NgZone, CORE_DIRECTIVES} from 'angular2/angular2';
// Services
import {MESSAGE_PROVIDERS, Message} from '../services/Message';
import * as Rx from '@reactivex/rxjs';
interface LetterConfig {
text: string;
top: number;
left: number;
index: number;
}
@Component({
selector: 'timeflies',
providers: [ MESSAGE_PROVIDERS ],
directives: [ CORE_DIRECTIVES ],
template: `
<div style="background-color: papayawhip; height: 500px;">
<span *ng-for="#letter of letters"
[style.color]="color"
[style.left]="letter.left + 'px'"
[style.top]="letter.top + 'px'"
[style.position]="pos">
{{ letter.text }}
</span>
</div>
`
})
export class Timeflies {
pos = 'absolute';
color = 'red';
letters: LetterConfig[];
constructor(
private service: Message,
private el: ElementRef,
private zone: NgZone) {
}
onInit() {
// initial mapping (before mouse moves)
this.letters = this.service.message.map(
(val, idx) => ({
text: val,
top: 100,
left: (idx * 20 + 50),
index: idx
})
);
// run mouse move outside of Angular
// got this hint from @mgonto
this.zone.runOutsideAngular(() => {
(<any>Rx).Observable
.fromEvent(this.el.nativeElement, 'mousemove')
.map((e: MouseEvent) => {
//var offset = getOffset(this.el);
// subtract offset of the element
var o = this.el.nativeElement.getBoundingClientRect();
return {
offsetX: e.clientX - o.left,
offsetY: e.clientY - o.top
};
})
.flatMap(delta => {
return (<any>Rx).Observable
.fromArray(this.letters
.map((val, index) => ({
letter: val.text,
delta,
index
})));
})
.flatMap(letterConfig => {
return (<any>Rx).Observable
.timer( (letterConfig.index + 1) * 100)
.map(() => ({
text: letterConfig.letter,
top: letterConfig.delta.offsetY,
left: letterConfig.delta.offsetX + letterConfig.index * 20 + 20,
index: letterConfig.index
}));
})
.subscribe(letterConfig => {
// to render the letters, put them back into app zone
this.zone.run(() => this.letters[letterConfig.index] = letterConfig);
});
});//zone
}// timeflies
}