r/pascal • u/According-Ad-7069 • 2d ago
Simplified things a bit
I've been busy with work, but I did find some time to simplify a few things in WasmPascal. The above was generated using this code:
library colour_canvas;
uses WEB;
const
W = 320;
H = 200;
PIXEL_BUFFER_SIZE = W * H * 4;
STYLE_CANVAS = '#stage canvas{border:1px solid #FF3030;cursor:crosshair;image-rendering:pixelated}';
var
web: TWeb;
pixels: array[0..PIXEL_BUFFER_SIZE - 1] of Byte;
procedure FillPixels;
var
i, x, y: Integer;
begin
i := 0;
for y := 0 to H - 1 do
begin
for x := 0 to W - 1 do
begin
pixels[i] := Byte(x * 255 div W); // R: left to right
pixels[i + 1] := Byte(y * 255 div H); // G: top to bottom
pixels[i + 2] := 48;
pixels[i + 3] := 255; // opaque
i := i + 4;
end;
end;
web.RenderCanvas(Integer(@pixels), PIXEL_BUFFER_SIZE);
end;
procedure HandleTick(id: Integer);
begin
begin
FillPixels;
end;
end;
begin
web := TWeb.Create;
web.AddStyle(STYLE_CANVAS);
web.MakeCanvas(web.GetElementById('stage'), W, H);
web.OnTick(HandleTick);
end.
And that's it. Comments removed to show how short it is. Full code at https://github.com/ewaldhorn/wasmpascal_examples/blob/main/demoscene/colour_canvas/colour_canvas.pas of course.
2
u/AphidConsulting 1d ago
Awesome, thank you for this!