M
M
Mervin2015-09-26 12:12:05
JavaScript
Mervin, 2015-09-26 12:12:05

How to determine the color of the outermost pixel of an image?

There is an image with an even color along the edge, one-color. How to determine the color, taking from the edge and set the parent to this color in style?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Aves, 2015-09-26
@Mervin

var ctx = document.querySelector('canvas').getContext('2d');
var img = document.querySelector('img');
if (img.complete) drawBackground();
else img.onload = drawBackground;

function drawBackground() {
    ctx.drawImage(img, 0, 0);
    var d = ctx.getImageData(0, 0, 1, 1).data;
    img.parentNode.style.backgroundColor = 'rgb(0, 1, 2)'.replace(/\d/g, function(s) {
        return d[s];
    });
}

JS Bin

D
Denis Ineshin, 2015-09-26
@IonDen

Only with Canvas. Here is a thread discussing how it works.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question