1
1
#[ macro_use]
2
2
extern crate seed;
3
- #[ macro_use]
4
- extern crate serde_derive;
5
3
use rand:: prelude:: * ;
6
4
use seed:: prelude:: * ;
5
+ use serde:: { Deserialize , Serialize } ;
7
6
8
7
// Model
9
8
@@ -24,7 +23,7 @@ impl Car {
24
23
/// _Note_:
25
24
/// Optional feature "wasm-bindgen" has to be enabled for crate `rand` (otherwise it panics).
26
25
fn generate_speed ( ) -> f64 {
27
- thread_rng ( ) . gen_range ( 400.0 , 800.0 )
26
+ thread_rng ( ) . gen_range ( 400. , 800. )
28
27
}
29
28
30
29
fn generate_color ( ) -> CarColor {
@@ -35,14 +34,14 @@ impl Car {
35
34
36
35
impl Default for Car {
37
36
fn default ( ) -> Self {
38
- let car_width = 120.0 ;
37
+ let car_width = 120. ;
39
38
Self {
40
39
x : -car_width,
41
- y : 100.0 ,
40
+ y : 100. ,
42
41
speed : Self :: generate_speed ( ) ,
43
42
color : Self :: generate_color ( ) ,
44
43
width : car_width,
45
- height : 60.0 ,
44
+ height : 60. ,
46
45
}
47
46
}
48
47
}
@@ -88,13 +87,13 @@ fn update(msg: Msg, model: &mut Model, orders: &mut Orders<Msg>) {
88
87
Msg :: OnAnimationFrame ( time) => {
89
88
let delta = match model. previous_time {
90
89
Some ( previous_time) => time - previous_time,
91
- None => 0.0 ,
90
+ None => 0. ,
92
91
} ;
93
92
model. previous_time = Some ( time) ;
94
93
95
- if delta > 0.0 {
94
+ if delta > 0. {
96
95
// move car at least 1px to the right
97
- model. car . x += f64:: max ( 1.0 , delta / 1000.0 * model. car . speed ) ;
96
+ model. car . x += f64:: max ( 1. , delta / 1000. * model. car . speed ) ;
98
97
99
98
// we don't see car anymore => back to start + generate new color and speed
100
99
if model. car . x > model. viewport_width {
@@ -108,27 +107,21 @@ fn update(msg: Msg, model: &mut Model, orders: &mut Orders<Msg>) {
108
107
109
108
// View
110
109
111
- fn px ( number : impl ToString + Copy ) -> String {
112
- let mut value = number. to_string ( ) ;
113
- value. push_str ( "px" ) ;
114
- value
115
- }
116
-
117
110
fn view ( model : & Model ) -> El < Msg > {
118
111
// scene container + sky
119
112
div ! [
120
113
style! {
121
114
"overflow" => "hidden" ;
122
- "width" => " 100%" ;
115
+ "width" => unit! ( 100 , % ) ;
123
116
"position" => "relative" ;
124
- "height" => "170px" ;
117
+ "height" => unit! ( 170 , px ) ;
125
118
"background-color" => "deepskyblue" ;
126
119
} ,
127
120
// road
128
121
div![ style! {
129
- "width" => " 100%" ;
130
- "height" => "20px" ;
131
- "bottom" => "0" ;
122
+ "width" => unit! ( 100 , % ) ;
123
+ "height" => unit! ( 20 , px ) ;
124
+ "bottom" => 0 ;
132
125
"background-color" => "darkgray" ;
133
126
"position" => "absolute" ;
134
127
} ] ,
@@ -140,44 +133,44 @@ fn view_car(car: &Car) -> El<Msg> {
140
133
div ! [
141
134
// car container
142
135
style! {
143
- "width" => px ( car. width) ;
144
- "height" => px ( car. height) ;
145
- "top" => px ( car. y) ;
146
- "left" => px ( car. x) ;
136
+ "width" => unit! ( car. width, px ) ;
137
+ "height" => unit! ( car. height, px ) ;
138
+ "top" => unit! ( car. y, px ) ;
139
+ "left" => unit! ( car. x, px ) ;
147
140
"position" => "absolute" ;
148
141
} ,
149
142
// windows
150
143
div![ style! {
151
144
"background-color" => "rgb(255,255,255,0.5)" ;
152
- "left" => px ( car. width * 0.25 ) ;
153
- "width" => px ( car. width * 0.50 ) ;
154
- "height" => px ( car. height * 0.60 ) ;
155
- "border-radius" => "9999px" ;
145
+ "left" => unit! ( car. width * 0.25 , px ) ;
146
+ "width" => unit! ( car. width * 0.5 , px ) ;
147
+ "height" => unit! ( car. height * 0.6 , px ) ;
148
+ "border-radius" => unit! ( 9999 , px ) ;
156
149
"position" => "absolute" ;
157
150
} ] ,
158
151
// body
159
152
div![ style! {
160
- "top" => px ( car. height * 0.35 ) ;
153
+ "top" => unit! ( car. height * 0.35 , px ) ;
161
154
"background-color" => car. color;
162
- "width" => px ( car. width) ;
163
- "height" => px ( car. height * 0.50 ) ;
164
- "border-radius" => "9999px" ;
155
+ "width" => unit! ( car. width, px ) ;
156
+ "height" => unit! ( car. height * 0.5 , px ) ;
157
+ "border-radius" => unit! ( 9999 , px ) ;
165
158
"position" => "absolute" ;
166
159
} ] ,
167
160
view_wheel( car. width * 0.15 , car) ,
168
- view_wheel( car. width * 0.60 , car)
161
+ view_wheel( car. width * 0.6 , car)
169
162
]
170
163
}
171
164
172
165
fn view_wheel ( wheel_x : f64 , car : & Car ) -> El < Msg > {
173
- let wheel_radius = car. height * 0.40 ;
166
+ let wheel_radius = car. height * 0.4 ;
174
167
div ! [ style! {
175
- "top" => px ( car. height * 0.55 ) ;
176
- "left" => px ( wheel_x) ;
168
+ "top" => unit! ( car. height * 0.55 , px ) ;
169
+ "left" => unit! ( wheel_x, px ) ;
177
170
"background-color" => "black" ;
178
- "width" => px ( wheel_radius) ;
179
- "height" => px ( wheel_radius) ;
180
- "border-radius" => "9999px" ;
171
+ "width" => unit! ( wheel_radius, px ) ;
172
+ "height" => unit! ( wheel_radius, px ) ;
173
+ "border-radius" => unit! ( 9999 , px ) ;
181
174
"position" => "absolute" ;
182
175
} ]
183
176
}
0 commit comments