Html5 Canvas overlay

后端 未结 1 794
遥遥无期
遥遥无期 2021-01-07 14:04

Is there a good solution to draw a bitmap with an overlay color with canvas ?

What I\'m looking to do is drawing a bitmap with a unique color for all not transparent

1条回答
  •  没有蜡笔的小新
    2021-01-07 14:22

    Live Demo

    One way to do it is to loop through each pixel and change the r/g/b values to the value you want it. By skipping over the alpha value it will only change the opaque pixels to the color you want.

    var canvas = document.getElementById("canvas"),
        ctx = canvas.getContext("2d"),
        image = document.getElementById("testImage");
    
    ctx.drawImage(image,0,0);
    
    var imgd = ctx.getImageData(0, 0, 128, 128),
        pix = imgd.data,
        uniqueColor = [0,0,255]; // Blue for an example, can change this value to be anything.
    
    // Loops through all of the pixels and modifies the components.
    for (var i = 0, n = pix.length; i 

    0 讨论(0)
提交回复
热议问题