Skip to content

Lightpanda: the headless browser designed for AI and automation

add canvas toDataURL and toBlob returning 1x1 png

Pierre Tachoire342eaf103660
diff --git a/src/browser/tests/canvas/canvas_serialization.html b/src/browser/tests/canvas/canvas_serialization.html
new file mode 100644
index 000000000..38a155bd9
--- /dev/null
+++ b/src/browser/tests/canvas/canvas_serialization.html
@@ -0,0 +1,108 @@
+<!DOCTYPE html>
+<head>
+ <script src="../testing.js"></script>
+</head>
+
+<body>
+ <canvas id=c width=64 height=32></canvas>
+</body>
+
+<!--
+ There is no rasterizer: every 2D draw call is a no-op, so a canvas is always
+ blank and serializes to a fixed 1x1 transparent PNG. A real browser would
+ size the image to the canvas, but nothing here can observe image dimensions
+ (Image.naturalWidth is always 0).
+-->
+
+<script id=toDataURL_basic>
+{
+ const canvas = document.getElementById('c');
+ const url = canvas.toDataURL();
+ testing.expectEqual('string', typeof url);
+ testing.expectEqual(true, url.startsWith('data:image/png;base64,'));
+ // iVBORw0KGgo is the base64 of the 8-byte PNG signature.
+ testing.expectEqual(true, url.startsWith('data:image/png;base64,iVBORw0KGgo'));
+}
+</script>
+
+<script id=toDataURL_is_stable>
+{
+ const canvas = document.getElementById('c');
+ testing.expectEqual(canvas.toDataURL(), canvas.toDataURL());
+}
+</script>
+
+<script id=toDataURL_is_size_independent>
+{
+ // Deliberate: with no rasterizer every canvas is blank, so they all
+ // serialize to the same 1x1 transparent PNG regardless of dimensions.
+ const a = document.createElement('canvas');
+ a.width = 10; a.height = 10;
+ const b = document.createElement('canvas');
+ b.width = 20; b.height = 20;
+ testing.expectEqual(a.toDataURL(), b.toDataURL());
+}
+</script>
+
+<script id=toDataURL_default_size>
+{
+ // Default canvas is 300x150; it must serialize like any other.
+ const canvas = document.createElement('canvas');
+ testing.expectEqual(300, canvas.width);
+ testing.expectEqual(150, canvas.height);
+ testing.expectEqual(true, canvas.toDataURL().startsWith('data:image/png;base64,'));
+}
+</script>
+
+<script id=toDataURL_unsupported_type_falls_back_to_png>
+{
+ const canvas = document.getElementById('c');
+ // Per spec an unsupported type falls back to image/png.
+ testing.expectEqual(true, canvas.toDataURL('image/jpeg').startsWith('data:image/png;base64,'));
+ testing.expectEqual(true, canvas.toDataURL('image/webp', 0.5).startsWith('data:image/png;base64,'));
+}
+</script>
+
+<script id=toDataURL_zero_size>
+{
+ const canvas = document.createElement('canvas');
+ canvas.width = 0;
+ testing.expectEqual('data:,', canvas.toDataURL());
+
+ const other = document.createElement('canvas');
+ other.height = 0;
+ testing.expectEqual('data:,', other.toDataURL());
+}
+</script>
+
+<script id=toBlob_basic>
+{
+ const canvas = document.getElementById('c');
+ testing.expectEqual('function', typeof canvas.toBlob);
+
+ canvas.toBlob(function(blob) {
+ testing.expectEqual('image/png', blob.type);
+ testing.expectEqual(67, blob.size);
+ });
+}
+</script>
+
+<script id=toBlob_is_async>
+{
+ const canvas = document.getElementById('c');
+ let called = false;
+ canvas.toBlob(function() { called = true; });
+ // The callback runs from a task, never synchronously.
+ testing.expectEqual(false, called);
+}
+</script>
+
+<script id=toBlob_zero_size_yields_null>
+{
+ const canvas = document.createElement('canvas');
+ canvas.width = 0;
+ canvas.toBlob(function(blob) {
+ testing.expectEqual(null, blob);
+ });
+}
+</script>
diff --git a/src/browser/webapi/element/html/Canvas.zig b/src/browser/webapi/element/html/Canvas.zig
index 74ed84f82..3a1e53f5c 100644
--- a/src/browser/webapi/element/html/Canvas.zig
+++ b/src/browser/webapi/element/html/Canvas.zig
@@ -18,6 +18,7 @@
const lp = @import("lightpanda");
const std = @import("std");
+const log = lp.log;
const js = @import("../../../js/js.zig");
const Factory = @import("../../../Factory.zig");
const Frame = @import("../../../Frame.zig");
@@ -25,6 +26,7 @@ const Node = @import("../../Node.zig");
const Element = @import("../../Element.zig");
const HtmlElement = @import("../Html.zig");
+const Blob = @import("../../Blob.zig");
const CanvasRenderingContext2D = @import("../../canvas/CanvasRenderingContext2D.zig");
const WebGLRenderingContext = @import("../../canvas/WebGLRenderingContext.zig");
const OffscreenCanvas = @import("../../canvas/OffscreenCanvas.zig");
@@ -97,6 +99,107 @@ pub fn getContext(self: *Canvas, context_type: []const u8, frame: *Frame) !?Draw
return drawing_context;
}
+/// Largest canvas a real browser backs with a bitmap (16384 x 16384). Past
+/// that, and at zero size, there are no pixels to serialize.
+const max_area = 16384 * 16384;
+
+const png_mime = "image/png";
+
+/// A 1x1 fully transparent PNG.
+///
+/// We have no rasterizer - every `CanvasRenderingContext2D` draw call is a
+/// no-op - so a canvas is always blank and every canvas serializes to this
+/// same image. A real browser's output would carry the canvas' dimensions,
+/// but nothing here can observe them (`Image.naturalWidth` is always 0), and
+/// a constant keeps serialization O(1) no matter how large the canvas or how
+/// often a page (typically a fingerprinting script) asks for it.
+const blank_png_base64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==";
+const blank_png_data_url = "data:" ++ png_mime ++ ";base64," ++ blank_png_base64;
+const blank_png = blk: {
+ const decoder = std.base64.standard.Decoder;
+ var buf: [decoder.calcSizeForSlice(blank_png_base64) catch unreachable]u8 = undefined;
+ decoder.decode(&buf, blank_png_base64) catch unreachable;
+ const decoded = buf;
+ break :blk decoded;
+};
+
+/// Whether the canvas has pixels to serialize at all. Both serializers answer
+/// with the spec's "no data" values when it doesn't.
+fn hasBitmap(self: *const Canvas) bool {
+ const width = self.getWidth();
+ const height = self.getHeight();
+ return width > 0 and height > 0 and @as(u64, width) * height <= max_area;
+}
+
+/// Serializes the canvas, always as `blank_png`. Per spec an unsupported
+/// `type` falls back to image/png, and with nothing drawn there is no lossy
+/// encoding for `quality` to control, so both arguments are ignored.
+pub fn toDataURL(self: *const Canvas, _: ?[]const u8, _: ?f64) []const u8 {
+ // Per spec, a canvas with no pixels serializes to this exact string.
+ return if (self.hasBitmap()) blank_png_data_url else "data:,";
+}
+
+/// Same image as `toDataURL`, handed to `callback` as a Blob from a task.
+/// A canvas with no pixels calls back with null, per spec.
+pub fn toBlob(self: *const Canvas, callback: js.Function.Global, _: ?[]const u8, _: ?f64, exec: *Execution) !void {
+ const blob: ?*Blob = if (self.hasBitmap()) try Blob.initFromBytes(&blank_png, png_mime, exec) else null;
+ errdefer if (blob) |b| b.releaseRef(exec.page);
+
+ // The Blob outlives this call, so it needs a reference of its own until
+ // the task has handed it to the callback.
+ if (blob) |b| {
+ b.acquireRef();
+ }
+
+ const task = try exec._factory.create(ToBlobCallback{
+ .exec = exec,
+ .blob = blob,
+ .callback = callback,
+ });
+ errdefer exec._factory.destroy(task);
+
+ try exec._scheduler.add(task, ToBlobCallback.run, 0, .{
+ .name = "canvas.toBlob",
+ .finalizer = ToBlobCallback.cancelled,
+ });
+}
+
+const ToBlobCallback = struct {
+ exec: *Execution,
+ blob: ?*Blob,
+ callback: js.Function.Global,
+
+ // `run` and `cancelled` are mutually exclusive, so each releases exactly
+ // once.
+ fn cancelled(ctx: *anyopaque) void {
+ const self: *ToBlobCallback = @ptrCast(@alignCast(ctx));
+ self.deinit();
+ }
+
+ fn deinit(self: *ToBlobCallback) void {
+ self.callback.release();
+ if (self.blob) |b| {
+ b.releaseRef(self.exec.page);
+ }
+ self.exec._factory.destroy(self);
+ }
+
+ fn run(ctx: *anyopaque) !?u32 {
+ const self: *ToBlobCallback = @ptrCast(@alignCast(ctx));
+ defer self.deinit();
+
+ var ls: js.Local.Scope = undefined;
+ self.exec.js.localScope(&ls);
+ defer ls.deinit();
+
+ ls.toLocal(self.callback).call(void, .{self.blob}) catch |err| {
+ self.exec.page.recordJsError(err);
+ log.warn(.js, "canvas.toBlob", .{ .err = err });
+ };
+ return null;
+ }
+};
+
/// Transfers control of the canvas to an OffscreenCanvas.
/// Returns an OffscreenCanvas with the same dimensions.
pub fn transferControlToOffscreen(self: *Canvas, exec: *Execution) !*OffscreenCanvas {
@@ -119,5 +222,12 @@ pub const JsApi = struct {
pub const width = reflect.unsignedLong("width", .{ .default = 300 });
pub const height = reflect.unsignedLong("height", .{ .default = 150 });
pub const getContext = bridge.function(Canvas.getContext, .{});
+ pub const toDataURL = bridge.function(Canvas.toDataURL, .{});
+ pub const toBlob = bridge.function(Canvas.toBlob, .{});
pub const transferControlToOffscreen = bridge.function(Canvas.transferControlToOffscreen, .{});
};
+
+const testing = @import("../../../../testing.zig");
+test "WebApi: HTMLCanvasElement serialization" {
+ try testing.htmlRunner("canvas/canvas_serialization.html", .{});
+}