1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| const getPointColor = (ctx, x, y) => { const { data } = ctx.getImageData(x, y, 1, 1);
const [r, g, b] = data; const a = data[3] / 255;
return `rgba(${r}, ${g}, ${b}, ${a})`; };
const getRectColors = (ctx, x, y, size) => { const halfSize = Math.floor(size / 2); const { data: imageData } = ctx.getImageData( x - halfSize, y - halfSize, size, size );
const colors = []; for (let i = 0; i < size; i++) { if (!colors[i]) { colors[i] = []; }
for (let j = 0; j < size; j++) { const position = (size * i + j) * 4; const r = imageData[position]; const g = imageData[position + 1]; const b = imageData[position + 2]; const a = imageData[position + 3] / 255; colors[i][j] = `rgba(${r}, ${g}, ${b}, ${a})`; } }
return { colors, }; };
const drawMagnifier = (colors, size = 8) => { const count = colors.length;
const diameter = size * count; const radius = diameter / 2;
const canvas = document.createElement("canvas"); canvas.width = diameter; canvas.height = diameter;
canvas.style = ` position: static; width: ${diameter}px; height: ${diameter}px; `;
const ctx = canvas.getContext("2d");
colors.forEach((row, i) => row.forEach((color, j) => { ctx.fillStyle = color; ctx.fillRect(j * size, i * size, size, size); }) );
ctx.fillStyle = "#000"; ctx.lineWidth = 1; ctx.strokeRect(radius - size / 2, radius - size / 2, size, size);
return canvas; };
const getColorContainer = ({ colors, containerDOM, pos }) => { let container = containerDOM;
if (!container) { const magnifierContainer = document.createElement("div"); container = magnifierContainer; }
container.style = ` position: fixed; left: ${pos.x}px; top: ${pos.y}px; transform: translate(-50%, -50%); z-index: 999; pointer-events: none; `;
container.innerHTML = "";
if (!container.classList.contains("maginifier-container")) { container.classList.add("maginifier-container"); }
const maginifierCanvas = drawMagnifier(colors); container.appendChild(maginifierCanvas);
return container; };
|