summaryrefslogtreecommitdiff
path: root/test/shapes.zig
diff options
context:
space:
mode:
Diffstat (limited to 'test/shapes.zig')
-rw-r--r--test/shapes.zig63
1 files changed, 63 insertions, 0 deletions
diff --git a/test/shapes.zig b/test/shapes.zig
new file mode 100644
index 0000000..5145143
--- /dev/null
+++ b/test/shapes.zig
@@ -0,0 +1,63 @@
+//! The cases `Command.shape` must draw alike on both hosts, for the native
+//! screenshot test (test/shapes.script) and the browser one
+//! (tools/shapes_web.mjs): overlapping translucent circles, a concave
+//! polygon, an acute translucent zig-zag, a ring, a 1 px line. The console
+//! line `circles N` switches to N circles batched or not (`batched N`),
+//! for timing.
+const std = @import("std");
+const sk = @import("sketch");
+
+pub const name = "shapes";
+pub const width = 400;
+pub const height = 400;
+
+pub const State = struct {
+ circles: u32 = 0,
+ batched: bool = false,
+};
+
+pub const commands = .{ "circles", "batched" };
+
+pub fn command(s: *State, line: []const u8) !bool {
+ var words = std.mem.tokenizeScalar(u8, line, ' ');
+ const verb = words.next() orelse return false;
+ if (!std.mem.eql(u8, verb, "circles") and !std.mem.eql(u8, verb, "batched")) return false;
+ s.circles = try std.fmt.parseInt(u32, words.next() orelse "0", 10);
+ s.batched = std.mem.eql(u8, verb, "batched");
+ return true;
+}
+
+const red: sk.Color = .{ .r = 255, .g = 0, .b = 0, .a = 128 };
+const green: sk.Color = .{ .r = 0, .g = 128, .b = 0, .a = 128 };
+const blue: sk.Color = .{ .r = 0, .g = 0, .b = 255, .a = 128 };
+const purple: sk.Color = .{ .r = 128, .g = 0, .b = 128, .a = 160 };
+
+const arrow = [_]sk.math.Vec{ .{ 220, 20 }, .{ 380, 20 }, .{ 380, 180 }, .{ 300, 70 }, .{ 220, 180 } };
+const zigzag = [_]sk.math.Vec{ .{ 20, 380 }, .{ 60, 220 }, .{ 100, 380 }, .{ 140, 220 }, .{ 180, 380 } };
+
+pub fn draw(s: *const State, f: *sk.Frame) void {
+ f.clear(sk.Color.white);
+ if (s.circles > 0) return many(s, f);
+ // Two commands: the overlap blends twice, on both hosts.
+ sk.draw.circle(f, .{ 80, 80 }, 50, .{ .fill = red });
+ sk.draw.circle(f, .{ 140, 80 }, 50, .{ .fill = red });
+ sk.draw.polygon(f, &arrow, .{ .fill = green });
+ var t: sk.draw.Tessellation = .empty;
+ t.strokePolyline(f.scratch, &zigzag, 14, .identity) catch return;
+ f.shape(&t, blue);
+ sk.draw.circle(f, .{ 300, 240 }, 30, .{ .stroke = purple, .width = 8 });
+ sk.draw.line(f, .{ 220, 300.5 }, .{ 380, 300.5 }, .{ .stroke = .black });
+}
+
+/// Circles spread by a hash, in one shape each or all in one.
+fn many(s: *const State, f: *sk.Frame) void {
+ var t: sk.draw.Tessellation = .empty;
+ for (0..s.circles) |i| {
+ const h = std.hash.Wyhash.hash(0, std.mem.asBytes(&i));
+ const at: sk.math.Vec = .{ @floatFromInt(h % 400), @floatFromInt((h >> 16) % 400) };
+ if (s.batched) {
+ t.fillCircle(f.scratch, at, 4, .identity) catch return;
+ } else sk.draw.circle(f, at, 4, .{ .fill = .{ .r = @truncate(h >> 32), .g = 40, .b = 90, .a = 160 } });
+ }
+ if (s.batched) f.shape(&t, .{ .r = 200, .g = 40, .b = 90 });
+}