javascript - Is it possible to make a gradient-transparent/layer masking image using canvas? -
i've been following lessons transparency , gradients on mozilla site: https://developer.mozilla.org/en-us/docs/web/guide/html/canvas_tutorial/applying_styles_and_colors have not been able figure 1 out.
i know can achieve these effects png image; however, in program working on gradient change according image moved.
here's example of effect i'm looking for. http://home.insightbb.com/~epyonxl1/gradientex.jpg
i've aded code here http://code.google.com/p/canvasimagegradient/ adds drawimagegradient function canvasrenderingcontext2d. can draw image linear or radial gradient. doesn't work in ie, excanvas, due lack of getimagedata/putimagedata support.
the following code example draw image radial gradient (context retrieve , image load not shown):
var radgrad = ctx.createradialgradient( img.width / 2, img.height / 2, 10, img.width / 2, img.height / 2, img.width/2); radgrad.addcolorstop(0, "transparent"); radgrad.addcolorstop(1, "#000"); ctx.drawimagegradient(img, 112.5, 130, radgrad);
the code works follows:
- create canvas element dynamically , draw image on it.
- retrieve imagedata new canvas.
- retrieve imagedata location on canvas want draw image on to.
- iterate through destination imagedata , update each pixel adding percentage (derived gradient transparency value) of image , destination pixel values.
- finally put updated image data onto destination canvas.
obviously performance issue images larger. image on http://code.google.com/p/canvasimagegradient/ takes 6-10ms draw. 1024x768 image takes 100ms-250ms draw. still usable though long you're not animating.
Comments
Post a Comment