Last active
May 5, 2016 16:19
-
-
Save SekibOmazic/b6bc98319f4681ff8a0684966f011663 to your computer and use it in GitHub Desktop.
angular2 timeflies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
System.config({ | |
transpiler: 'typescript', | |
typescriptOptions: { | |
emitDecoratorMetadata: true | |
}, | |
map: { | |
app: './src' | |
}, | |
packages: { | |
app: { | |
main: './main.ts', | |
defaultExtension: 'ts' | |
} | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>angular2 playground</title> | |
<script src="https://code.angularjs.org/2.0.0-beta.17/angular2-polyfills.js"></script> | |
<script src="https://code.angularjs.org/tools/system.js"></script> | |
<script src="https://code.angularjs.org/tools/typescript.js"></script> | |
<script src="config.js"></script> | |
<script src="https://code.angularjs.org/2.0.0-beta.17/Rx.js"></script> | |
<script src="https://code.angularjs.org/2.0.0-beta.17/angular2.dev.js"></script> | |
<script src="https://code.angularjs.org/2.0.0-beta.17/http.dev.js"></script> | |
<script src="https://code.angularjs.org/2.0.0-beta.17/router.dev.js"></script> | |
<script> | |
System.import('app') | |
.catch(console.error.bind(console)); | |
</script> | |
</head> | |
<body> | |
<app></app> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'rxjs/Rx'; | |
import {HTTP_PROVIDERS, Http} from 'angular2/http'; | |
import {Component, ElementRef, NgZone} from 'angular2/core'; | |
import {bootstrap} from 'angular2/platform/browser'; | |
import {Observable} from 'rxjs/Observable'; | |
import 'rxjs/add/observable/from'; | |
import {Subject} from 'rxjs/Subject'; | |
export class Message { | |
message: Array<string>; | |
constructor() { | |
this.message = ('COME TO COMSYSTO AND BE HAPPY').split(''); | |
} | |
} | |
interface LetterConfig { | |
text: string; | |
top: number; | |
left: number; | |
index: number; | |
} | |
@Component({ | |
selector: 'timeflies', | |
providers: [ Message ], | |
template: ` | |
<div style="background-color: papayawhip; height: 500px;"> | |
<span *ngFor="let 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 implements OnInit { | |
pos = 'absolute'; | |
color = 'red'; | |
letters: LetterConfig[]; | |
constructor( | |
private service: Message, | |
private el: ElementRef, | |
private zone: NgZone) { | |
} | |
ngOnInit() { | |
// initial mapping (before mouse moves) | |
this.letters = this.service.message.map( | |
(val, idx) => ({ | |
text: val, | |
top: 100, | |
left: (idx * 20 + 50), | |
index: idx | |
}) | |
); | |
this.zone.runOutsideAngular(() => { | |
Observable | |
.fromEvent(this.el.nativeElement, 'mousemove') | |
.map((e: MouseEvent) => { | |
// 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 Observable | |
.from(this.letters | |
.map((val, index) => ({ | |
letter: val.text, | |
delta, | |
index | |
}))); | |
}) | |
.flatMap(letterConfig => { | |
return (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 | |
} | |
@Component({ | |
selector: 'app', | |
directives: [Timeflies], | |
template: `<timeflies></timeflies>` | |
}) | |
export class App{ | |
constructor(){} | |
} | |
bootstrap(App, [HTTP_PROVIDERS]); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment