使用getImageData获取RGB值

时间:2018-09-13 08:52:01

标签: rgb getimagedata

我正在读取图像的所有像素,并且试图获取像素的rgb值并进行更改。目前,我只能从中得到1个变量。从1到255不等。但是我似乎无法从中得到3个值。我在做什么错了?

<script>
var effectButton;
var paintButton;
var canvas;
var context;

function init()
{
    var image = document.getElementById('SourceImage');
    effectButton = document.getElementById('EffectButton');
    paintButton = document.getElementById('PaintButton');
    canvas = document.getElementById('Canvas');
    context = canvas.getContext('2d');

    // Set the canvas the same width and height of the image
    canvas.width = image.width;
    canvas.height = image.height;

    paintButton.addEventListener('click', function ()
    {
        drawImage(image);
    });

    effectButton.addEventListener('click', addEffect);
}

function drawImage(image)
{
  context.drawImage(image, 0, 0);
}

function addEffect()
{
    var imageData = context.getImageData(0, 0, canvas.width, canvas.height);
    MakeMoreRed(imageData.data);
    context.putImageData(imageData, 0, 0);
}

function MakeMoreRed(data)
{
    for (var i = 0; i < data.length; i++ )
    {
        //How do i get the rgb value?? from data[i]
        data[i] = data[i] + 5;

        //var green = data[i] + 5;
        //var blue= data[i] + 5;
    }
}

window.addEventListener('load', init);

</script>

0 个答案:

没有答案