Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can check a working prototype here: <a href="http://jsbin.com/ipaliq/edit" rel="nofollow noreferrer">http://jsbin.com/ipaliq/edit</a></p> <p>I bet there's ton's of room for optimization and better/faster algorithms, but I hope this proof of concepts will point you in the right direction.</p> <p>The basic algorithm goes through each pixel of the original image and calculates its new position if it was subject to an spherical deformation.</p> <p>Here's the result:</p> <p><img src="https://i.stack.imgur.com/KxqDQ.png" alt="Sphere"></p> <p>Code:</p> <pre><code>var w = 150, h = 150; var canvas=document.getElementById("myCanvas"); var ctx=canvas.getContext("2d"); ctx.fillStyle="red"; ctx.fillRect(0,0,w,h); //-- Init Canvas var initCanvas = function() { var imgData=ctx.getImageData(0,0,w,h); for (i=0; i&lt;imgData.width*imgData.height*4;i+=4) { imgData.data[i]=i%150*1.5; imgData.data[i+1]=i%150*1.5; imgData.data[i+2]=(i/imgData.width)%150*1.5; imgData.data[i+3]=255; } ctx.putImageData(imgData,0,0); }; initCanvas(); var doSpherize = function() { var refractionIndex = 0.5; // [0..1] //refraction index of the sphere var radius = 75; var radius2 = radius * radius; var centerX = 75; var centerY = 75; //center of the sphere var origX = 0; var origY = 0; for (x=0; x&lt;w;x+=1) for (y=0; y&lt;h;y+=1) { var distX = x - centerX; var distY = y - centerY; var r2 = distX * distX + distY * distY; origX = x; origY = y; if ( r2 &gt; 0.0 &amp;&amp; r2 &lt; radius2 ) { // distance var z2 = radius2 - r2; var z = Math.sqrt(z2); // refraction var xa = Math.asin( distX / Math.sqrt( distX * distX + z2 ) ); var xb = xa - xa * refractionIndex; var ya = Math.asin( distY / Math.sqrt( distY * distY + z2 ) ); var yb = ya - ya * refractionIndex; // displacement origX = origX - z * Math.tan( xb ); origY = origY - z * Math.tan( yb ); } // read var imgData=ctx.getImageData(origX,origY,1,1); // write ctx.putImageData(imgData,x+w,y+h); } }; doSpherize(); </code></pre> <p><strong>Note</strong></p> <p>I would advice agains such an intense operation to be handed by the frontend without webGL pixel shaders. Those are very optimized and will be able to take advantage of GPU accelaration.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload