Skip to content

Instantly share code, notes, and snippets.

@rostegg
Created September 2, 2021 11:31
Show Gist options
  • Save rostegg/8954275933d3ae516e287a8a685dd723 to your computer and use it in GitHub Desktop.
Save rostegg/8954275933d3ae516e287a8a685dd723 to your computer and use it in GitHub Desktop.
Get ImageData from canvas, loading picture via url
const getImageDataFromUrl = (url, callback) => {
const canvas = document.createElement("canvas")
const context = canvas.getContext("2d")
const image = new Image()
image.onload = async () => {
const { width, height } = image
const { data: imageData } = context.getImageData(0, 0, width, height)
callback(imageData)
}
image.src = url
}
function log(data) {
console.log(data)
}
getImageDataFromUrl('https://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png', log)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment