//! Phase 3's 2D features, for the native screenshot test //! (test/render.script) and the browser one (tools/render_web.mjs): an //! offscreen target with translucent content, drawn plain, with a //! translucent tint and turned; additive blending of shapes, a rect and //! an image over gray; shapes of several colours in one command; a soft //! image from `pixels`, tinted and turned; antialiased shape edges. //! //! The target is painted only when `in.jumped` (or when made): the bar //! under it grows by one step per repaint, and `hue` picks its colour, so //! a script can see that `set` repaints and plain frames do not. const std = @import("std"); const sk = @import("sketch"); const vec = sk.math; pub const name = "render"; pub const width = 400; pub const height = 300; pub const State = struct { hue: u8 = 0, }; pub const Session = struct { layer: sk.Image = .none, soft: sk.Image = .none, paints: u32 = 0, }; const red: sk.Color = .{ .r = 255, .g = 0, .b = 0, .a = 128 }; /// 32 x 32 white, alpha falling off from the middle, like NoC's texture. fn soft() [32 * 32]sk.Color { @setEvalBranchQuota(100_000); var px: [32 * 32]sk.Color = undefined; for (0..32) |y| for (0..32) |x| { const d = vec.distance(.{ @floatFromInt(x), @floatFromInt(y) }, .{ 15.5, 15.5 }) / 16; px[y * 32 + x] = sk.Color.white.alphaFloat(1 - d); }; return px; } const soft_pixels = soft(); pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void { f.clear(.white); const fresh = ss.layer == .none; if (fresh) { ss.layer = f.host.newImage(100, 100); ss.soft = f.host.newImage(32, 32); f.pixels(ss.soft, 32, 32, std.mem.sliceAsBytes(&soft_pixels), .{}); } if (fresh or f.in.jumped) { ss.paints += 1; f.target(ss.layer); f.clear(sk.Color.black.alpha(0)); sk.draw.circle(f, .{ 50, 50 }, 40, .{ .fill = red }); f.fill(.{ .x = 10, .y = 60, .w = 50, .h = 30 }, if (s.hue == 0) sk.Color.hex(0x0000ff) else sk.Color.hex(0x00a000)); f.target(.none); } // The target three ways: as is, half transparent, turned. f.image(ss.layer, .{ .x = 10, .y = 10, .w = 100, .h = 100 }, .{}); f.image(ss.layer, .{ .x = 120, .y = 10, .w = 100, .h = 100 }, .{ .tint = sk.Color.white.alpha(128) }); f.imageAt(ss.layer, .place(.{ 300, 60 }, std.math.pi / 6.0), .{}); f.fill(.{ .x = 10, .y = 115, .w = @as(f32, @floatFromInt(@min(ss.paints, 30))) * 10, .h = 4 }, .black); // Additive blending over gray. f.fill(.{ .x = 10, .y = 130, .w = 180, .h = 90 }, 0x404040); f.blend(.add); sk.draw.circle(f, .{ 60, 175 }, 35, .{ .fill = red }); sk.draw.circle(f, .{ 100, 175 }, 35, .{ .fill = sk.Color.hex(0x00c800) }); f.fill(.{ .x = 140, .y = 140, .w = 40, .h = 70 }, sk.Color.hex(0x2020ff).alpha(200)); f.image(ss.soft, .{ .x = 140, .y = 180, .w = 32, .h = 32 }, .{ .tint = sk.Color.hex(0xff64ff).alpha(200) }); f.blend(.alpha); // Several colours in one shape: three circles and a ring (two subpaths). var t: sk.draw.Tessellation = .empty; var colors: std.ArrayList(sk.Color) = .empty; const each = [_]struct { at: vec.Vec, color: sk.Color }{ .{ .at = .{ 230, 160 }, .color = sk.Color.hex(0xff0000) }, .{ .at = .{ 260, 160 }, .color = sk.Color.hex(0x00ff00).alpha(128) }, .{ .at = .{ 290, 160 }, .color = sk.Color.hex(0x0000ff).alpha(128) }, }; for (each) |e| { t.fillCircle(f.scratch, e.at, 20, .identity) catch return; while (colors.items.len < t.ends.items.len) colors.append(f.scratch, e.color) catch return; } t.strokeCircle(f.scratch, .{ 350, 160 }, 25, 8, .identity) catch return; while (colors.items.len < t.ends.items.len) colors.append(f.scratch, sk.Color.hex(0x800080)) catch return; f.shapeColors(&t, colors.items); // The soft image: plain, tinted translucent, and turned 45 degrees. f.fill(.{ .x = 210, .y = 200, .w = 180, .h = 90 }, 0x203040); f.image(ss.soft, .{ .x = 220, .y = 220, .w = 32, .h = 32 }, .{}); f.image(ss.soft, .{ .x = 270, .y = 220, .w = 32, .h = 32 }, .{ .tint = sk.Color.hex(0xff64ff).alpha(128) }); f.imageAt(ss.soft, .place(.{ 346, 236 }, std.math.pi / 4.0), .{ .size = .{ 48, 48 } }); // Thin antialiased lines: a fan of 1 px strokes. for (0..8) |i| { const a = @as(f32, @floatFromInt(i)) * std.math.pi / 16.0; sk.draw.line(f, .{ 30, 280 }, .{ 30 + 150 * @cos(a), 280 - 50 * @sin(a) }, .{ .stroke = .black }); } }