### React Component Integration Example Source: https://context7.com/anime4kwebboost/anime4k-webgpu/llms.txt This example demonstrates how to integrate Anime4K-WebGPU into a React application using functional components and the `useEffect` hook for initialization. ```APIDOC ## React Component Integration Example ### Description Provides a complete example of integrating Anime4K-WebGPU within a React application. It utilizes `useRef` for accessing DOM elements and `useEffect` to manage the WebGPU rendering pipeline initialization. ### Method Component rendering with `render` function called within `useEffect`. ### Endpoint N/A (React Component) ### Parameters No direct API parameters, but the component manages its own state and refs. ### Request Example ```javascript import React, { useRef, useEffect } from 'react'; import { CNNx2UL, GANUUL, render } from 'anime4k-webgpu'; export const VideoUpscaler: React.FC = () => { const canvasRef = useRef(null); const videoRef = useRef(null); useEffect(() => { const video = videoRef.current; const canvas = canvasRef.current; if (!video || !canvas) return; video.muted = true; video.src = '/my-video.mp4'; async function init() { await render({ video, canvas, pipelineBuilder: (device, inputTexture) => { const upscale = new CNNx2UL({ device, inputTexture, }); const restore = new GANUUL({ device, inputTexture: upscale.getOutputTexture(), }); return [upscale, restore]; }, }); await video.play(); } init(); }, []); return (
); }; ``` ### Response #### Success Response (Rendered Video) Displays a canvas element with the video upscaled and processed by Anime4K-WebGPU. ``` -------------------------------- ### React Component Integration Example Source: https://context7.com/anime4kwebboost/anime4k-webgpu/llms.txt Provides a complete example of integrating Anime4K-WebGPU into a React application using `useRef` and `useEffect` hooks. It sets up video playback and upscaling using CNNx2UL and GANUUL. ```typescript import React, { useRef, useEffect } from 'react'; import { CNNx2UL, GANUUL, render } from 'anime4k-webgpu'; export const VideoUpscaler: React.FC = () => { const canvasRef = useRef(null); const videoRef = useRef(null); useEffect(() => { const video = videoRef.current; const canvas = canvasRef.current; if (!video || !canvas) return; video.muted = true; video.src = '/my-video.mp4'; async function init() { await render({ video, canvas, pipelineBuilder: (device, inputTexture) => { const upscale = new CNNx2UL({ device, inputTexture, }); const restore = new GANUUL({ device, inputTexture: upscale.getOutputTexture(), }); return [upscale, restore]; }, }); await video.play(); } init(); }, []); return (
); }; ``` -------------------------------- ### Render Wrapper Setup with Preset Mode - TypeScript Source: https://github.com/anime4kwebboost/anime4k-webgpu/blob/main/README.md Shows how to utilize the `render` function with a preset Anime4K mode (e.g., `ModeA`) for video enhancement. This approach requires specifying the native video dimensions and the target canvas dimensions to configure the correct pipeline combination for upscaling. ```typescript import { ModeA, render } from 'anime4k-webgpu'; await render({ video, canvas, // inputTexture(video) -> Mode A -> (canvas) pipelineBuilder: (device, inputTexture) => { const preset = new ModeA({ device, inputTexture, nativeDimensions: { width: video.videoWidth, height: video.videoHeight, }, targetDimensions: { width: canvas.width, height: canvas.height, } }) return [preset]; }, }); ``` -------------------------------- ### Render Wrapper Setup with Custom Pipeline - TypeScript Source: https://github.com/anime4kwebboost/anime4k-webgpu/blob/main/README.md Demonstrates how to use the `render` function to set up a custom video rendering pipeline using WebGPU. It takes source video and destination canvas elements, and a pipeline builder function to define the sequence of Anime4K processing steps (e.g., upscaling and restoration). ```typescript import { CNNx2UL, GANUUL, render } from 'anime4k-webgpu'; await render({ // your source video HTMLElement video, // your render destination canvas HTMLElement canvas, // your function to build custom pipeline // return all pipelines in order of execution // e.g. inputTexture(video) -> CNNx2UL -> GANUUL -> (canvas) pipelineBuilder: (device, inputTexture) => { const upscale = new CNNx2UL({ device, inputTexture, }); const restore = new GANUUL({ device, inputTexture: upscale.getOutputTexture(), }); return [upscale, restore]; }, }); ``` -------------------------------- ### Preset Mode Integration API Source: https://context7.com/anime4kwebboost/anime4k-webgpu/llms.txt This section details how to use predefined optimization modes for automatic pipeline configuration based on input and target resolutions. This simplifies setup for common upscaling tasks. ```APIDOC ## Preset Mode Integration API ### Description Integrate predefined optimization modes for automatic pipeline configuration. These presets simplify the setup by automatically determining the necessary processing steps based on input and target resolutions. ### Method `render(options)` with a preset `pipelineBuilder` ### Endpoint N/A (Client-side JavaScript function) ### Parameters #### Request Body - **video** (HTMLVideoElement) - Required - The video element to process. - **canvas** (HTMLCanvasElement) - Required - The canvas element to render the output to. - **pipelineBuilder** (function) - Required - A function that returns an array containing a single preset pipeline instance. - Example Preset: `ModeA` - **device** (GPUDevice) - The WebGPU device. - **inputTexture** (GPUTexture) - The input texture from the video frame. - **nativeDimensions** (object) - Required - The original dimensions of the video. - **width** (number) - The native width. - **height** (number) - The native height. - **targetDimensions** (object) - Required - The desired dimensions for the output canvas. - **width** (number) - The target width. - **height** (number) - The target height. ### Request Example ```javascript import { ModeA, render } from 'anime4k-webgpu'; const video = document.querySelector('video'); const canvas = document.querySelector('canvas'); canvas.width = 1920; canvas.height = 1080; await render({ video, canvas, pipelineBuilder: (device, inputTexture) => { const preset = new ModeA({ device, inputTexture, nativeDimensions: { width: video.videoWidth, height: video.videoHeight, }, targetDimensions: { width: canvas.width, height: canvas.height, } }); return [preset]; }, }); await video.play(); ``` ### Response #### Success Response (void) Processes and renders video frames to the specified canvas using the selected preset mode. ``` -------------------------------- ### Integrating Anime4K Pipeline into Existing WebGPU Setup - TypeScript Source: https://github.com/anime4kwebboost/anime4k-webgpu/blob/main/README.md Illustrates how to integrate an Anime4K pipeline, such as `CNNx2UL`, into an existing WebGPU rendering process. This involves importing the pipeline class, instantiating it with the device and input texture, and then using its output texture and `pass` method within the WebGPU command encoder. ```typescript // +++ import CNNx2UL, one of the CNN upscale pipeline +++ import { Anime4KPipeline, CNNx2UL } from 'anime4k-webgpu'; // your original texture to be processed const inputTexture: GPUTexture; // +++ instantiate pipeline +++ const pipeline: Anime4KPipeline = new CNNx2UL({ device, inputTexture }); // bind (upscaled) output texture wherever you want e.g. render pipeline const renderBindGroup = device.createBindGroup({ ... entries: [{ binding: 0, // +++ use pipeline.getOutputTexture() instead of inputTexture +++ resource: pipeline.getOutputTexture().createView(), }] }); function frame() { const commandEncoder: GPUCommandEncoder; // +++ inject commands into the encoder +++ pipeline.pass(commandEncoder); // begin other render pass... } ``` -------------------------------- ### Render Wrapper Function API Source: https://context7.com/anime4kwebboost/anime4k-webgpu/llms.txt This section describes the basic render function for integrating Anime4K-WebGPU. It handles WebGPU setup and the video-to-canvas pipeline, allowing custom shader configurations. ```APIDOC ## Render Wrapper Function API ### Description Provides a simple rendering function that manages WebGPU initialization and the video-to-canvas pipeline. It allows for custom shader configurations through a `pipelineBuilder`. ### Method `render(options)` ### Endpoint N/A (Client-side JavaScript function) ### Parameters #### Request Body - **video** (HTMLVideoElement) - Required - The video element to process. - **canvas** (HTMLCanvasElement) - Required - The canvas element to render the output to. - **pipelineBuilder** (function) - Required - A function that takes `device` and `inputTexture` as arguments and returns an array of `Anime4K` pipeline stages. - **device** (GPUDevice) - The WebGPU device. - **inputTexture** (GPUTexture) - The input texture from the video frame. ### Request Example ```javascript import { CNNx2UL, GANUUL, render } from 'anime4k-webgpu'; const video = document.querySelector('video'); const canvas = document.querySelector('canvas'); await render({ video, canvas, pipelineBuilder: (device, inputTexture) => { const upscale = new CNNx2UL({ device, inputTexture, }); const restore = new GANUUL({ device, inputTexture: upscale.getOutputTexture(), }); return [upscale, restore]; }, }); video.play(); ``` ### Response #### Success Response (void) This function does not return a value but processes and renders video frames to the specified canvas. #### Response Example N/A ``` -------------------------------- ### Error Handling and Browser Compatibility (TypeScript) Source: https://context7.com/anime4kwebboost/anime4k-webgpu/llms.txt Demonstrates robust initialization of anime4k-webgpu with error handling for WebGPU support and adapter requests. Includes a fallback mechanism for unsupported browsers, switching to regular video playback. ```typescript import { render, CNNx2M } from 'anime4k-webgpu'; async function initializeUpscaling() { try { if (!navigator.gpu) { throw new Error('WebGPU is not supported in this browser'); } const adapter = await navigator.gpu.requestAdapter(); if (!adapter) { throw new Error('Failed to get GPU adapter'); } await render({ video: document.querySelector('video'), canvas: document.querySelector('canvas'), pipelineBuilder: (device, inputTexture) => { return [new CNNx2M({ device, inputTexture })]; }, }); console.log('Anime4K initialized successfully'); } catch (error) { console.error('Initialization failed:', error); // Fallback to regular video playback document.querySelector('video').style.display = 'block'; document.querySelector('canvas').style.display = 'none'; } } initializeUpscaling(); ``` -------------------------------- ### Preset Mode Integration with ModeA Source: https://context7.com/anime4kwebboost/anime4k-webgpu/llms.txt Shows how to use the `render` function with a predefined optimization mode (`ModeA`) for automatic pipeline configuration. This is useful for scaling to specific target resolutions. ```typescript import { ModeA, render } from 'anime4k-webgpu'; const video = document.querySelector('video'); const canvas = document.querySelector('canvas'); canvas.width = 1920; canvas.height = 1080; await render({ video, canvas, pipelineBuilder: (device, inputTexture) => { const preset = new ModeA({ device, inputTexture, nativeDimensions: { width: video.videoWidth, height: video.videoHeight, }, targetDimensions: { width: canvas.width, height: canvas.height, } }); return [preset]; }, }); await video.play(); ``` -------------------------------- ### Preset Mode C - Fast Performance Upscaling (TypeScript) Source: https://context7.com/anime4kwebboost/anime4k-webgpu/llms.txt Implements Preset Mode C for performance-optimized, real-time upscaling with minimal overhead using anime4k-webgpu. It uses lighter models for faster processing. Requires device and inputTexture. ```typescript import { ModeC } from 'anime4k-webgpu'; const processor = new ModeC({ device, inputTexture, nativeDimensions: { width: 854, height: 480 }, targetDimensions: { width: 1920, height: 1080 }, }); // ModeC uses lighter models for faster processing const commandEncoder = device.createCommandEncoder(); processor.pass(commandEncoder); device.queue.submit([commandEncoder.finish()]); ``` -------------------------------- ### Multi-Pipeline Chain for Video Enhancement (TypeScript) Source: https://context7.com/anime4kwebboost/anime4k-webgpu/llms.txt Demonstrates chaining multiple Anime4K pipelines (denoising, deblurring, restoration, upscaling) to create a sophisticated video enhancement workflow. Each pipeline takes the output of the previous one as its input, allowing for complex image processing sequences. Requires a WebGPU device and initial input texture. ```typescript import { DoG, BilateralMean, CNNx2VL, CNNVL } from 'anime4k-webgpu'; // Build processing chain const denoise = new BilateralMean({ device, inputTexture, }); denoise.updateParam('strength', 0.15); denoise.updateParam('strength2', 1.5); const deblur = new DoG({ device, inputTexture: denoise.getOutputTexture(), }); deblur.updateParam('strength', 5.0); const restore = new CNNVL({ device, inputTexture: deblur.getOutputTexture(), }); const upscale = new CNNx2VL({ device, inputTexture: restore.getOutputTexture(), }); // Execute all pipelines in order function processFrame() { const commandEncoder = device.createCommandEncoder(); denoise.pass(commandEncoder); deblur.pass(commandEncoder); restore.pass(commandEncoder); upscale.pass(commandEncoder); device.queue.submit([commandEncoder.finish()]); // Use upscale.getOutputTexture() for final rendering } ``` -------------------------------- ### Render Wrapper Function with CNNx2UL and GANUUL Source: https://context7.com/anime4kwebboost/anime4k-webgpu/llms.txt Demonstrates using the `render` function with custom `pipelineBuilder` to chain CNNx2UL and GANUUL for video upscaling and restoration. Requires video and canvas elements as input. ```typescript import { CNNx2UL, GANUUL, render } from 'anime4k-webgpu'; const video = document.querySelector('video'); const canvas = document.querySelector('canvas'); await render({ video, canvas, pipelineBuilder: (device, inputTexture) => { const upscale = new CNNx2UL({ device, inputTexture, }); const restore = new GANUUL({ device, inputTexture: upscale.getOutputTexture(), }); return [upscale, restore]; }, }); video.play(); ``` -------------------------------- ### Low-Level WebGPU Pipeline Integration (TypeScript) Source: https://context7.com/anime4kwebboost/anime4k-webgpu/llms.txt Integrates Anime4K pipelines directly into existing WebGPU render pipelines. This approach allows for advanced use cases by injecting custom compute passes into the rendering workflow. It requires an initialized WebGPU device and input texture. ```typescript import { Anime4KPipeline, CNNx2UL } from 'anime4k-webgpu'; // Existing WebGPU setup const adapter = await navigator.gpu.requestAdapter(); const device = await adapter.requestDevice(); // Your original texture to be processed const inputTexture = device.createTexture({ size: [1280, 720, 1], format: 'rgba16float', usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST, }); // Instantiate the upscaling pipeline const pipeline: Anime4KPipeline = new CNNx2UL({ device, inputTexture }); // Create bind group using pipeline output const renderBindGroup = device.createBindGroup({ layout: renderBindGroupLayout, entries: [{ binding: 0, resource: pipeline.getOutputTexture().createView(), }] }); // In your render loop function renderFrame() { const commandEncoder = device.createCommandEncoder(); // Inject Anime4K compute pass pipeline.pass(commandEncoder); // Continue with render pass const passEncoder = commandEncoder.beginRenderPass({ colorAttachments: [{ view: context.getCurrentTexture().createView(), loadOp: 'clear', storeOp: 'store', }], }); passEncoder.setPipeline(renderPipeline); passEncoder.setBindGroup(0, renderBindGroup); passEncoder.draw(6); passEncoder.end(); device.queue.submit([commandEncoder.finish()]); requestAnimationFrame(renderFrame); } ``` -------------------------------- ### Updating Anime4K Pipeline Parameters - TypeScript Source: https://github.com/anime4kwebboost/anime4k-webgpu/blob/main/README.md Demonstrates how to dynamically update adjustable parameters of an Anime4K pipeline during runtime. The `updateParam` method allows modifying parameters like 'strength' for effects such as deblurring, with the changes applied on the next rendering cycle. ```typescript pipeline.updateParam('strength', 3.0); ``` -------------------------------- ### Deblur Pipeline with Dynamic Parameters (TypeScript) Source: https://context7.com/anime4kwebboost/anime4k-webgpu/llms.txt Applies a Difference of Gaussians (DoG) filter for deblurring with an adjustable strength parameter. The strength can be updated dynamically, allowing for real-time adjustments based on user input or other factors. This requires a WebGPU device and input texture. ```typescript import { DoG } from 'anime4k-webgpu'; const deblur = new DoG({ device, inputTexture, }); // Set deblur strength (0 = no deblur, higher = stronger) deblur.updateParam('strength', 7.0); // In render loop function frame() { const commandEncoder = device.createCommandEncoder(); deblur.pass(commandEncoder); device.queue.submit([commandEncoder.finish()]); } // Dynamically adjust strength document.querySelector('#strength-slider').addEventListener('input', (e) => { const strength = parseFloat(e.target.value); deblur.updateParam('strength', strength); }); ``` -------------------------------- ### Preset Mode B - High Quality Upscaling (TypeScript) Source: https://context7.com/anime4kwebboost/anime4k-webgpu/llms.txt Implements Preset Mode B for high-quality upscaling with denoising using the anime4k-webgpu library. It requires device, inputTexture, and dimensions for processing. Outputs an enhanced texture. ```typescript import { ModeB } from 'anime4k-webgpu'; const processor = new ModeB({ device, inputTexture, nativeDimensions: { width: 640, height: 360 }, targetDimensions: { width: 1920, height: 1080 }, }); const commandEncoder = device.createCommandEncoder(); processor.pass(commandEncoder); const finalTexture = processor.getOutputTexture(); device.queue.submit([commandEncoder.finish()]); ``` -------------------------------- ### Denoise Pipeline with Bilateral Filter (TypeScript) Source: https://context7.com/anime4kwebboost/anime4k-webgpu/llms.txt Applies bilateral mean denoising, a technique that smooths images while preserving edges. Configurable parameters include intensity sigma ('strength') for color smoothing and spatial sigma ('strength2') for the spatial reach of the filter. This pipeline requires a WebGPU device and input texture. ```typescript import { BilateralMean } from 'anime4k-webgpu'; const denoise = new BilateralMean({ device, inputTexture, }); // Set intensity sigma (controls color smoothing) // Lower values = preserve more detail, higher = more smoothing denoise.updateParam('strength', 0.2); // Set spatial sigma (controls spatial reach of filter) // Higher values = smoother colors across wider areas denoise.updateParam('strength2', 2.0); // In render loop const commandEncoder = device.createCommandEncoder(); denoise.pass(commandEncoder); const outputTexture = denoise.getOutputTexture(); ``` -------------------------------- ### CNN-Based Upscaling (2x) (TypeScript) Source: https://context7.com/anime4kwebboost/anime4k-webgpu/llms.txt Performs ultra-quality 2x upscaling using a convolutional neural network (CNN). This pipeline takes an input texture and produces an output texture with double the dimensions and enhanced detail. Requires a WebGPU device and input texture. ```typescript import { CNNx2UL } from 'anime4k-webgpu'; const upscaler = new CNNx2UL({ device, inputTexture, // 1280x720 }); const outputTexture = upscaler.getOutputTexture(); // 2560x1440 function render() { const commandEncoder = device.createCommandEncoder(); upscaler.pass(commandEncoder); device.queue.submit([commandEncoder.finish()]); requestAnimationFrame(render); } ``` -------------------------------- ### GAN-Based Restoration (TypeScript) Source: https://context7.com/anime4kwebboost/anime4k-webgpu/llms.txt Applies generative adversarial network (GAN) based restoration to enhance image details. This pipeline takes an input texture and produces an output texture of the same dimensions but with improved visual fidelity. Requires a WebGPU device and input texture. ```typescript import { GANUUL } from 'anime4k-webgpu'; const restore = new GANUUL({ device, inputTexture, }); // Output has same dimensions but enhanced detail const outputTexture = restore.getOutputTexture(); const commandEncoder = device.createCommandEncoder(); restore.pass(commandEncoder); device.queue.submit([commandEncoder.finish()]); ``` -------------------------------- ### GAN 4x Ultra Upscaling (TypeScript) Source: https://context7.com/anime4kwebboost/anime4k-webgpu/llms.txt Performs maximum quality upscaling using a GAN model for a 4x resolution increase with anime4k-webgpu. It takes the device and inputTexture to generate a significantly upscaled output. ```typescript import { GANx4UUL } from 'anime4k-webgpu'; const massiveUpscale = new GANx4UUL({ device, inputTexture, // 480x270 }); // Output will be 1920x1080 (4x in each dimension) const outputTexture = massiveUpscale.getOutputTexture(); const commandEncoder = device.createCommandEncoder(); massiveUpscale.pass(commandEncoder); device.queue.submit([commandEncoder.finish()]); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.