Created
November 3, 2017 00:27
-
-
Save wallabra/4ca72d92a1102f6db762a034a10b1d10 to your computer and use it in GitHub Desktop.
My attempt at translating an image and... 2 hard 2 explain
This file contains hidden or 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
var a, b, brightness, checked, e, g, getBrightnessOf, icanvas, ictx, j, l, margin, pixels, r, ref, ref1, ref2, rx, ry, threshold, weight, x, y; | |
try | |
{ | |
icanvas = document.createElement('canvas'); | |
icanvas.width = img.naturalWidth; | |
icanvas.height = img.naturalHeight; | |
chunkSize *= renderer.canvas.height / 1000; | |
ictx = icanvas.getContext('2d'); | |
ictx.drawImage(img, 0, 0, Math.floor(renderer.canvas.height * icanvas.width / icanvas.height / chunkSize), Math.floor(renderer.canvas.height / chunkSize)); | |
map.clearResize(Math.floor(renderer.canvas.width / chunkSize), Math.floor(renderer.canvas.height / chunkSize)); | |
getBrightnessOf = function(r, g, b, a) | |
{ | |
return (0.299 * r + 0.587 * g + 0.114 * b) * a - a + 1; | |
}; | |
margin = Math.floor((renderer.canvas.width - (renderer.canvas.height * icanvas.width / icanvas.height)) / chunkSize / 2); | |
console.log(margin, (Math.floor(renderer.canvas.width / chunkSize)) + "x" + (Math.floor(renderer.canvas.height / chunkSize)), (Math.floor(renderer.canvas.height * icanvas.width / icanvas.height / chunkSize)) + "x" + (Math.floor(renderer.canvas.height / chunkSize))); | |
for (rx = j = 0, ref = Math.floor(renderer.canvas.width / chunkSize); 0 <= ref ? j < ref : j > ref; rx = 0 <= ref ? ++j : --j) | |
{ | |
for (ry = l = 0, ref1 = Math.floor(renderer.canvas.height / chunkSize); 0 <= ref1 ? l < ref1 : l > ref1; ry = 0 <= ref1 ? ++l : --l) | |
{ | |
if (margin > rx || rx >= Math.floor(renderer.canvas.width / chunkSize) - margin) | |
{ | |
map.setDataAttr(rx, ry, 'weight', 4); | |
continue; | |
} | |
x = Math.floor((rx - margin) * chunkSize * icanvas.width / (renderer.canvas.width - margin * 2 * chunkSize)); | |
y = Math.floor(ry * chunkSize * icanvas.height / renderer.canvas.height); | |
pixels = ictx.getImageData(x, y, 1, 1).data; | |
ref2 = pixels.map(function(c) | |
{ | |
return c / 255.0; | |
}), r = ref2[0], g = ref2[1], b = ref2[2], a = ref2[3]; | |
brightness = getBrightnessOf(r, g, b, a); | |
threshold = +(document.getElementById('threshold').value) / 100; | |
map.setDataAttr(rx, ry, 'obstacle', brightness < threshold); | |
if (brightness >= threshold) | |
{ | |
checked = document.getElementById('weightmap').checked; | |
if (checked) | |
{ | |
weight = Math.floor(((threshold - brightness) / threshold) * 24); | |
map.setDataAttr(rx, ry, 'weight', weight); | |
} | |
} | |
} | |
} | |
console.log(rx, Math.floor(renderer.canvas.width / chunkSize), "|", x, icanvas.width); | |
} | |
catch (error) | |
{ | |
e = error; | |
setStatus('<span style="color:red;">Error importing map from image!</span>'); | |
setTimeout((function() | |
{ | |
return setStatus(lastStatus); | |
}), 5000); | |
throw e; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment