| OLD | NEW |
| 1 import 'package:angular/angular.dart'; | 1 import 'package:angular/angular.dart'; |
| 2 import 'package:angular/application_factory.dart'; | |
| 3 import 'dart:html'; | 2 import 'dart:html'; |
| 4 import 'dart:math'; | 3 import 'dart:math'; |
| 5 import 'dart:core'; | 4 import 'dart:core'; |
| 6 | 5 |
| 7 var random = new Random(); | 6 var random = new Random(); |
| 8 var width = 400; | 7 var width = 400; |
| 9 var height = 400; | 8 var height = 400; |
| 10 var speed = .05; | 9 var speed = .05; |
| 11 | 10 |
| 12 class BallModel { | 11 class BallModel { |
| 13 var x = width * random.nextDouble(); | 12 var x = width * random.nextDouble(); |
| 14 var y = height * random.nextDouble(); | 13 var y = height * random.nextDouble(); |
| 15 var velX = 2 * speed * random.nextDouble() - speed; | 14 var velX = 2 * speed * random.nextDouble() - speed; |
| 16 var velY = 2 * speed * random.nextDouble() - speed; | 15 var velY = 2 * speed * random.nextDouble() - speed; |
| 17 var color = BallModel._color(); | 16 var color = BallModel._color(); |
| 18 | 17 |
| 19 static _color() { | 18 static _color() { |
| 20 var color = '#'; | 19 var color = '#'; |
| 21 for(var i = 0; i < 6; i++) { | 20 for(var i = 0; i < 6; i++) { |
| 22 color += (16 * random.nextDouble()).floor().toRadixString(16); | 21 color += (16 * random.nextDouble()).floor().toRadixString(16); |
| 23 } | 22 } |
| 24 return color; | 23 return color; |
| 25 } | 24 } |
| 26 | 25 |
| 27 } | 26 } |
| 28 | 27 |
| 29 @Controller( | 28 @NgController( |
| 30 selector: '[bounce-controller]', | 29 selector: '[bounce-controller]', |
| 31 publishAs: 'bounce') | 30 publishAs: 'bounce') |
| 32 class BounceController { | 31 class BounceController { |
| 33 var lastTime = window.performance.now(); | 32 var lastTime = window.performance.now(); |
| 34 var run = false; | 33 var run = true; |
| 35 var fps = 0; | 34 var fps = 0; |
| 36 var digestTime = 0; | 35 var digestTime = 0; |
| 37 var currentDigestTime = 0; | 36 var currentDigestTime = 0; |
| 38 var balls = []; | 37 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.scope) { | 42 BounceController(this.zone, this.scope) { |
| 43 changeCount(100); | 43 changeCount(100); |
| 44 if (run) tick(); | 44 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((_) => fn()); | 57 window.requestAnimationFrame((_) => zone.run(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 List<String> _CACHE = new List.generate(500, (i) => '${i}px'); | 100 @NgDirective( |
| 101 | |
| 102 @Decorator( | |
| 103 selector: '[ball-position]', | 101 selector: '[ball-position]', |
| 104 map: const { | 102 map: const { |
| 105 "ball-position": '=>position'}, | 103 "ball-position": '=>position'}) |
| 106 exportExpressions: const ['x', 'y']) | 104 class BallPositionDirective { |
| 107 class BallPosition { | |
| 108 final Element element; | 105 final Element element; |
| 109 final Scope scope; | 106 final Scope scope; |
| 110 BallPosition(this.element, this.scope); | 107 BallPositionDirective(this.element, this.scope); |
| 111 | |
| 112 px(x) => _CACHE[max(0, x.round())]; | |
| 113 | 108 |
| 114 set position(BallModel model) { | 109 set position(BallModel model) { |
| 115 var style = element.style; | 110 element.style.backgroundColor = model.color; |
| 116 style.backgroundColor = model.color; | |
| 117 scope | 111 scope |
| 118 ..watch('x', (x, _) => element.style.left = '${x + 10}px', | 112 ..watch('x', (x, _) => element.style.left = '${x + 10}px', context: mode
l, readOnly: true) |
| 119 context: model, canChangeModel: false) | 113 ..watch('y', (y, _) => element.style.top = '${y + 10}px', context: model
, readOnly: true); |
| 120 ..watch('y', (y, _) => element.style.top = '${y + 10}px', | |
| 121 context: model, canChangeModel: false); | |
| 122 } | 114 } |
| 123 } | 115 } |
| 124 | 116 |
| 125 class MyModule extends Module { | 117 class MyModule extends Module { |
| 126 MyModule() { | 118 MyModule() { |
| 127 type(BounceController); | 119 type(BounceController); |
| 128 type(BallPosition); | 120 type(BallPositionDirective); |
| 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)); |
| 129 } | 132 } |
| 130 } | 133 } |
| 131 | 134 |
| 132 main() { | 135 main() { |
| 133 applicationFactory().addModule(new MyModule()).run(); | 136 ngBootstrap(module: new MyModule()); |
| 134 } | 137 } |
| OLD | NEW |