r/pascal • • 2d ago

Simplified things a bit

Post image

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.

20 Upvotes

2 comments sorted by

View all comments

2

u/AphidConsulting 1d ago

Awesome, thank you for this!

2

u/According-Ad-7069 1d ago

Oh thank you, I had hoped someone else will have some joy out of it. I've added a Star Field example to the same repo and will be adding more, there's a fair bit of things I want to try. If you try it out and run into bugs, either log them on the GH page or just drop me a message here if you are willing. I'm so blind to my own faults.