Created
August 21, 2021 19:47
-
-
Save LaloHao/502d453b5d3239c3a8028a178a117bd1 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 udg = 'https://upload.wikimedia.org/wikipedia/commons/thumb/5/5f/Escudo_UdeG.svg/1200px-Escudo_UdeG.svg.png' | |
let img = document.createElement('img'); | |
let canvas = document.createElement('canvas'); | |
let pixels = [] | |
img.onload = function() { | |
canvas.width = img.width/4; | |
canvas.height = img.height/4; | |
canvas.getContext('2d').drawImage(img, 0, 0, img.width / 4, img.height / 4); | |
pixels = canvas.getContext('2d').getImageData(0, 0, img.width / 4, img.height / 4).data; | |
} | |
img.src = udg; | |
let strs = []; | |
let p = pixels | |
for (let i = 0; i < pixels.length; i += 4) { | |
const x = ( i / 4) % 300; | |
const y = ((i / 4) - x) / 300; | |
const c = `rgba(${p[i+0]}, ${p[i+1]}, ${p[i+2]}, ${p[i+3] / 255})`; | |
const o = `${c} ${x}px ${y}px`; | |
strs.push(o) | |
console.log(o); | |
} | |
let file = strs.join(',\n') | |
function download(filename, text) { | |
var element = document.createElement('a'); | |
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text)); | |
element.setAttribute('download', filename); | |
element.style.display = 'none'; | |
document.body.appendChild(element); | |
element.click(); | |
document.body.removeChild(element); | |
} | |
download('udg.txt', file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment