Created
May 6, 2019 18:04
-
-
Save gladchinda/67640b3ef47c6c02609ba885ee13774b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const wrapper = document.getElementById('image-wrapper'); | |
const img = wrapper.querySelector('img'); | |
const canvas = wrapper.querySelector('canvas'); | |
img.addEventListener('load', () => { | |
canvas.width = img.width; | |
canvas.height = img.height; | |
const ctx = canvas.getContext('2d'); | |
ctx.drawImage(img, 0, 0, width, height); | |
const imageData = ctx.getImageData(0, 0, width, height); | |
const data = imageData.data; | |
for (let i = 0, len = data.length; i < len; i += 4) { | |
const avg = (data[i] + data[i + 1] + data[i + 2]) / 3; | |
data[i] = avg; // red | |
data[i + 1] = avg; // green | |
data[i + 2] = avg; // blue | |
} | |
ctx.putImageData(imageData, 0, 0); | |
}, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment