Animate path

This commit is contained in:
Arne Keller 2019-01-26 12:42:52 +01:00
parent 698346e1c6
commit 1b9b8ae007
6 changed files with 105 additions and 6 deletions

5
lisarennt2.txt Normal file
View File

@ -0,0 +1,5 @@
3
4 390 260 505 213 551 329 413 444
5 410 50 433 50 594 96 525 188 387 165
5 170 80 193 80 340 150 331 287 170 402
633 189

10
lisarennt3.txt Normal file
View File

@ -0,0 +1,10 @@
8
6 539 98 549 98 599 118 569 158 519 198 489 138
4 559 178 569 178 609 248 519 238
8 389 78 459 68 599 68 479 88 459 178 509 248 599 258 499 298
7 320 98 330 98 370 118 360 158 330 198 300 158 280 118
7 380 208 390 188 430 208 380 228 390 288 360 248 340 208
4 352 287 445 305 386 366 291 296
5 319 18 293 53 365 80 238 73 257 15
9 637 248 516 330 426 238 462 302 451 350 613 348 761 346 754 231 685 183
479 168

13
lisarennt4.txt Normal file
View File

@ -0,0 +1,13 @@
11
5 121 39 290 27 284 86 156 110 121 88
6 133 206 202 144 254 170 278 224 201 194 156 258
6 160 290 247 301 162 398 365 280 276 253 208 233
3 170 421 386 298 384 472
4 408 297 428 297 565 199 413 475
4 300 120 440 160 382 227 320 201
3 323 34 440 20 308 85
3 500 20 500 140 376 103
8 540 20 600 40 600 100 740 100 700 340 660 340 660 140 540 140
4 580 240 633 402 896 475 508 466
10 780 140 1020 140 1020 480 960 480 960 200 800 200 773 301 900 300 900 340 740 340
856 270

14
lisarennt5.txt Normal file
View File

@ -0,0 +1,14 @@
12
4 400 185 650 185 650 255 400 255
5 497 10 598 19 604 102 549 165 447 141
7 320 165 380 165 380 285 510 285 510 305 420 305 320 305
4 300 55 360 45 380 135 320 145
4 200 35 280 35 280 215 200 215
5 170 215 300 265 300 325 180 305 150 245
6 90 35 130 95 170 55 180 155 120 195 70 135
4 400 125 430 145 410 175 380 155
4 90 225 140 225 130 265 90 285
5 540 280 541 280 800 280 800 400 540 400
10 380 340 381 340 520 340 460 380 540 440 720 460 420 460 140 440 140 300 280 380
4 80 300 81 300 140 280 120 360
621 162

View File

@ -1,6 +1,6 @@
use svg::Document; use svg::Document;
use svg::Node; use svg::Node;
use svg::node::element::Circle; use svg::node::element::{AnimateMotion, MotionPath, Circle};
use svg::node::element::Path; use svg::node::element::Path;
use svg::node::element::path::Data; use svg::node::element::path::Data;
@ -8,16 +8,18 @@ use std::io;
use super::*; use super::*;
pub(crate) fn dump_svg(points: &[(Point, &'static str)], lines: &[(Line, &'static str)], polys: &[Polygon]) { pub(crate) fn dump_svg(points: &[(Point, &'static str)], lines: &[(Line, &'static str)], polys: &[Polygon], route1: &[Point], route2_start: Point) {
let mut document = Document::new() let mut document = Document::new()
// view box at (0, -200), w*h (200, 200) // view box at (0, -200), w*h (200, 200)
.set("viewBox", (0, -1100, 2100, 2100)); .set("viewBox", (0, -1100, 2100, 2100))
.set("xmlns:xlink", "http://www.w3.org/1999/xlink");
for circle in points.iter().map(|(p, color)| { for circle in points.iter().map(|(p, color)| {
Circle::new() Circle::new()
.set("cx", f64::from(p.x())) .set("cx", f64::from(p.x()))
.set("cy", f64::from(-p.y())) .set("cy", f64::from(-p.y()))
.set("r", 1.2) .set("r", 25.2)
.set("opacity", 0.5)
.set("fill", *color) .set("fill", *color)
}) { }) {
document.append(circle); document.append(circle);
@ -31,7 +33,8 @@ pub(crate) fn dump_svg(points: &[(Point, &'static str)], lines: &[(Line, &'stati
Path::new() Path::new()
.set("fill", "none") .set("fill", "none")
.set("stroke", *color) .set("stroke", *color)
.set("stroke-width", 0.25) .set("stroke-width", 2.0)
.set("opacity", 0.5)
.set("d", data) .set("d", data)
}) { }) {
document.append(path); document.append(path);
@ -61,5 +64,59 @@ pub(crate) fn dump_svg(points: &[(Point, &'static str)], lines: &[(Line, &'stati
document.append(path); document.append(path);
} }
let mut first: (f64, f64) = (route1[0].x().into(), route1[0].y().into());
first.1 *= -1.0;
let mut data = Data::new()
.move_to(first);
for point in &route1[1..] {
let c = point.x_y();
let mut c: (f64, f64) = (c.0.into(), c.1.into());
c.1 *= -1.0;
data = data.line_to(c);
}
let path = Path::new()
.set("id", "route1")
.set("fill", "none")
.set("stroke", "none")
.set("d", data);
document.append(path);
let mut dot1 = Circle::new()
.set("cx", 0.0)
.set("cy", 0.0)
.set("r", 25.2)
.set("opacity", 0.75)
.set("fill", "gray");
let mut motion1 = AnimateMotion::new().set("dur", "2s").set("repeatCount", "indefinite");
let path = MotionPath::new().set("xlink:href", "#route1");
motion1.append(path);
dot1.append(motion1);
document.append(dot1);
let mut first: (f64, f64) = (route2_start.x().into(), route2_start.y().into());
first.1 *= -1.0;
let mut data = Data::new()
.move_to(first);
let last = route1.last().unwrap();
data = data.line_to::<(f64, f64)>((last.x().into(), (|x:f64|-x)(last.y().into())));
let path = Path::new()
.set("id", "route2")
.set("fill", "none")
.set("stroke", "none")
.set("d", data);
document.append(path);
let mut dot1 = Circle::new()
.set("cx", 0.0)
.set("cy", 0.0)
.set("r", 25.2)
.set("opacity", 0.75)
.set("fill", "gray");
let mut motion1 = AnimateMotion::new().set("dur", "2s").set("repeatCount", "indefinite");
let path = MotionPath::new().set("xlink:href", "#route2");
motion1.append(path);
dot1.append(motion1);
document.append(dot1);
svg::write(io::stdout(), &document).unwrap(); svg::write(io::stdout(), &document).unwrap();
} }

View File

@ -142,7 +142,7 @@ fn main() {
(x, "gray") (x, "gray")
}).collect::<Vec<_>>(); }).collect::<Vec<_>>();
display::dump_svg(&points, &lines, &polys); display::dump_svg(&points, &lines, &polys, &route.iter().map(|x| x.pos).collect::<Vec<_>>(), first.bus.translate(0.0.into(), best_delay));
} }
/// [a; b] /// [a; b]