| OLD | NEW |
| (Empty) | |
| 1 import 'package:angular/angular.dart'; |
| 2 import 'dart:html'; |
| 3 import 'dart:math'; |
| 4 import 'dart:core'; |
| 5 |
| 6 var random = new Random(); |
| 7 var width = 400; |
| 8 var height = 400; |
| 9 var speed = .05; |
| 10 |
| 11 class BallModel { |
| 12 var x = width * random.nextDouble(); |
| 13 var y = height * random.nextDouble(); |
| 14 var velX = 2 * speed * random.nextDouble() - speed; |
| 15 var velY = 2 * speed * random.nextDouble() - speed; |
| 16 var color = BallModel._color(); |
| 17 |
| 18 static _color() { |
| 19 var color = '#'; |
| 20 for(var i=0; i < 6; i++) { |
| 21 color += (16 * random.nextDouble()).floor().toRadixString(16); |
| 22 } |
| 23 return color; |
| 24 } |
| 25 |
| 26 } |
| 27 |
| 28 @NgController( |
| 29 selector: '[bounce-controller]', |
| 30 publishAs: 'bounce' |
| 31 ) |
| 32 class BounceController { |
| 33 var lastTime = window.performance.now(); |
| 34 var run = true; |
| 35 var fps = 0; |
| 36 var digestTime = 0; |
| 37 var balls = []; |
| 38 var zone; |
| 39 var scope; |
| 40 var ballClassName = 'ball'; |
| 41 |
| 42 BounceController(NgZone this.zone, Scope this.scope) { |
| 43 changeCount(100); |
| 44 tick(); |
| 45 } |
| 46 |
| 47 toggleCSS() { |
| 48 ballClassName = ballClassName == '' ? 'ball' : ''; |
| 49 } |
| 50 |
| 51 playPause() { |
| 52 run = !run; |
| 53 if (run) requestAnimationFrame(tick); |
| 54 } |
| 55 |
| 56 requestAnimationFrame(fn) { |
| 57 window.requestAnimationFrame((_) => zone.run(fn)); |
| 58 } |
| 59 |
| 60 changeCount(count) { |
| 61 while(count > 0) { |
| 62 balls.add(new BallModel()); |
| 63 count--; |
| 64 } |
| 65 while(count < 0) { |
| 66 balls.removeAt(0); |
| 67 count++; |
| 68 } |
| 69 tick(); |
| 70 } |
| 71 |
| 72 timeDigest() { |
| 73 var start = window.performance.now(); |
| 74 scope.$evalAsync(() { |
| 75 digestTime = (window.performance.now() - start).round(); |
| 76 }, outsideDigest: true); |
| 77 } |
| 78 |
| 79 tick() { |
| 80 var now = window.performance.now(), |
| 81 delay = now - lastTime; |
| 82 |
| 83 fps = (1000/delay).round(); |
| 84 for(var i=0, ii=balls.length; i<ii; i++) { |
| 85 var b = balls[i]; |
| 86 b.x += delay * b.velX; |
| 87 b.y += delay * b.velY; |
| 88 if (b.x < 0) { b.x *= -1; b.velX *= -1; } |
| 89 if (b.y < 0) { b.y *= -1; b.velY *= -1; } |
| 90 if (b.x > width) { b.x = 2*width - b.x; b.velX *= -1; } |
| 91 if (b.y > height) { b.y = 2*height - b.y; b.velY *= -1; } |
| 92 } |
| 93 lastTime = now; |
| 94 timeDigest(); |
| 95 if (run) requestAnimationFrame(tick); |
| 96 } |
| 97 } |
| 98 |
| 99 @NgDirective( |
| 100 selector: '[ball-position]', |
| 101 map: const { |
| 102 "ballPosition": '=>position' |
| 103 } |
| 104 ) |
| 105 class BallPositionDirective { |
| 106 Element element; |
| 107 Scope scope; |
| 108 BallPositionDirective(Element this.element, Scope this.scope); |
| 109 |
| 110 set position(BallModel model) { |
| 111 element.style.backgroundColor = model.color; |
| 112 scope.$watch(() { |
| 113 element.style.left = '${model.x + 10}px'; |
| 114 element.style.top = '${model.y + 10}px'; |
| 115 }); |
| 116 } |
| 117 } |
| 118 |
| 119 class MyModule extends Module { |
| 120 MyModule() { |
| 121 type(BounceController); |
| 122 type(BallPositionDirective); |
| 123 } |
| 124 } |
| 125 |
| 126 main() { |
| 127 ngBootstrap(module: new MyModule()); |
| 128 } |
| OLD | NEW |