OLD | NEW |
| (Empty) |
1 dart_library.library('sunflower', null, /* Imports */[ | |
2 'dart_sdk' | |
3 ], function(exports, dart_sdk) { | |
4 'use strict'; | |
5 const core = dart_sdk.core; | |
6 const html = dart_sdk.html; | |
7 const math = dart_sdk.math; | |
8 const dart = dart_sdk.dart; | |
9 const dartx = dart_sdk.dartx; | |
10 const sunflower = Object.create(null); | |
11 const circle = Object.create(null); | |
12 const painter = Object.create(null); | |
13 sunflower.SEED_RADIUS = 2; | |
14 sunflower.SCALE_FACTOR = 4; | |
15 sunflower.MAX_D = 300; | |
16 sunflower.centerX = sunflower.MAX_D / 2; | |
17 sunflower.centerY = sunflower.centerX; | |
18 sunflower.querySelector = function(selector) { | |
19 return html.document[dartx.querySelector](selector); | |
20 }; | |
21 dart.fn(sunflower.querySelector, html.Element, [core.String]); | |
22 dart.defineLazy(sunflower, { | |
23 get canvas() { | |
24 return dart.as(sunflower.querySelector("#canvas"), html.CanvasElement); | |
25 } | |
26 }); | |
27 dart.defineLazy(sunflower, { | |
28 get context() { | |
29 return dart.as(sunflower.canvas[dartx.getContext]('2d'), html.CanvasRender
ingContext2D); | |
30 } | |
31 }); | |
32 dart.defineLazy(sunflower, { | |
33 get slider() { | |
34 return dart.as(sunflower.querySelector("#slider"), html.InputElement); | |
35 } | |
36 }); | |
37 dart.defineLazy(sunflower, { | |
38 get notes() { | |
39 return sunflower.querySelector("#notes"); | |
40 } | |
41 }); | |
42 dart.defineLazy(sunflower, { | |
43 get PHI() { | |
44 return (dart.notNull(math.sqrt(5)) + 1) / 2; | |
45 } | |
46 }); | |
47 sunflower.seeds = 0; | |
48 sunflower.main = function() { | |
49 sunflower.slider[dartx.addEventListener]('change', dart.fn(e => sunflower.dr
aw(), dart.void, [html.Event])); | |
50 sunflower.draw(); | |
51 }; | |
52 dart.fn(sunflower.main, dart.void, []); | |
53 sunflower.draw = function() { | |
54 sunflower.seeds = core.int.parse(sunflower.slider[dartx.value]); | |
55 sunflower.context[dartx.clearRect](0, 0, sunflower.MAX_D, sunflower.MAX_D); | |
56 for (let i = 0; i < dart.notNull(sunflower.seeds); i++) { | |
57 let theta = i * painter.TAU / dart.notNull(sunflower.PHI); | |
58 let r = dart.notNull(math.sqrt(i)) * sunflower.SCALE_FACTOR; | |
59 let x = sunflower.centerX + r * dart.notNull(math.cos(theta)); | |
60 let y = sunflower.centerY - r * dart.notNull(math.sin(theta)); | |
61 new sunflower.SunflowerSeed(x, y, sunflower.SEED_RADIUS).draw(sunflower.co
ntext); | |
62 } | |
63 sunflower.notes[dartx.text] = `${sunflower.seeds} seeds`; | |
64 }; | |
65 dart.fn(sunflower.draw, dart.void, []); | |
66 circle.Circle = class Circle extends core.Object { | |
67 new(x, y, radius) { | |
68 this.x = x; | |
69 this.y = y; | |
70 this.radius = radius; | |
71 } | |
72 }; | |
73 dart.setSignature(circle.Circle, { | |
74 constructors: () => ({new: [circle.Circle, [core.num, core.num, core.num]]}) | |
75 }); | |
76 painter.CirclePainter = class CirclePainter extends core.Object { | |
77 new() { | |
78 this.color = painter.ORANGE; | |
79 } | |
80 draw(context) { | |
81 context[dartx.beginPath](); | |
82 context[dartx.lineWidth] = 2; | |
83 context[dartx.fillStyle] = this.color; | |
84 context[dartx.strokeStyle] = this.color; | |
85 context[dartx.arc](this.x, this.y, this.radius, 0, painter.TAU, false); | |
86 context[dartx.fill](); | |
87 context[dartx.closePath](); | |
88 context[dartx.stroke](); | |
89 } | |
90 }; | |
91 painter.CirclePainter[dart.implements] = () => [circle.Circle]; | |
92 dart.setSignature(painter.CirclePainter, { | |
93 methods: () => ({draw: [dart.void, [html.CanvasRenderingContext2D]]}) | |
94 }); | |
95 sunflower.SunflowerSeed = class SunflowerSeed extends dart.mixin(circle.Circle
, painter.CirclePainter) { | |
96 new(x, y, radius, color) { | |
97 if (color === void 0) color = null; | |
98 super.new(x, y, radius); | |
99 if (color != null) this.color = color; | |
100 } | |
101 }; | |
102 dart.setSignature(sunflower.SunflowerSeed, { | |
103 constructors: () => ({new: [sunflower.SunflowerSeed, [core.num, core.num, co
re.num], [core.String]]}) | |
104 }); | |
105 painter.ORANGE = "orange"; | |
106 painter.RED = "red"; | |
107 painter.BLUE = "blue"; | |
108 painter.TAU = math.PI * 2; | |
109 painter.querySelector = function(selector) { | |
110 return html.document[dartx.querySelector](selector); | |
111 }; | |
112 dart.fn(painter.querySelector, html.Element, [core.String]); | |
113 dart.defineLazy(painter, { | |
114 get canvas() { | |
115 return dart.as(painter.querySelector("#canvas"), html.CanvasElement); | |
116 } | |
117 }); | |
118 dart.defineLazy(painter, { | |
119 get context() { | |
120 return dart.as(painter.canvas[dartx.getContext]('2d'), html.CanvasRenderin
gContext2D); | |
121 } | |
122 }); | |
123 // Exports: | |
124 exports.sunflower = sunflower; | |
125 exports.circle = circle; | |
126 exports.painter = painter; | |
127 }); | |
128 | |
129 //# sourceMappingURL=sunflower.js.map | |
OLD | NEW |