3

Is it possible to give a glow effect to an image automatically, say using canvas?

jsfiddle

The canvas tag would have to omit the transparent

transparent iphonev

and make it have an outter glow?

outterglow

<canvas id="canvas" width=960 height=960></canvas>
Community
  • 1
  • 1
Armeen Moon
  • 18,061
  • 35
  • 120
  • 233
  • The naive approach would work, though there may be better ways to skin the cat. **(1)** copy the image to a canvas **(2)** set all pixels with a non-zero alpha value to be the glow-colour. **(3)** Perform alpha-based blurring on the canvas produced in steps 1 & 2. (don't touch the colours, only average the alpha values with the 4 immediate neighbours - up,down,left,right) - repeat as required **(4)** draw the original image back onto the canvas. The blurring step will have increased the number of pixels with the glow colour, while simultaneously fading their alpha value to 0 at the edges. – enhzflep Apr 04 '14 at 03:20
  • Update: it does work, though with a problem. I'd forgotten that the blurring operation would _shrink_ the size of the glow. The method constructs a glowing portion that is smaller than the opaque part of the source image. :(. Source available if helpful. – enhzflep Apr 04 '14 at 03:55

1 Answers1

5

Make a canvas path glow by applying a series of overlapping shadows with increasing blur

A Demo: http://jsfiddle.net/m1erickson/Z3Lx2/

enter image description here

You can change the styling of the glow by varying the number of overlays and the blur size.

Example code for a glow effect:

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");

// glow
var glowColor="blue";

ctx.save();
ctx.strokeStyle = glowColor;
ctx.shadowColor = glowColor;
ctx.shadowOffsetX=300;
for (var i = 0; i < 10; i++) {
    ctx.shadowBlur = i * 2;
    ctx.strokeRect(-270, 30, 75, 150);
}
ctx.restore();

To get the outline path of your phone image, you can use the "marching ants" algorithm.

This algorithm will create a path that outlines an image.

In your case you would define the image as all pixels that are not transparent.

Here's a very good implementation of "marching ants" that is used in the excellent d3 library:

https://github.com/d3/d3-plugins/blob/master/geom/contour/contour.js

It's used like this:

DrawImage your phone on the canvas.

// draw the image
// (this time to grab the image's pixel data

ctx.drawImage(img,0,0);

Get the pixel color array from the canvas using ctx.getImageData

// grab the image's pixel data

imgData=ctx.getImageData(0,0,canvas.width,canvas.height);
data=imgData.data;

Define a function that checks the pixel array for non-transparent pixels at any x,y on the canvas.

// This is used by the marching ants algorithm
// to determine the outline of the non-transparent
// pixels on the image

var defineNonTransparent=function(x,y){
    var a=data[(y*cw+x)*4+3];
    return(a>20);
}

Call the contour function:

// call the marching ants algorithm
// to get the outline path of the image
// (outline=outside path of transparent pixels

var points=geom.contour(defineNonTransparent);

Here's an example result:

  • the glow is automatically generated using overlapping shadows

  • the outline path of the phone is calculated using the marching ants algorithm

enter image description here

markE
  • 102,905
  • 11
  • 164
  • 176
  • Marking this correct and doing this as we speak but would you happen to have a working jsfiddle of it? just incase I run into any snags. – Armeen Moon Apr 04 '14 at 17:30
  • 1
    Yep, I made the last image with working example code (just to make sure there were no bugs): http://jsfiddle.net/m1erickson/Rk3rF/ – markE Apr 05 '14 at 03:32