www.xbdev.net
xbdev - software development
Friday February 6, 2026
Home | Contact | Support | WebGPU Graphics and Compute ... | Neural Networks and WebGPU Compute.. Learning from Data.....
     
 

Neural Networks and WebGPU Compute..

Learning from Data.....

 


Inverse Kinematics (2 Links)



• Network (6-10-2) - input is base (x,y), end eff pos(x,y), target pos(x,y) and output angles (ang0, ang1)


IK output for the neural network - end-effector follows the mouse cursor around the screen.
IK output for the neural network - end-effector follows the mouse cursor around the screen.


Complete Code



await xinitialize( {layers:[6,10,2], build:'cpu'learningrate:0.2} );

let div document.createElement('div');
document.body.appendChilddiv );
div.innerHTML = `
<canvas id="mycanvas" width="512" height="512" style="border:1px solid #ff0000;"> </canvas>
<br><br>
<div id="counter">Iteration:</div>
<br>
<div id="ideal">Analytic Angles:</div>
`;

var 
canvas      null;
var 
context     null;
var 
iteration   0;    
var 
mousePos    = [];
var 
centerPos   = [];
var 
linkAngles  = [00];
var 
linkLengths = [125125];
const 
PI2         Math.PI 2;
    
document.onmousemove handleMouseMove;
function 
handleMouseMove(event) {
    
event event || window.event// IE-ism
    
    
canvas    canvas || document.getElementById('mycanvas');
    
context   context || canvas.getContext('2d');
    var 
rect canvas.getBoundingClientRect();
    
    
mousePos  = [event.clientX rect.leftevent.clientY rect.top];
    
centerPos = [rect.width*0.5rect.height*0.5];
}

function 
drawLinefromtocol="green"thick=)
{
    
canvas    canvas || document.getElementById('mycanvas');
    
context   context || canvas.getContext('2d');
        
    
context.beginPath(); 
    
context.lineWidth thick
    
context.lineCap 'round'
    
context.strokeStyle col
    
context.moveTo(from[0]-2from[1]-2); 
    
context.lineTo(to[0]-2to[1]-2); 
    
context.stroke(); // Draws the line. 
}

function 
drawCirclepos)
{
    
canvas    canvas || document.getElementById('mycanvas');
    
context   context || canvas.getContext('2d');
        
    
context.fillStyle "#000ff0";
    
// arc(x, y, radius, startAngle, endAngle, anticlockwise)
    
context.beginPath();
    
context.arc(pos[0]-r*0.5pos[1]-r*0.5r0Math.PI 2true); 
    
context.stroke(); // Draws the Circle. 
}

function 
drawLinks(angslengthscol)
{
    
let posbase = [centerPos[0],centerPos[1]]; // starting position
    
    
for (var k=0k<2; ++k)
    {
        
let ang angs[k];
        if ( 
ang ang angs[k-1];
        
        
let posend = [ posbase[0] + Math.cosang )*lengths[k],
                       
posbase[1] + Math.sinang )*lengths[k] ];
                   
        
drawLineposbaseposendcol );
        
        
posbase = [ posend[0], posend[1] ];

    }
}

function 
calcIKAnglesfromtolens )
{
    
let l1 lens[0];
    
let l2 lens[1];
    
    
let x centerPos[0] - mousePos[0];
    
let y centerPos[1] - mousePos[1];
    
    
let tmp = (x*y*l1*l1 l2*l2) / (2*l1*l2);
    if ( 
tmp < -tmp = -1;
    if ( 
tmp >  tmp 1;
    
    
let ang2 Math.acostmp );
    
let ang1 Math.atan2y) - Math.atan(  l2*Math.sin(ang2) / (l1+l2*Math.cos(ang2))  ) + Math.PI;
    
    if ( 
ang1 ang1 ang1 PI2;
    if ( 
ang2 ang2 ang2 PI2;
    
    return [
ang1ang2];
}

async function preview(){
    
// check if data/images is still loading
    
if ( mousePos.length )
    {
        
requestAnimationFrame(preview);
        return;
    }
    
iteration++;
    
    var 
elem document.getElementById('counter');
    
elem.innerHTML "Iteration: " iteration;
        
    
canvas    canvas || document.getElementById('mycanvas');
    
context   context || canvas.getContext('2d');
        
    
context.clearRect(00context.canvas.widthcontext.canvas.height);

    
drawCirclemousePos);
    
drawCirclecenterPos);
    
drawLinecenterPosmousePos"blue");
    
    var 
idealAngles calcIKAnglescenterPosmousePoslinkLengths );
    
drawLinksidealAngleslinkLengths"green" );
  
    
elem document.getElementById('ideal');
    
elem.innerHTML "Ideal: " idealAngles[0] + ", " idealAngles[1];
    
    
//console.log( idealAngles );
    //console.log( mousePos );
  
    
var datain = [ mousePos[0]/512,    mousePos[1]/512
                   
centerPos[0]/512,   centerPos[1]/512,
                   
linkLengths[0]/PI2linkLengths[1]/PI2 ];
                   
    var 
result await xactivatedatain ); 
    
    var 
dataout = [ result[0]*PI2result[1]*PI2 ];
    
    
drawLinksdataoutlinkLengths"purple" );
    
  
    
requestAnimationFrame(iterate);
}

/*
 Keep training the neural network to improve its accuracy
 */
async function iterate(){

    for (
let r=0r<100; ++r)
    {
        
let rp = [ Math.random()*512Math.random()*512 ];
        
        var 
datain = [ rp[0]/512,    rp[1]/512
                       
centerPos[0]/512,   centerPos[1]/512,
                       
linkLengths[0]/PI2linkLengths[1]/PI2 ];
        
        var 
dataout calcIKAnglescenterPosrplinkLengths );

        
dataout = [ dataout[0]/PI2dataout[1]/PI2 ];
        
        var 
dynamicRate =  .01/(1+.0005*iteration);
        
await xactivatedatain ); 
        
await xpropagate(dynamicRatedataout );
    }

    
preview();
}
    
preview();



Resources and Links


• WebGPU Lab Demo [LINK]





















WebGPU by Example: Fractals, Image Effects, Ray-Tracing, Procedural Geometry, 2D/3D, Particles, Simulations WebGPU Compute graphics and animations using the webgpu api 12 week course kenwright learn webgpu api kenwright programming compute and graphics applications with html5 and webgpu api kenwright real-time 3d graphics with webgpu kenwright webgpu api develompent a quick start guide kenwright webgpu by example 2022 kenwright webgpu gems kenwright webgpu interactive compute and graphics visualization cookbook kenwright wgsl webgpu shading language cookbook kenwright wgsl webgpugems shading language cookbook kenwright



 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2025 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.