Last active
March 30, 2016 16:29
-
-
Save johnlindquist/4b486b0f07a3bd6ce8f5a55650c0b412 to your computer and use it in GitHub Desktop.
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 lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>RxJSCraft</title> | |
<script src="https://npmcdn.com/[email protected]/dist/system.js"></script> | |
<script src="https://npmcdn.com/[email protected]/lib/typescript.js"></script> | |
<script src="https://npmcdn.com/[email protected]/bundles/Rx.js"></script> | |
<style> | |
.red-ball{ | |
position: absolute; | |
background-color: red; | |
border-radius: 50%; | |
width:20px; | |
height:20px; | |
} | |
.container{ | |
border: 10px solid black; | |
width: 200px; | |
height: 200px; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="red-ball"></div> | |
<div class="red-ball"></div> | |
<div class="red-ball"></div> | |
<div class="red-ball"></div> | |
<div class="red-ball"></div> | |
<div class="red-ball"></div> | |
<div class="red-ball"></div> | |
<div class="red-ball"></div> | |
<div class="red-ball"></div> | |
<div class="red-ball"></div> | |
</div> | |
<script> | |
SystemJS.config({ | |
transpiler: "typescript", | |
typescriptOptions: { | |
emitDecoratorMetadata: true | |
}, | |
packages: { | |
"src": { | |
"main": "app", | |
"defaultExtension": "ts" | |
} | |
} | |
}); | |
System.import('src'); | |
</script> | |
</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 {Observable} from "rxjs/Rx"; | |
const container = document.querySelector('.container'); | |
const redBalls = document.querySelectorAll('.red-ball'); | |
const redBallsArray = Array.from(redBalls); | |
const redBalls$ = Observable.from(redBallsArray); | |
//randomly position the balls | |
redBallsArray.forEach((ball:HTMLElement) => { | |
ball.style.top = Math.random() * 200 + "px"; | |
ball.style.left = Math.random() * 200 + "px"; | |
}); | |
const redBallClick$ = Observable.fromEvent(redBalls, 'click') | |
.do(event => event.stopPropagation()); | |
const containerClick$ = Observable.fromEvent(container, 'click') | |
.do(event => event.stopPropagation()); | |
const changeElementColor = color => element => { | |
element.style.backgroundColor = color; | |
}; | |
redBallClick$ | |
.pluck('target') | |
.subscribe(changeElementColor("green")); | |
containerClick$ | |
.switchMapTo(redBalls$) | |
.subscribe(changeElementColor("red")); | |
redBallClick$ | |
.pluck("target") | |
.window(containerClick$) | |
.mergeMap(windowed => windowed.toArray()) | |
.switchMap(x => Observable.from(new Set(x))) | |
.withLatestFrom(containerClick$) | |
.subscribe(([ball, event])=>{ | |
console.log(ball) | |
const {clientX:x, clientY:y} = event; | |
const {offsetLeft:left, offsetTop:top, offsetWidth:w, offsetHeight:h} = ball; | |
const targetX = x - left - w/2; | |
const targetY = y - top - h/2; | |
ball.style['transition-duration'] = "1s"; | |
ball.style.transform = `translateX(${targetX}px) translateY(${targetY}px)`; | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment