Creates a WebGL context without a window for headless rendering and server-side graphics
Overall
score
96%
Build a headless rendering utility that can render different colored rectangles to specific regions of a canvas, with support for clipping regions.
Your implementation should support rendering to multiple independent rectangular regions on a single canvas. Each region should be able to have its own content rendered independently, and content should not bleed outside region boundaries.
Implement the following functionality:
Your implementation should satisfy the following test cases:
Given a 800x600 canvas, when clearing the entire canvas to red (RGB: 255, 0, 0), then all pixels should be red @test
Given a 800x600 canvas, when rendering to region (100, 100, 200, 150) with green color (RGB: 0, 255, 0) and the rest with blue (RGB: 0, 0, 255), then pixels at (150, 150) should be green and pixels at (50, 50) should be blue @test
Given a 800x600 canvas with three non-overlapping regions: region A (0, 0, 200, 200) cleared to red, region B (200, 0, 200, 200) cleared to green, and region C (400, 0, 200, 200) cleared to blue, then sampling pixels from each region should return the correct colors @test
Given a canvas where region (50, 50, 100, 100) is active, when clearing to white (RGB: 255, 255, 255), then only pixels within (50, 50, 100, 100) should be white and pixels outside should remain unchanged @test
@generates
/**
* Creates a new multi-region renderer
* @param {number} width - Canvas width in pixels
* @param {number} height - Canvas height in pixels
* @returns {object} Renderer instance
*/
function createRenderer(width, height) {
// IMPLEMENTATION HERE
}
/**
* Sets the active rendering region
* @param {object} renderer - The renderer instance
* @param {number} x - Region x coordinate (left)
* @param {number} y - Region y coordinate (top)
* @param {number} width - Region width
* @param {number} height - Region height
*/
function setRegion(renderer, x, y, width, height) {
// IMPLEMENTATION HERE
}
/**
* Clears the current region to the specified color
* @param {object} renderer - The renderer instance
* @param {number} r - Red component (0-255)
* @param {number} g - Green component (0-255)
* @param {number} b - Blue component (0-255)
* @param {number} a - Alpha component (0-255)
*/
function clearRegion(renderer, r, g, b, a) {
// IMPLEMENTATION HERE
}
/**
* Retrieves pixel data from the canvas
* @param {object} renderer - The renderer instance
* @returns {Uint8Array} Pixel data in RGBA format
*/
function getPixels(renderer) {
// IMPLEMENTATION HERE
}
/**
* Cleans up renderer resources
* @param {object} renderer - The renderer instance
*/
function destroyRenderer(renderer) {
// IMPLEMENTATION HERE
}
module.exports = {
createRenderer,
setRegion,
clearRegion,
getPixels,
destroyRenderer
};Provides headless WebGL rendering capabilities.
Install with Tessl CLI
npx tessl i tessl/npm-gldocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10