贝塞尔曲线控制点
Bézier Curve Control Point
拖动控制点时,二次贝塞尔曲线弯曲,两条辅助线实时跟随。
适用场景
适用于矢量工具、曲线图编辑器和缓动调节器。 用户需要弄懂是哪一个控制点改变了曲线时,把手柄和辅助线一起画在曲线旁。
应用示例
在动画设置面板中拖动一个贝塞尔控制点,预览曲线立即弯曲,辅助线也跟着变化。设计师能观察控制点怎样改变靠近两个端点的曲线。
AI 提示词
给【区域】加入二次贝塞尔曲线编辑器。转换指针坐标到 viewBox,联动更新路径和导线,为控制点提供方向键和重置。
示例代码
保存为 HTML 文件后,用浏览器打开即可运行。代码与页面里的演示一致。
查看 HTML
<!doctype html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>贝塞尔曲线控制点</title>
<style>
:root {
color-scheme: dark;
--demo-bg: #171629;
--demo-ink: #f8f5ff;
--demo-accent: #b9a6ff;
--demo-muted: #bfbbd3;
font-family: system-ui, sans-serif;
color: var(--demo-ink);
background: var(--demo-bg);
}
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; min-width: 0; }
button, input { font: inherit; }
.demo {
display: grid;
place-items: center;
align-content: center;
min-height: 100vh;
padding: 14px;
overflow: hidden;
background: radial-gradient(ellipse at 50% 24%, #28233f, var(--demo-bg) 78%);
}
.demo-button {
border: 1px solid #ffffff3d;
border-radius: 7px;
background: #ffffff12;
color: var(--demo-ink);
padding: 6px 11px;
font-size: 12px;
cursor: pointer;
}
:focus-visible {
outline: 2px solid var(--demo-accent);
outline-offset: 3px;
}
.ui { width: min(100%, 238px); display: grid; gap: 8px; position: relative; font-size: 12px; line-height: 1.35; }
.ui .surface { min-width: 0; border: 1px solid #676591; border-radius: 10px; background: #282744; padding: 8px; }
.ui .bar { display: flex; align-items: center; justify-content: space-between; gap: 6px; }
.ui .actions { display: flex; align-items: center; justify-content: center; flex-wrap: wrap; gap: 5px; }
.ui .muted { color: #cecae9; font-size: 11px; }
.ui .center { text-align: center; }
.ui .mono { font: 11px ui-monospace, monospace; font-variant-numeric: tabular-nums; }
.ui p { margin: 0; }
.ui .button { min-height: 30px; padding: 4px 7px; border-radius: 7px; border: 1px solid #79779f; background: #3d3a60; color: #f9f5ff; font-size: 11px; font-weight: 650; cursor: pointer; }
.ui .button:hover { background: #4e4976; }
.ui .primary { background: #c5eeec; border-color: #dafaee; color: #17213b; }
.ui .primary:hover { background: #e3fff9; }
.ui .button:disabled { opacity: .6; cursor: default; }
.ui .field { min-width: 0; width: 100%; border: 1px solid #79779f; border-radius: 6px; background: #1b1a31; color: #faf5ff; padding: 5px 7px; font-size: 12px; }
.ui .field::placeholder { color: #c2bcdd; }
.ui input[type="range"] { accent-color: #b8ebe0; width: 100%; min-width: 0; margin: 0; }
.ui button { font: inherit; }
.ui [hidden] { display: none !important; }
.ui progress { accent-color: #b8ebe0; }
@media (prefers-reduced-motion: reduce) {
.ui *, .ui *::after, .ui *::before { transition-duration: 0s !important; animation-duration: 0s !important; scroll-behavior: auto !important; }
}
.curve-stage{position:relative;height:100px;border-radius:8px;border:1px solid #716b9d;background:repeating-linear-gradient(90deg,#ac9aea20 0 1px,transparent 1px 22px),repeating-linear-gradient(0deg,#ac9aea20 0 1px,transparent 1px 20px),#24213e}.curve-stage svg{width:100%;height:100%;position:absolute;inset:0;fill:none}.curve-stage .guide{stroke:#9684c0;stroke-dasharray:4 3}.curve-stage .curve{stroke:#b9efe3;stroke-width:3}.curve-stage circle{fill:#dcc7ff}.point{position:absolute;left:50%;top:17%;translate:-50% -50%;width:22px;height:22px;border:4px solid #292543;border-radius:50%;background:#ffcd8c;box-shadow:0 0 0 1px #bfabda;cursor:move;touch-action:none}
</style>
</head>
<body>
<main class="demo">
<div class="ui"><div class="curve-stage" aria-label="曲线编辑器"><svg viewBox="0 0 220 100" preserveAspectRatio="none" aria-hidden="true"><path class="guide" d="M12 82 L110 17 L208 82"/><path class="curve" d="M12 82 Q110 17 208 82"/><circle cx="12" cy="82" r="3"/><circle cx="208" cy="82" r="3"/></svg><button class="point" type="button" aria-label="曲线控制点"></button></div><div class="bar"><span class="mono coordinates">110, 17</span><button class="button reset" type="button">重置</button></div></div>
</main>
<script>
function dragOn(surface, handlers) {
let pointer = null;
let origin = null;
function finish(event, canceled) {
if (event.pointerId !== pointer) return;
const delta = { x: event.clientX - origin.x, y: event.clientY - origin.y };
pointer = null;
handlers.end(delta, canceled);
}
surface.addEventListener('pointerdown', (event) => {
if (pointer !== null || event.button !== 0) return;
pointer = event.pointerId;
origin = { x: event.clientX, y: event.clientY };
surface.setPointerCapture(pointer);
handlers.start?.(event);
});
surface.addEventListener('pointermove', (event) => {
if (event.pointerId === pointer) {
handlers.move({ x: event.clientX - origin.x, y: event.clientY - origin.y }, event);
}
});
surface.addEventListener('pointerup', (event) => finish(event, false));
surface.addEventListener('pointercancel', (event) => finish(event, true));
surface.addEventListener('lostpointercapture', (event) => finish(event, true));
}
const clamp = (value, low, high) => Math.min(high, Math.max(low, value));
const stage = document.querySelector('.curve-stage');
const point = stage.querySelector('.point');
let x = 110, y = 17;
function set(nx, ny) {
x = clamp(Math.round(nx), 13, 207); y = clamp(Math.round(ny), 12, 87);
point.style.left = x / 2.2 + '%'; point.style.top = y + '%';
stage.querySelector('.guide').setAttribute('d', 'M12 82 L' + x + ' ' + y + ' L208 82');
stage.querySelector('.curve').setAttribute('d', 'M12 82 Q' + x + ' ' + y + ' 208 82');
document.querySelector('.coordinates').textContent = x + ', ' + y;
}
dragOn(point, { move(delta, event) { const box = stage.getBoundingClientRect(); set((event.clientX - box.left) * 220 / box.width, (event.clientY - box.top) * 100 / box.height); }, end() {} });
const directions = { ArrowLeft: [-3, 0], ArrowRight: [3, 0], ArrowUp: [0, -3], ArrowDown: [0, 3] };
point.addEventListener('keydown', (event) => { const direction = directions[event.key]; if (direction) { event.preventDefault(); set(x + direction[0], y + direction[1]); } });
document.querySelector('.reset').addEventListener('click', () => { set(110, 17); point.focus(); });
</script>
</body>
</html>UI Skills 原创示例