OLD | NEW |
1 import 'package:angular/angular.dart'; | 1 import 'package:angular/angular.dart'; |
| 2 import 'package:angular/application_factory.dart'; |
2 import 'dart:html'; | 3 import 'dart:html'; |
3 import 'dart:math'; | 4 import 'dart:math'; |
4 import 'dart:core'; | 5 import 'dart:core'; |
5 | 6 |
6 var random = new Random(); | 7 var random = new Random(); |
7 var width = 400; | 8 var width = 400; |
8 var height = 400; | 9 var height = 400; |
9 var speed = .05; | 10 var speed = .05; |
10 | 11 |
11 class BallModel { | 12 class BallModel { |
12 var x = width * random.nextDouble(); | 13 var x = width * random.nextDouble(); |
13 var y = height * random.nextDouble(); | 14 var y = height * random.nextDouble(); |
14 var velX = 2 * speed * random.nextDouble() - speed; | 15 var velX = 2 * speed * random.nextDouble() - speed; |
15 var velY = 2 * speed * random.nextDouble() - speed; | 16 var velY = 2 * speed * random.nextDouble() - speed; |
16 var color = BallModel._color(); | 17 var color = BallModel._color(); |
17 | 18 |
18 static _color() { | 19 static _color() { |
19 var color = '#'; | 20 var color = '#'; |
20 for(var i = 0; i < 6; i++) { | 21 for(var i = 0; i < 6; i++) { |
21 color += (16 * random.nextDouble()).floor().toRadixString(16); | 22 color += (16 * random.nextDouble()).floor().toRadixString(16); |
22 } | 23 } |
23 return color; | 24 return color; |
24 } | 25 } |
25 | 26 |
26 } | 27 } |
27 | 28 |
28 @NgController( | 29 @Controller( |
29 selector: '[bounce-controller]', | 30 selector: '[bounce-controller]', |
30 publishAs: 'bounce') | 31 publishAs: 'bounce') |
31 class BounceController { | 32 class BounceController { |
32 var lastTime = window.performance.now(); | 33 var lastTime = window.performance.now(); |
33 var run = true; | 34 var run = false; |
34 var fps = 0; | 35 var fps = 0; |
35 var digestTime = 0; | 36 var digestTime = 0; |
36 var currentDigestTime = 0; | 37 var currentDigestTime = 0; |
37 var balls = []; | 38 var balls = []; |
38 final NgZone zone; | |
39 final Scope scope; | 39 final Scope scope; |
40 var ballClassName = 'ball'; | 40 var ballClassName = 'ball'; |
41 | 41 |
42 BounceController(this.zone, this.scope) { | 42 BounceController(this.scope) { |
43 changeCount(100); | 43 changeCount(100); |
44 tick(); | 44 if (run) tick(); |
45 } | 45 } |
46 | 46 |
47 void toggleCSS() { | 47 void toggleCSS() { |
48 ballClassName = ballClassName == '' ? 'ball' : ''; | 48 ballClassName = ballClassName == '' ? 'ball' : ''; |
49 } | 49 } |
50 | 50 |
51 void playPause() { | 51 void playPause() { |
52 run = !run; | 52 run = !run; |
53 if (run) requestAnimationFrame(tick); | 53 if (run) requestAnimationFrame(tick); |
54 } | 54 } |
55 | 55 |
56 void requestAnimationFrame(fn) { | 56 void requestAnimationFrame(fn) { |
57 window.requestAnimationFrame((_) => zone.run(fn)); | 57 window.requestAnimationFrame((_) => fn()); |
58 } | 58 } |
59 | 59 |
60 void changeCount(count) { | 60 void changeCount(count) { |
61 while(count > 0) { | 61 while(count > 0) { |
62 balls.add(new BallModel()); | 62 balls.add(new BallModel()); |
63 count--; | 63 count--; |
64 } | 64 } |
65 while(count < 0 && balls.isNotEmpty) { | 65 while(count < 0 && balls.isNotEmpty) { |
66 balls.removeAt(0); | 66 balls.removeAt(0); |
67 count++; | 67 count++; |
(...skipping 22 matching lines...) Expand all Loading... |
90 if (b.y < 0) { b.y *= -1; b.velY *= -1; } | 90 if (b.y < 0) { b.y *= -1; b.velY *= -1; } |
91 if (b.x > width) { b.x = 2*width - b.x; b.velX *= -1; } | 91 if (b.x > width) { b.x = 2*width - b.x; b.velX *= -1; } |
92 if (b.y > height) { b.y = 2*height - b.y; b.velY *= -1; } | 92 if (b.y > height) { b.y = 2*height - b.y; b.velY *= -1; } |
93 } | 93 } |
94 lastTime = now; | 94 lastTime = now; |
95 timeDigest(); | 95 timeDigest(); |
96 if (run) requestAnimationFrame(tick); | 96 if (run) requestAnimationFrame(tick); |
97 } | 97 } |
98 } | 98 } |
99 | 99 |
100 @NgDirective( | 100 List<String> _CACHE = new List.generate(500, (i) => '${i}px'); |
| 101 |
| 102 @Decorator( |
101 selector: '[ball-position]', | 103 selector: '[ball-position]', |
102 map: const { | 104 map: const { |
103 "ball-position": '=>position'}) | 105 "ball-position": '=>position'}, |
104 class BallPositionDirective { | 106 exportExpressions: const ['x', 'y']) |
| 107 class BallPosition { |
105 final Element element; | 108 final Element element; |
106 final Scope scope; | 109 final Scope scope; |
107 BallPositionDirective(this.element, this.scope); | 110 BallPosition(this.element, this.scope); |
| 111 |
| 112 px(x) => _CACHE[max(0, x.round())]; |
108 | 113 |
109 set position(BallModel model) { | 114 set position(BallModel model) { |
110 element.style.backgroundColor = model.color; | 115 var style = element.style; |
| 116 style.backgroundColor = model.color; |
111 scope | 117 scope |
112 ..watch('x', (x, _) => element.style.left = '${x + 10}px', context: mode
l, readOnly: true) | 118 ..watch('x', (x, _) => element.style.left = '${x + 10}px', |
113 ..watch('y', (y, _) => element.style.top = '${y + 10}px', context: model
, readOnly: true); | 119 context: model, canChangeModel: false) |
| 120 ..watch('y', (y, _) => element.style.top = '${y + 10}px', |
| 121 context: model, canChangeModel: false); |
114 } | 122 } |
115 } | 123 } |
116 | 124 |
117 class MyModule extends Module { | 125 class MyModule extends Module { |
118 MyModule() { | 126 MyModule() { |
119 type(BounceController); | 127 type(BounceController); |
120 type(BallPositionDirective); | 128 type(BallPosition); |
121 value(GetterCache, new GetterCache({ | |
122 'x': (o) => o.x, | |
123 'y': (o) => o.y, | |
124 'bounce': (o) => o.bounce, | |
125 'fps': (o) => o.fps, | |
126 'balls': (o) => o.balls, | |
127 'length': (o) => o.length, | |
128 'digestTime': (o) => o.digestTime, | |
129 'ballClassName': (o) => o.ballClassName | |
130 })); | |
131 value(ScopeStats, new ScopeStats(report: true)); | |
132 } | 129 } |
133 } | 130 } |
134 | 131 |
135 main() { | 132 main() { |
136 ngBootstrap(module: new MyModule()); | 133 applicationFactory().addModule(new MyModule()).run(); |
137 } | 134 } |
OLD | NEW |