Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/browser/canvas/CanvasRenderingContext2D.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (C) 2023-2025 Lightpanda (Selecy SAS)
//
// Francis Bouvier <francis@lightpanda.io>
// Pierre Tachoire <pierre@lightpanda.io>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

/// This class doesn't implement a `constructor`.
/// It can be obtained with a call to `HTMLCanvasElement#getContext`.
/// https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D
const CanvasRenderingContext2D = @This();

pub fn _fillRect(x: f64, y: f64, width: f64, height: f64) void {
_ = x;
_ = y;
_ = width;
_ = height;
}

pub fn get_fillStyle(_: *const CanvasRenderingContext2D) []const u8 {
return "";
}

pub fn set_fillStyle(_: *const CanvasRenderingContext2D, _: []const u8) void {}
6 changes: 6 additions & 0 deletions src/browser/canvas/root.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//! Canvas API.
//! https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API

pub const Interfaces = .{
@import("./CanvasRenderingContext2D.zig"),
};
22 changes: 22 additions & 0 deletions src/browser/html/elements.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const DataSet = @import("DataSet.zig");

const StyleSheet = @import("../cssom/StyleSheet.zig");
const CSSStyleDeclaration = @import("../cssom/CSSStyleDeclaration.zig");
const CanvasRenderingContext2D = @import("../canvas/CanvasRenderingContext2D.zig");

// HTMLElement interfaces
pub const Interfaces = .{
Expand Down Expand Up @@ -487,6 +488,23 @@ pub const HTMLCanvasElement = struct {
pub const Self = parser.Canvas;
pub const prototype = *HTMLElement;
pub const subtype = .node;

/// This should be a union once we support other context types.
const ContextAttributes = struct {
alpha: bool,
color_space: []const u8 = "srgb",
};

pub fn _getContext(
ctx_type: []const u8,
_: ?ContextAttributes,
) !CanvasRenderingContext2D {
if (!std.mem.eql(u8, ctx_type, "2d")) {
return error.NotSupported;
}

return .{};
}
};

pub const HTMLDListElement = struct {
Expand Down Expand Up @@ -1356,3 +1374,7 @@ test "Browser: HTML.HtmlScriptElement" {
test "Browser: HTML.HtmlSlotElement" {
try testing.htmlRunner("html/slot.html");
}

test "Browser: HTML.HTMLCanvasElement" {
try testing.htmlRunner("html/canvas.html");
}
1 change: 1 addition & 0 deletions src/browser/js/types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const Interfaces = generate.Tuple(.{
@import("../xhr/xhr.zig").Interfaces,
@import("../navigation/root.zig").Interfaces,
@import("../file/root.zig").Interfaces,
@import("../canvas/root.zig").Interfaces,
@import("../xhr/form_data.zig").Interfaces,
@import("../xmlserializer/xmlserializer.zig").Interfaces,
@import("../fetch/fetch.zig").Interfaces,
Expand Down
11 changes: 11 additions & 0 deletions src/tests/html/canvas.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<script src="../testing.js"></script>

<script id=canvas>
const element = document.createElement("canvas");
const ctx = element.getContext("2d");
testing.expectEqual(true, ctx instanceof CanvasRenderingContext2D);
// We can't really test this but let's try to call it.
ctx.fillRect(0, 0, 0, 0);
testing.expectEqual("", ctx.fillStyle);
</script>