Skip to content

Commit 4cf61d1

Browse files
committed
initial dummy canvas
1 parent 47ceabc commit 4cf61d1

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (C) 2023-2025 Lightpanda (Selecy SAS)
2+
//
3+
// Francis Bouvier <[email protected]>
4+
// Pierre Tachoire <[email protected]>
5+
//
6+
// This program is free software: you can redistribute it and/or modify
7+
// it under the terms of the GNU Affero General Public License as
8+
// published by the Free Software Foundation, either version 3 of the
9+
// License, or (at your option) any later version.
10+
//
11+
// This program is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
// GNU Affero General Public License for more details.
15+
//
16+
// You should have received a copy of the GNU Affero General Public License
17+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
19+
/// This class doesn't implement a `constructor`.
20+
/// It can be obtained with a call to `HTMLCanvasElement#getContext`.
21+
/// https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D
22+
const CanvasRenderingContext2D = @This();
23+
24+
pub fn _fillRect(x: f64, y: f64, width: f64, height: f64) void {
25+
_ = x;
26+
_ = y;
27+
_ = width;
28+
_ = height;
29+
}
30+
31+
pub fn get_fillStyle(_: *const CanvasRenderingContext2D) []const u8 {
32+
return "";
33+
}
34+
35+
pub fn set_fillStyle(_: *const CanvasRenderingContext2D, _: []const u8) void {}

src/browser/canvas/root.zig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//! Canvas API.
2+
//! https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API
3+
4+
pub const Interfaces = .{
5+
@import("./CanvasRenderingContext2D.zig"),
6+
};

src/browser/html/elements.zig

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const DataSet = @import("DataSet.zig");
3232

3333
const StyleSheet = @import("../cssom/StyleSheet.zig");
3434
const CSSStyleDeclaration = @import("../cssom/CSSStyleDeclaration.zig");
35+
const CanvasRenderingContext2D = @import("../canvas/CanvasRenderingContext2D.zig");
3536

3637
// HTMLElement interfaces
3738
pub const Interfaces = .{
@@ -487,6 +488,23 @@ pub const HTMLCanvasElement = struct {
487488
pub const Self = parser.Canvas;
488489
pub const prototype = *HTMLElement;
489490
pub const subtype = .node;
491+
492+
/// This should be a union once we support other context types.
493+
const ContextAttributes = struct {
494+
alpha: bool,
495+
color_space: []const u8 = "srgb",
496+
};
497+
498+
pub fn _getContext(
499+
ctx_type: []const u8,
500+
_: ?ContextAttributes,
501+
) !CanvasRenderingContext2D {
502+
if (!std.mem.eql(u8, ctx_type, "2d")) {
503+
return error.NotSupported;
504+
}
505+
506+
return .{};
507+
}
490508
};
491509

492510
pub const HTMLDListElement = struct {
@@ -1356,3 +1374,7 @@ test "Browser: HTML.HtmlScriptElement" {
13561374
test "Browser: HTML.HtmlSlotElement" {
13571375
try testing.htmlRunner("html/slot.html");
13581376
}
1377+
1378+
test "Browser: HTML.HTMLCanvasElement" {
1379+
try testing.htmlRunner("html/canvas.html");
1380+
}

src/browser/js/types.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const Interfaces = generate.Tuple(.{
1818
@import("../xhr/xhr.zig").Interfaces,
1919
@import("../navigation/root.zig").Interfaces,
2020
@import("../file/root.zig").Interfaces,
21+
@import("../canvas/root.zig").Interfaces,
2122
@import("../xhr/form_data.zig").Interfaces,
2223
@import("../xmlserializer/xmlserializer.zig").Interfaces,
2324
@import("../fetch/fetch.zig").Interfaces,

src/tests/html/canvas.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<script src="../testing.js"></script>
3+
4+
<script id=canvas>
5+
const element = document.createElement("canvas");
6+
const ctx = element.getContext("2d");
7+
testing.expectEqual(true, ctx instanceof CanvasRenderingContext2D);
8+
// We can't really test this but let's try to call it.
9+
ctx.fillRect(0, 0, 0, 0);
10+
testing.expectEqual("", ctx.fillStyle);
11+
</script>

0 commit comments

Comments
 (0)