1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
//! Example 0.6: Perlin Noise Walker. Noise moves a circle; the canvas is
//! never cleared, so the circles pile up into a trail, drawn into an
//! offscreen image that keeps them (`f.target`).
const std = @import("std");
const sk = @import("sketch");
const noise = @import("processing_noise.zig").noise;
pub const name = "noise_walker";
pub const width = 640;
pub const height = 360;
const Walker = struct {
tx: f32 = 0,
ty: f32 = 10000,
x: f32 = 0,
y: f32 = 0,
fn step(w: *Walker) void {
w.x = std.math.lerp(0, width, noise(w.tx, 0, 0));
w.y = std.math.lerp(0, height, noise(w.ty, 0, 0));
w.tx += 0.01;
w.ty += 0.01;
}
};
pub const State = struct {
walker: Walker = .{},
clock: sk.Clock = .{},
};
pub fn update(s: *State, in: *const sk.Input) void {
for (0..s.clock.steps(in)) |_| s.walker.step();
}
pub const Session = struct {
trail: sk.Image = .none,
/// The walk again, as far as the trail shows it.
shadow: Walker = .{},
drawn: u64 = 0,
};
const pen: sk.draw.Pen = .{ .fill = .gray(127), .stroke = .black, .width = 2 };
/// The trail is a cache of the walk, not State: after a jump (a seek, a
/// load, a reload) it is painted again from the first step.
pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void {
if (ss.trail == .none or f.in.jumped) {
if (ss.trail == .none) ss.trail = f.host.newImage(width, height);
ss.* = .{ .trail = ss.trail };
f.target(ss.trail);
f.clear(.white);
} else f.target(ss.trail);
while (ss.drawn < s.clock.count) : (ss.drawn += 1) {
ss.shadow.step();
sk.draw.circle(f, .{ ss.shadow.x, ss.shadow.y }, 24, pen);
}
// A `set` moved the walker off the walk: go on from there.
ss.shadow = s.walker;
f.target(.none);
f.image(ss.trail, .{ .x = 0, .y = 0, .w = width, .h = height }, .{});
}
|