Skip to content

Instantly share code, notes, and snippets.

@SandyGifford
Last active May 15, 2020 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SandyGifford/e3e34be403b598e99f3ed31cb12c8031 to your computer and use it in GitHub Desktop.
Save SandyGifford/e3e34be403b598e99f3ed31cb12c8031 to your computer and use it in GitHub Desktop.
Paste this into the console when viewing any image in chrome. You'll be prompted for a "max" value. Click anywhere on the image to place an "X". Click on any X to remove it. State is stored in local storage so you can rerun the script on any image and pick up from where you left off. Click the number in the top right to change the max.
/**********************************/
/* */
/* DO NOT PUT ANY CODE YOU DO NOT */
/* UNDERSTAND INTO YOUR BROWSER'S */
/* CONSOLE. */
/* */
/**********************************/
(() => {
let total;
const img = document.querySelector("img");
const data = {};
function makeEl({ tagname, ns = "http://www.w3.org/2000/svg", attrs = {}, children = [] }) {
const el = document.createElementNS(ns, tagname);
const { style = {} } = attrs;
delete attrs.style;
Object.keys(style).forEach(prop => el.style[prop] = style[prop]);
Object.keys(attrs).forEach(prop => el.setAttribute(prop, attrs[prop]));
children.forEach(child => {
el.append(makeEl(child));
});
return el;
}
function getCountColor() {
return total ?
`hsl(${Math.round(120 * (Object.keys(data).length / total))}, 100%, 50%)` :
"yellow"
}
const svg = makeEl({
tagname: "svg",
attrs: { style: {
position: "fixed",
top: "50%",
left: "50%",
width: `${img.offsetWidth}px`,
height: `${img.offsetHeight}px`,
transform: "translate(-50%, -50%)"
} }
});
let lastId = 0;
const countEl = makeEl({
ns: "http://www.w3.org/1999/xhtml",
tagname: "div",
attrs: { style: {
position: "fixed",
top: "0px",
right: "0px",
margin: "10px",
color: getCountColor(),
fontFamily: "sans-serif",
userSelect: "none"
} }
});
countEl.addEventListener("click", askForNewTotal);
window.addEventListener("resize", () => {
svg.style.width = `${img.offsetWidth}px`;
svg.style.height = `${img.offsetHeight}px`;
render();
});
document.body.append(svg);
document.body.append(countEl);
const existing = parseFloat(localStorage.getItem("total"));
if (typeof existing === "number" && !isNaN(existing)) total = existing;
else askForNewTotal();
function askForNewTotal() {
const res = parseFloat(prompt("What is the total? (leave blank for no total)", localStorage.getItem("total") || 50));
total = (typeof res === "number" && !isNaN(res)) ? res : 0;
localStorage.setItem("total", total);
render();
}
function addEx(x, y, surpressRender = false) {
const id = lastId++;
data[id] = { x, y };
localStorage.setItem("exes", JSON.stringify(data));
if (!surpressRender) render();
}
function removeEx(id) {
delete data[id];
localStorage.setItem("exes", JSON.stringify(data));
render();
}
function render() {
while (svg.lastChild) {
svg.removeChild(svg.lastChild);
}
countEl.style.color = getCountColor();
countEl.textContent = Object.keys(data).length + (total ? ` of ${total}` : "");
const rad = img.offsetWidth * 5 / 1000000;
const strokeWidth = `${img.offsetWidth * 3 / 10000}%`;
Object.keys(data).forEach(id => {
const {x, y} = data[id];
const ex = makeEl({
tagname: "g",
children: [
{
tagname: "line",
attrs: {
x1: `${100 * (x - rad)}%`,
y1: `${100 * (y - rad)}%`,
x2: `${100 * (x + rad)}%`,
y2: `${100 * (y + rad)}%`,
style: { strokeWidth, stroke: "red" }
}
},
{
tagname: "line",
attrs: {
x1: `${100 * (x + rad)}%`,
y1: `${100 * (y - rad)}%`,
x2: `${100 * (x - rad)}%`,
y2: `${100 * (y + rad)}%`,
style: { strokeWidth, stroke: "red" }
}
}
]
});
ex.addEventListener("click", e => {
removeEx(id);
e.stopPropagation();
e.preventDefault();
});
svg.append(ex);
});
}
svg.addEventListener("click", e => {
const cX = (e.clientX - img.offsetLeft) / img.offsetWidth;
const cY = (e.clientY - img.offsetTop) / img.offsetHeight;
addEx(cX, cY);
e.stopPropagation();
e.preventDefault();
});
const prevData = JSON.parse(localStorage.getItem("exes") || "{}");
Object.keys(prevData).forEach(id => addEx(prevData[id].x, prevData[id].y, true));
render();
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment