OLD | NEW |
1 var sunflower; | 1 var sunflower; |
2 (function(sunflower) { | 2 (function(exports) { |
3 'use strict'; | 3 'use strict'; |
4 let ORANGE = "orange"; | 4 let ORANGE = "orange"; |
5 let SEED_RADIUS = 2; | 5 let SEED_RADIUS = 2; |
6 let SCALE_FACTOR = 4; | 6 let SCALE_FACTOR = 4; |
7 let TAU = math.PI * 2; | 7 let TAU = math.PI * 2; |
8 let MAX_D = 300; | 8 let MAX_D = 300; |
9 let centerX = MAX_D / 2; | 9 let centerX = MAX_D / 2; |
10 let centerY = centerX; | 10 let centerY = centerX; |
11 // Function querySelector: (String) → Element | 11 // Function querySelector: (String) → Element |
12 function querySelector(selector) { | 12 function querySelector(selector) { |
13 return dom.document.querySelector(selector); | 13 return dom.document.querySelector(selector); |
14 } | 14 } |
15 sunflower.seeds = 0; | 15 exports.seeds = 0; |
16 dart.defineLazyProperties(sunflower, { | 16 dart.defineLazyProperties(exports, { |
17 get slider() { | 17 get slider() { |
18 return dart.as(querySelector("#slider"), dom.InputElement); | 18 return dart.as(querySelector("#slider"), dom.InputElement); |
19 }, | 19 }, |
20 get notes() { | 20 get notes() { |
21 return querySelector("#notes"); | 21 return querySelector("#notes"); |
22 }, | 22 }, |
23 get PHI() { | 23 get PHI() { |
24 return (math.sqrt(5) + 1) / 2; | 24 return (math.sqrt(5) + 1) / 2; |
25 }, | 25 }, |
26 get context() { | 26 get context() { |
27 return dart.as(dart.as(querySelector("#canvas"), dom.CanvasElement).getCon
text('2d'), dom.CanvasRenderingContext2D); | 27 return dart.as(dart.as(querySelector("#canvas"), dom.CanvasElement).getCon
text('2d'), dom.CanvasRenderingContext2D); |
28 } | 28 } |
29 }); | 29 }); |
30 class Circle extends dart.Object { | 30 class Circle extends dart.Object { |
31 Circle(x, y, radius) { | 31 Circle(x, y, radius) { |
32 this.x = x; | 32 this.x = x; |
33 this.y = y; | 33 this.y = y; |
34 this.radius = radius; | 34 this.radius = radius; |
35 } | 35 } |
36 } | 36 } |
37 class CirclePainter extends dart.Object { | 37 class CirclePainter extends dart.Object { |
38 CirclePainter() { | 38 CirclePainter() { |
39 this.color = ORANGE; | 39 this.color = ORANGE; |
40 } | 40 } |
41 draw() { | 41 draw() { |
42 sunflower.context.beginPath(); | 42 exports.context.beginPath(); |
43 sunflower.context.lineWidth = 2; | 43 exports.context.lineWidth = 2; |
44 sunflower.context.fillStyle = this.color; | 44 exports.context.fillStyle = this.color; |
45 sunflower.context.strokeStyle = this.color; | 45 exports.context.strokeStyle = this.color; |
46 sunflower.context.arc(this.x, this.y, this.radius, 0, TAU, false); | 46 exports.context.arc(this.x, this.y, this.radius, 0, TAU, false); |
47 sunflower.context.fill(); | 47 exports.context.fill(); |
48 sunflower.context.closePath(); | 48 exports.context.closePath(); |
49 sunflower.context.stroke(); | 49 exports.context.stroke(); |
50 } | 50 } |
51 } | 51 } |
52 class SunflowerSeed extends dart.mixin(Circle, CirclePainter) { | 52 class SunflowerSeed extends dart.mixin(Circle, CirclePainter) { |
53 SunflowerSeed(x, y, radius, color) { | 53 SunflowerSeed(x, y, radius, color) { |
54 if (color === void 0) | 54 if (color === void 0) |
55 color = null; | 55 color = null; |
56 super.Circle(x, y, radius); | 56 super.Circle(x, y, radius); |
57 if (color !== null) | 57 if (color !== null) |
58 this.color = color; | 58 this.color = color; |
59 } | 59 } |
60 } | 60 } |
61 // Function main: () → void | 61 // Function main: () → void |
62 function main() { | 62 function main() { |
63 sunflower.slider.addEventListener('change', (e) => draw()); | 63 exports.slider.addEventListener('change', (e) => draw()); |
64 draw(); | 64 draw(); |
65 } | 65 } |
66 // Function draw: () → void | 66 // Function draw: () → void |
67 function draw() { | 67 function draw() { |
68 sunflower.seeds = core.int.parse(sunflower.slider.value); | 68 exports.seeds = core.int.parse(exports.slider.value); |
69 sunflower.context.clearRect(0, 0, MAX_D, MAX_D); | 69 exports.context.clearRect(0, 0, MAX_D, MAX_D); |
70 for (let i = 0; i < sunflower.seeds; i++) { | 70 for (let i = 0; i < exports.seeds; i++) { |
71 let theta = dart.notNull(i * dart.notNull(TAU)) / dart.notNull(sunflower.P
HI); | 71 let theta = dart.notNull(i * dart.notNull(TAU)) / dart.notNull(exports.PHI
); |
72 let r = math.sqrt(i) * SCALE_FACTOR; | 72 let r = math.sqrt(i) * SCALE_FACTOR; |
73 let x = dart.notNull(centerX) + dart.notNull(dart.notNull(r) * math.cos(th
eta)); | 73 let x = dart.notNull(centerX) + dart.notNull(dart.notNull(r) * math.cos(th
eta)); |
74 let y = dart.notNull(centerY) - dart.notNull(dart.notNull(r) * math.sin(th
eta)); | 74 let y = dart.notNull(centerY) - dart.notNull(dart.notNull(r) * math.sin(th
eta)); |
75 new SunflowerSeed(x, y, SEED_RADIUS).draw(); | 75 new SunflowerSeed(x, y, SEED_RADIUS).draw(); |
76 } | 76 } |
77 sunflower.notes.textContent = `${sunflower.seeds} seeds`; | 77 exports.notes.textContent = `${exports.seeds} seeds`; |
78 } | 78 } |
79 // Exports: | 79 // Exports: |
80 sunflower.ORANGE = ORANGE; | 80 exports.ORANGE = ORANGE; |
81 sunflower.SEED_RADIUS = SEED_RADIUS; | 81 exports.SEED_RADIUS = SEED_RADIUS; |
82 sunflower.SCALE_FACTOR = SCALE_FACTOR; | 82 exports.SCALE_FACTOR = SCALE_FACTOR; |
83 sunflower.TAU = TAU; | 83 exports.TAU = TAU; |
84 sunflower.MAX_D = MAX_D; | 84 exports.MAX_D = MAX_D; |
85 sunflower.centerX = centerX; | 85 exports.centerX = centerX; |
86 sunflower.centerY = centerY; | 86 exports.centerY = centerY; |
87 sunflower.querySelector = querySelector; | 87 exports.querySelector = querySelector; |
88 sunflower.Circle = Circle; | 88 exports.Circle = Circle; |
89 sunflower.CirclePainter = CirclePainter; | 89 exports.CirclePainter = CirclePainter; |
90 sunflower.SunflowerSeed = SunflowerSeed; | 90 exports.SunflowerSeed = SunflowerSeed; |
91 sunflower.main = main; | 91 exports.main = main; |
92 sunflower.draw = draw; | 92 exports.draw = draw; |
93 })(sunflower || (sunflower = {})); | 93 })(sunflower || (sunflower = {})); |
OLD | NEW |