Template:WebGL2Canvas/doc: Difference between revisions
Add documentation for WebGL2Canvas template (via create-page on MediaWiki MCP Server) |
Fix code blocks - use pre instead of syntaxhighlight (via update-page on MediaWiki MCP Server) |
||
| 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: | ||
< | <pre>scene.init = function(gl, canvas) { | ||
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) { | 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) { | scene.resize = function(gl, width, height) { | ||
// Called when canvas size changes | |||
gl.viewport(0, 0, width, height); | |||
}; | }; | ||
scene.cleanup = function(gl) { | scene.cleanup = function(gl) { | ||
// Optional: called when canvas is removed | |||
// Free resources here | |||
}; | };</pre> | ||
</ | |||
=== Lifecycle Methods === | === Lifecycle Methods === | ||
| Line 85: | Line 79: | ||
== 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]] | ||
Revision as of 15:51, 1 February 2026
Embeds a WebGL2 canvas that loads a scene from the WebGL: namespace.
Usage
{{WebGL2Canvas|SceneName}}
Or with named parameters:
{{WebGL2Canvas
|scene=SceneName
|width=100%
|height=400
|min-width=800
|min-height=600
}}
Parameters
| 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
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
| 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
The scene object also provides:
scene.canvas- the canvas DOM elementscene.gl- the WebGL2 rendering context
You can store custom state on the scene object (e.g., scene.program, scene.vao).
Examples
Fixed size canvas:
{{WebGL2Canvas|Test|width=800|height=600}}
Full-width responsive canvas:
{{WebGL2Canvas|Test|width=100%|height=400}}