Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I am aware that this is a PHP related question, but you can achieve nice transparent gradients with use of javascript and html5 canvas element.</p> <p>So I wrote this small script that:</p> <ul> <li>detects browsers that support canvas element, if browser doesn't support canvas (fortunately only a few percent of users are left) then full image is displayed.</li> <li>creates canvas and appends element after image</li> <li>arguments in the create_gradient() function can be changed for custom shapes</li> <li>it works with all popular image formats ( tested with .jpg, .bmp, .gif, .png ) </li> <li>you can add more ‘colorstops’ ( <code>grd.addColorStop()</code> ) to change the flow of the gradient</li> </ul> <p><strong>script</strong></p> <pre><code>window.onload = function() { if ( typeof CanvasRenderingContext2D !== 'function' ) { document.getElementById('gradient-image').style.visibility = "visible"; return; } var image = document.getElementById('gradient-image'); // these are the default values, change them for custom shapes create_gradient( image, image.width/2, image.height/2, image.height/4, image.width/2, image.height/2, image.height/2 ); } function create_gradient( image, start_x, start_y, start_r, end_x, end_y, end_r ){ var canvas = document.createElement('canvas'); var parent = image.parentNode; if ( parent.lastchild == image ) parent.appendChild(canvas); else parent.insertBefore(canvas, image.nextSibling); canvas.width = image.width; canvas.height = image.height; var context = canvas.getContext('2d'); var grd = context.createRadialGradient( start_x, start_y, start_r, end_x, end_y, end_r ); grd.addColorStop(0, 'rgba(0,0,0,1)' ); // grd.addColorStop(0.2, 'rgba(0,0,0,0.8)' ); grd.addColorStop(1, 'rgba(0,0,0,0)' ); context.fillStyle = grd; context.fillRect(0, 0, image.width, image.height); var grd_data = context.getImageData(0, 0, image.width, image.height); context.drawImage( image, 0, 0); var img_data = context.getImageData(0, 0, image.width, image.height); var grd_pixel = grd_data.data; var img_pixel = img_data.data; var length = img_data.data.length for ( i = 3; i &lt; length; i += 4 ) { img_pixel[i] = grd_pixel[i]; } context.putImageData(img_data, 0, 0); } </code></pre> <p><strong>html</strong></p> <pre><code>&lt;img id="gradient-image"src=""&gt; </code></pre> <p><strong>css</strong></p> <pre><code>#gradient-image { position: absolute; visibility: hidden; } </code></pre>
    singulars
    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. 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.
 

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