[fpc-pascal] pas2js Webgl unit committed

Michael Van Canneyt michael at freepascal.org
Tue May 1 16:56:41 CEST 2018



On Tue, 1 May 2018, Ryan Joseph wrote:

>
>
>> On May 1, 2018, at 8:28 PM, Michael Van Canneyt <michael at freepascal.org> wrote:
>> 
>> Check the canvas demo (demo/rtl/democanvas2d.pas)
>
> something like this?
>
> var
>  canvas: TJSElement; // What type should this be?
>  gl: TJSWebGLRenderingContext;
> begin
>  canvas := document.createElement('canvas');
>  document.body.appendChild(canvas);
>  gl := canvas.getContext('webgl');

You must do
   gl := TJSWebGLRenderingContext(canvas.getContext('webgl'));

because getContext can return various classes depending on the argument.

>  canvas.width := newWidth;
>  canvas.height := newHeight;
>  gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
>
> Not sure about the type which createElement returns that has the getContext method.

createElement returns guaranteed TJSElement, but normally it will be TJSHTMLCanvasElement
for your call.

So
   canvas: TJSHTMLCanvasElement;

begin
   canvas:=TJSHTMLCanvasElement(document.createElement('canvas'));


> Do you need document.body.appendChild also? Sorry it’s been a long time since I used JS to do even simple things. Never knew it well.

Yes, you need that too to insert the new element in the DOM tree.

Michael.


More information about the fpc-pascal mailing list