Jump to content

Template:WebGL2Canvas/doc: Difference between revisions

From Apogea Wiki
Dane (talk | contribs)
Fix code blocks - use pre instead of syntaxhighlight (via update-page on MediaWiki MCP Server)
Dane (talk | contribs)
Add scene.container to docs (via update-page on MediaWiki MCP Server)
 
Line 72: Line 72:
=== Context Object ===
=== Context Object ===
The <code>scene</code> object also provides:
The <code>scene</code> object also provides:
* <code>scene.container</code> - the mount div element (useful for adding overlays or UI)
* <code>scene.canvas</code> - the canvas DOM element
* <code>scene.canvas</code> - the canvas DOM element
* <code>scene.gl</code> - the WebGL2 rendering context
* <code>scene.gl</code> - the WebGL2 rendering context

Latest revision as of 15:53, 1 February 2026

Embeds a WebGL2 canvas that loads a scene from the WebGL: namespace.

{{WebGL2Canvas|SceneName}}

Or with named parameters:

{{WebGL2Canvas
|scene=SceneName
|width=100%
|height=400
|min-width=800
|min-height=600
}}

Parameters

[edit source]
Parameter Default Description
scene or 1 (required) Name of the scene to load from WebGL:SceneName
width 100% Canvas width. Accepts pixels (800) or percentage (100%)
height 100% Canvas height. Accepts pixels (600) or percentage (100%)
min-width 800 Minimum width in pixels (used when width is a percentage)
min-height 600 Minimum height in pixels (used when height is a percentage)

Writing Scenes

[edit source]

Scene scripts are stored in the WebGL: namespace (e.g., WebGL:Test). Only users in the webgl-developer group can edit this namespace.

Scenes use instance mode - define lifecycle methods on the scene object:

scene.init = function(gl, canvas) {
  // Called once after script loads
  // Set up shaders, buffers, textures
  gl.clearColor(0.1, 0.1, 0.15, 1.0);
};

scene.render = function(gl, time) {
  // Called every frame
  // time = seconds since scene started
  gl.clear(gl.COLOR_BUFFER_BIT);
};

scene.resize = function(gl, width, height) {
  // Called when canvas size changes
  gl.viewport(0, 0, width, height);
};

scene.cleanup = function(gl) {
  // Optional: called when canvas is removed
  // Free resources here
};

Lifecycle Methods

[edit source]
Method Arguments Description
init gl, canvas Called once after script loads. Set up WebGL resources.
render gl, time Called every frame. time is seconds elapsed.
resize gl, width, height Called when canvas dimensions change.
cleanup gl Called when element is removed. Free resources.

Context Object

[edit source]

The scene object also provides:

  • scene.container - the mount div element (useful for adding overlays or UI)
  • scene.canvas - the canvas DOM element
  • scene.gl - the WebGL2 rendering context

You can store custom state on the scene object (e.g., scene.program, scene.vao).

Examples

[edit source]

Fixed size canvas:

{{WebGL2Canvas|Test|width=800|height=600}}

Full-width responsive canvas:

{{WebGL2Canvas|Test|width=100%|height=400}}