Jump to content

Template:WebGL2Canvas/doc: Difference between revisions

From Apogea Wiki
Dane (talk | contribs)
Add documentation for WebGL2Canvas template (via create-page on MediaWiki MCP Server)
 
Dane (talk | contribs)
Add scene.container to docs (via update-page on MediaWiki MCP Server)
 
(One intermediate revision by the same user not shown)
Line 2: Line 2:


== Usage ==
== Usage ==
<pre>
<pre>{{WebGL2Canvas|SceneName}}</pre>
{{WebGL2Canvas|SceneName}}
</pre>


Or with named parameters:
Or with named parameters:
<pre>
<pre>{{WebGL2Canvas
{{WebGL2Canvas
|scene=SceneName
|scene=SceneName
|width=100%
|width=100%
Line 14: Line 11:
|min-width=800
|min-width=800
|min-height=600
|min-height=600
}}
}}</pre>
</pre>


== Parameters ==
== Parameters ==
Line 38: Line 34:
Scenes use instance mode - define lifecycle methods on the <code>scene</code> object:
Scenes use instance mode - define lifecycle methods on the <code>scene</code> object:


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


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


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


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


=== Lifecycle Methods ===
=== Lifecycle Methods ===
Line 78: 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
Line 85: Line 80:
== Examples ==
== Examples ==
Fixed size canvas:
Fixed size canvas:
<pre>
<pre>{{WebGL2Canvas|Test|width=800|height=600}}</pre>
{{WebGL2Canvas|Test|width=800|height=600}}
</pre>


Full-width responsive canvas:
Full-width responsive canvas:
<pre>
<pre>{{WebGL2Canvas|Test|width=100%|height=400}}</pre>
{{WebGL2Canvas|Test|width=100%|height=400}}
</pre>


[[Category:Template documentation]]
[[Category:Template documentation]]

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}}