ImageData: prefer new typed array type

This commit is contained in:
Halil Durak
2026-02-17 01:45:40 +03:00
parent c30207ac63
commit 9a4cebaa1b

View File

@@ -29,8 +29,7 @@ const Page = @import("../Page.zig");
const ImageData = @This(); const ImageData = @This();
_width: u32, _width: u32,
_height: u32, _height: u32,
/// Can be mutated from both Zig and JS ends; see `js.Uint8ClampedArray(.ref)`. _data: js.ArrayBufferRef(.Uint8Clamped),
_data: []u8,
pub const ConstructorSettings = struct { pub const ConstructorSettings = struct {
/// Specifies the color space of the image data. /// Specifies the color space of the image data.
@@ -68,14 +67,14 @@ pub fn constructor(
} }
const size = width * height * 4; const size = width * height * 4;
const mem = try page.arena.alloc(u8, size); //const mem = try page.arena.alloc(u8, size);
// Zero-init since debug mode fills w/ char 170. //// Zero-init since debug mode fills w/ char 170.
@memset(mem, 0); //@memset(mem, 0);
return page._factory.create(ImageData{ return page._factory.create(ImageData{
._width = width, ._width = width,
._height = height, ._height = height,
._data = mem, ._data = page.js.createTypedArray(.Uint8Clamped, size),
}); });
} }
@@ -95,8 +94,8 @@ pub fn getColorSpace(_: *const ImageData) String {
return comptime .wrap("srgb"); return comptime .wrap("srgb");
} }
pub fn getData(self: *const ImageData) js.Uint8ClampedArray(.ref) { pub fn getData(self: *const ImageData) js.ArrayBufferRef(.Uint8Clamped) {
return .{ .values = self._data }; return self._data;
} }
pub const JsApi = struct { pub const JsApi = struct {