Created
July 12, 2024 12:27
-
-
Save lukejagodzinski/678383a8e0e4d38ccdb4a4065f4f553b to your computer and use it in GitHub Desktop.
Neuralink Grid Game
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
function findColor(canvas, targetColor) { | |
const ctx = canvas.getContext("2d"); | |
const width = canvas.width; | |
const height = canvas.height; | |
const imageData = ctx.getImageData(0, 0, width, height); | |
const data = imageData.data; | |
const [r, g, b, a] = targetColor; | |
for (let y = 0; y < height; y++) { | |
for (let x = 0; x < width; x++) { | |
const index = (y * width + x) * 4; | |
if ( | |
data[index] === r && | |
data[index + 1] === g && | |
data[index + 2] === b && | |
data[index + 3] === a | |
) { | |
return { x, y }; | |
} | |
} | |
} | |
return null; | |
} | |
function simulatePointerEvent(canvas, x, y) { | |
const rect = canvas.getBoundingClientRect(); | |
const dpr = window.devicePixelRatio || 1; | |
const clientX = rect.left + x / dpr + 5; | |
const clientY = rect.top + y / dpr + 5; | |
const event = new PointerEvent("pointerup", { | |
clientX, | |
clientY, | |
bubbles: true, | |
cancelable: true, | |
view: window, | |
}); | |
canvas.dispatchEvent(event); | |
} | |
const canvas = document.querySelector("canvas"); | |
const targetColor = [10, 132, 255, 255]; | |
window.setInterval(() => { | |
const coords = findColor(canvas, targetColor); | |
if (coords) { | |
simulatePointerEvent(canvas, coords.x, coords.y); | |
} | |
}, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment