ЖМЯКНИ ЖАБУ
[html]
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Жмякательная фотография</title>
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background: #f3eaf0;
overflow: hidden;
}
#container {
width: min(70vw, 350px);
aspect-ratio: 3 / 4;
display: flex;
justify-content: center;
align-items: center;
}
canvas {
width: 100%;
height: 100%;
object-fit: contain;
cursor: grab;
touch-action: none;
user-select: none;
}
canvas:active {
cursor: grabbing;
}
</style>
</head>
<body>
<div id="container">
<canvas id="face"></canvas>
</div>
<script>
const canvas = document.getElementById("face");
const ctx = canvas.getContext("2d");
const img = new Image();
img.crossOrigin = "anonymous";
img.src = "https://upforme.ru/uploads/001c/55/b9/324/906104.png";
const W = 600;
const H = 567;
const COLS = 35;
const ROWS = 45;
canvas.width = W;
canvas.height = H;
let points = [];
let triangles = [];
let dragging = false;
let lastPointer = null;
let pointer = {x: 0, y: 0};
const radius = 105;
const stiffness = 0.075;
const damping = 0.82;
function init() {
points = [];
for (let y = 0; y <= ROWS; y++) {
for (let x = 0; x <= COLS; x++) {
points.push({
x: x * W / COLS,
y: y * H / ROWS,
ox: 0,
oy: 0,
vx: 0,
vy: 0
});
}
}
triangles = [];
for (let y = 0; y < ROWS; y++) {
for (let x = 0; x < COLS; x++) {
const a = y * (COLS + 1) + x;
const b = a + 1;
const c = a + COLS + 1;
const d = c + 1;
triangles.push([a, b, c]);
triangles.push([b, d, c]);
}
}
}
function getPosition(e) {
const rect = canvas.getBoundingClientRect();
return {
x: (e.clientX - rect.left) / rect.width * W,
y: (e.clientY - rect.top) / rect.height * H
};
}
canvas.addEventListener("pointerdown", e => {
dragging = true;
lastPointer = getPosition(e);
pointer = lastPointer;
canvas.setPointerCapture(e.pointerId);
});
canvas.addEventListener("pointermove", e => {
if (!dragging) return;
const current = getPosition(e);
const dx = current.x - lastPointer.x;
const dy = current.y - lastPointer.y;
for (const p of points) {
const px = p.x + p.ox;
const py = p.y + p.oy;
const distance = Math.hypot(
px - lastPointer.x,
py - lastPointer.y
);
if (distance < radius) {
const influence = Math.pow(
1 - distance / radius, 2
);
p.ox += dx * influence * 1.5;
p.oy += dy * influence * 1.5;
}
}
lastPointer = current;
});
function release() {
dragging = false;
lastPointer = null;
}
canvas.addEventListener("pointerup", release);
canvas.addEventListener("pointercancel", release);
function drawTriangle(p1, p2, p3, q1, q2, q3) {
const denom =
p1.x * (p2.y - p3.y) +
p2.x * (p3.y - p1.y) +
p3.x * (p1.y - p2.y);
if (Math.abs(denom) < 0.0001) return;
const a =
(q1.x * (p2.y - p3.y) +
q2.x * (p3.y - p1.y) +
q3.x * (p1.y - p2.y)) / denom;
const b =
(q1.y * (p2.y - p3.y) +
q2.y * (p3.y - p1.y) +
q3.y * (p1.y - p2.y)) / denom;
const c =
(q1.x * (p3.x - p2.x) +
q2.x * (p1.x - p3.x) +
q3.x * (p2.x - p1.x)) / denom;
const d =
(q1.y * (p3.x - p2.x) +
q2.y * (p1.x - p3.x) +
q3.y * (p2.x - p1.x)) / denom;
const e = q1.x - a * p1.x - c * p1.y;
const f = q1.y - b * p1.x - d * p1.y;
ctx.save();
ctx.beginPath();
ctx.moveTo(q1.x, q1.y);
ctx.lineTo(q2.x, q2.y);
ctx.lineTo(q3.x, q3.y);
ctx.closePath();
ctx.clip();
ctx.transform(a, b, c, d, e, f);
ctx.drawImage(img, 0, 0, W, H);
ctx.restore();
}
function render() {
ctx.clearRect(0, 0, W, H);
if (!img.complete || !img.naturalWidth) {
requestAnimationFrame(render);
return;
}
for (const p of points) {
if (!dragging) {
p.vx += -p.ox * stiffness;
p.vy += -p.oy * stiffness;
p.vx *= damping;
p.vy *= damping;
p.ox += p.vx;
p.oy += p.vy;
if (Math.abs(p.ox) < 0.02 &&
Math.abs(p.oy) < 0.02 &&
Math.abs(p.vx) < 0.02 &&
Math.abs(p.vy) < 0.02) {
p.ox = 0;
p.oy = 0;
p.vx = 0;
p.vy = 0;
}
}
}
for (const triangle of triangles) {
const [i1, i2, i3] = triangle;
const p1 = points[i1];
const p2 = points[i2];
const p3 = points[i3];
const q1 = {
x: p1.x + p1.ox,
y: p1.y + p1.oy
};
const q2 = {
x: p2.x + p2.ox,
y: p2.y + p2.oy
};
const q3 = {
x: p3.x + p3.ox,
y: p3.y + p3.oy
};
drawTriangle(p1, p2, p3, q1, q2, q3);
}
requestAnimationFrame(render);
}
img.onload = () => {
init();
render();
};
img.onerror = () => {
ctx.fillStyle = "#e8d8e3";
ctx.fillRect(0, 0, W, H);
ctx.fillStyle = "#76566c";
ctx.font = "20px sans-serif";
ctx.textAlign = "center";
ctx.fillText("Не удалось загрузить фотографию", W/2, H/2);
};
</script>
</body>
</html>
[/html]
Отредактировано Ежонок (Сегодня 17:42:57)



















