Skip to content

Commit 184023a

Browse files
dmitrizdead-claudia
authored andcommitted
Examples: Pure reactive clone of the animation example (#1830)
* Replace run by stream * Switch to m.stream.scan * view switched to pure function * Separate view from render * Remove the state from globals * Remove global, side-effects and stuff * Extract position function * Cleanup * More cleanup
1 parent 1d2ed24 commit 184023a

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

animation-stream/flowers.jpg

58.6 KB
Loading

animation-stream/mosaic-stream.html

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Mosaic</title>
6+
<style>
7+
#root {margin:auto;max-width:600px;width:100%;}
8+
.slice {animation:enter 1s;animation-fill-mode:forwards;background-image:url(flowers.jpg);height:60px;float:left;opacity:0;width:60px;}
9+
.slice:nth-child(10n) {clear:right;}
10+
.exit {animation:exit 1s;}
11+
12+
@keyframes enter {
13+
from {opacity:0;transform:rotate(-90deg) scale(0);}
14+
to {opacity:1;transform:rotate(0) scale(1);}
15+
}
16+
@keyframes exit {
17+
from {opacity:1;transform:rotate(0) scale(1);}
18+
to {opacity:0;transform:rotate(90deg) scale(0);}
19+
}
20+
</style>
21+
</head>
22+
<body>
23+
<div id="root"></div>
24+
<script src="../../mithril.js"></script>
25+
<script src="../../stream/stream.js"></script>
26+
<script>
27+
var root = document.getElementById("root")
28+
29+
const range = (start, end) => {
30+
let full = []
31+
for (let i = start; i < end; i++) full.push(i)
32+
return full
33+
}
34+
35+
const exit = vnode => {
36+
vnode.dom.classList.add("exit")
37+
return new Promise(function(resolve) {
38+
setTimeout(resolve, 1000)
39+
})
40+
}
41+
42+
const backgroundPosition = step => i =>
43+
// X
44+
(i % step * (step+1)) + "% "
45+
// Y
46+
+ (Math.floor(i / step) * (step+1)) + "%"
47+
48+
// actions -> state -> vnode
49+
const view = exit => cells =>
50+
m(".container", cells.map(i =>
51+
m(".slice", {
52+
style: {backgroundPosition: backgroundPosition(10)(i)},
53+
onbeforeremove: exit
54+
})
55+
))
56+
57+
// delayed alternate array[0] and array[1] and pipe into actions stream
58+
const alternate = delay => array => actions => {
59+
let i = 1
60+
setInterval(() => actions(array[i = 1 - i]), delay)
61+
return actions
62+
}
63+
64+
// prepare stream alternating between empty and full
65+
alternate(2000)([[], range(0, 10 * 10)])(m.stream())
66+
// pipe state stream into view
67+
.map(view(exit))
68+
// pipe view stream into render
69+
.map(vnode => m.render(root, vnode))
70+
71+
</script>
72+
</body>
73+
</html>

0 commit comments

Comments
 (0)