03Rotate Handle Stemming From the Selection Box's Top
A short line extends from the top-center of the selection box to a round handle, and dragging that handle along a circular path spins the object around the selection box's center by that angle.
Best forTilting a sticker or stamp-style object at an angle
applyRotateangleFromPointermod360

<main class="stage">
<div class="scene is-demo" id="scene">
<div class="frame" id="frame">
<div class="rotator" id="rotator">
<div class="subjectShape"></div>
<div class="stick" aria-hidden="true"></div>
<div class="handle" id="handle" tabindex="0" role="slider" aria-label="Rotate handle, drag to rotate" aria-valuemin="0" aria-valuemax="360" aria-valuenow="0"></div>
</div>
</div>
<div class="readout" id="readout" aria-hidden="true"><span id="rd">0</span>°</div>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
// 변수 3개
$duration: $dur-enter;
$easing: $ease-out;
$color: $subject-pink;
// 계산 겹: index.html 의 angleFromPointer() 가 프레임 중심→포인터 벡터의 atan2 로 절대 각도를 구하고
// ROTATE_SNAP_DEG 배수로 반올림해 rotator 에 rotate() 로 준다 — 트윈이 아니라 매 프레임 산술.
@include stage($stage-orange);
.scene {
position: relative;
width: 320px;
height: 180px;
border-radius: $r-card;
overflow: hidden;
box-shadow: $shadow-raised;
background: linear-gradient(160deg, #ffd9c4, $stage-orange 90%);
}
.frame {
position: absolute;
top: 50%;
left: 50%;
width: 84px;
height: 84px;
margin: -42px;
}
.rotator {
position: relative;
width: 100%;
height: 100%;
transform-origin: 50% 50%;
will-change: transform;
}
.subjectShape {
position: absolute;
inset: 8px;
border-radius: $r-sm;
background: linear-gradient(135deg, $subject-lilac, $subject-pink);
box-shadow: $shadow-press;
}
.stick {
position: absolute;
left: 50%;
top: -22px;
width: 2px;
height: 22px;
margin-left: -1px;
background: rgba($subject-ink, .55);
}
.handle {
position: absolute;
left: 50%;
top: -30px;
width: 16px;
height: 16px;
margin: -8px 0 0 -8px;
border-radius: $r-pill;
background: $subject-cream;
border: 3px solid $subject-ink;
cursor: grab;
touch-action: none;
&:active { cursor: grabbing; }
&:focus-visible { outline: 2px solid $color; outline-offset: 3px; }
}
.readout {
position: absolute;
left: $sp-3;
bottom: $sp-3;
padding: $sp-1 $sp-2;
border-radius: $r-pill;
background: rgba($subject-ink, .78);
color: $subject-cream;
font-size: 10px;
font-variant-numeric: tabular-nums;
}
@media (max-width: 340px) {
.scene { width: 220px; height: 124px; }
.frame { width: 62px; height: 62px; margin: -31px; }
.stick { top: -16px; height: 16px; }
.handle { top: -22px; width: 13px; height: 13px; margin: -6px 0 0 -6px; }
.readout { font-size: 9px; }
}
@media (prefers-reduced-motion: reduce) {
// 실사용 경로 적용 — 핸들 드래그·키보드는 index.html 이 트윈 없이 즉시 반영한다.
// scss 만: 핸들 포커스 아웃라인 전환만 꺼서 잔여 트랜지션을 없앤다.
.handle { transition: none; }
}html, body {
margin: 0;
overflow: hidden;
}
.stage {
box-sizing: border-box;
min-height: 100vh;
display: grid;
place-items: center;
margin: 0;
background: #ff4d1f;
font-family: -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
padding: clamp(8px, 4vw, 24px);
}
.scene {
position: relative;
width: 320px;
height: 180px;
border-radius: 16px;
overflow: hidden;
box-shadow: 0 14px 24px rgba(23, 20, 26, 0.32);
background: linear-gradient(160deg, #ffd9c4, #ff4d1f 90%);
}
.frame {
position: absolute;
top: 50%;
left: 50%;
width: 84px;
height: 84px;
margin: -42px;
}
.rotator {
position: relative;
width: 100%;
height: 100%;
transform-origin: 50% 50%;
will-change: transform;
}
.subjectShape {
position: absolute;
inset: 8px;
border-radius: 8px;
background: linear-gradient(135deg, #b9a0ff, #ff9ebb);
box-shadow: 0 6px 12px rgba(23, 20, 26, 0.32);
}
.stick {
position: absolute;
left: 50%;
top: -22px;
width: 2px;
height: 22px;
margin-left: -1px;
background: rgba(23, 20, 26, 0.55);
}
.handle {
position: absolute;
left: 50%;
top: -30px;
width: 16px;
height: 16px;
margin: -8px 0 0 -8px;
border-radius: 999px;
background: #fff7e6;
border: 3px solid #17141a;
cursor: grab;
touch-action: none;
}
.handle:active {
cursor: grabbing;
}
.handle:focus-visible {
outline: 2px solid #ff9ebb;
outline-offset: 3px;
}
.readout {
position: absolute;
left: 12px;
bottom: 12px;
padding: 4px 8px;
border-radius: 999px;
background: rgba(23, 20, 26, 0.78);
color: #fff7e6;
font-size: 10px;
font-variant-numeric: tabular-nums;
}
@media (max-width: 340px) {
.scene {
width: 220px;
height: 124px;
}
.frame {
width: 62px;
height: 62px;
margin: -31px;
}
.stick {
top: -16px;
height: 16px;
}
.handle {
top: -22px;
width: 13px;
height: 13px;
margin: -6px 0 0 -6px;
}
.readout {
font-size: 9px;
}
}
@media (prefers-reduced-motion: reduce) {
.handle {
transition: none;
}
}(function () {
var ROTATE_SNAP_DEG = 15; // 변형 03 이 5 로 좁힌다
var LOOP_SEQUENCE = [70, 200, 320, 360];
var HOP_MS = 560;
var HOLD_MS = 260;
var scene = document.getElementById('scene');
var frame = document.getElementById('frame');
var rotator = document.getElementById('rotator');
var handle = document.getElementById('handle');
var rd = document.getElementById('rd');
var reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
var angle = 0;
var dragging = false;
var timers = [];
function clamp(v, lo, hi) { return Math.min(hi, Math.max(lo, v)); }
function easeOutCubic(t) { return 1 - Math.pow(1 - t, 3); }
function mod360(v) { return ((v % 360) + 360) % 360; }
function applyRotate(deg) {
angle = mod360(deg);
rotator.style.transform = 'rotate(' + angle.toFixed(1) + 'deg)';
rd.textContent = Math.round(angle);
handle.setAttribute('aria-valuenow', String(Math.round(angle)));
}
function human() {
if (scene.classList.contains('is-demo')) {
scene.classList.remove('is-demo');
timers.forEach(function (t) { clearTimeout(t); clearInterval(t); });
timers = [];
}
}
// 회전 핸들 = atan2 각 → rotate. 프레임 중심(회전하지 않는 고정 요소)에서 포인터까지의 벡터로
// 절대 각도를 매 프레임 구하고, ROTATE_SNAP_DEG 배수로 반올림해 스냅한다.
function angleFromPointer(e) {
var r = frame.getBoundingClientRect();
var cx = r.left + r.width / 2;
var cy = r.top + r.height / 2;
var raw = Math.atan2(e.clientY - cy, e.clientX - cx) * 180 / Math.PI;
return mod360(raw + 90);
}
handle.addEventListener('pointerdown', function (e) {
human();
dragging = true;
handle.setPointerCapture(e.pointerId);
});
handle.addEventListener('pointermove', function (e) {
if (!dragging) return;
var a = angleFromPointer(e);
var snapped = Math.round(a / ROTATE_SNAP_DEG) * ROTATE_SNAP_DEG;
applyRotate(snapped);
});
function end(e) {
if (!dragging) return;
dragging = false;
try { handle.releasePointerCapture(e.pointerId); } catch (err) {}
}
handle.addEventListener('pointerup', end);
handle.addEventListener('pointercancel', end);
handle.addEventListener('keydown', function (e) {
if (e.key === 'ArrowRight' || e.key === 'ArrowUp') { human(); applyRotate(angle + ROTATE_SNAP_DEG); e.preventDefault(); }
else if (e.key === 'ArrowLeft' || e.key === 'ArrowDown') { human(); applyRotate(angle - ROTATE_SNAP_DEG); e.preventDefault(); }
});
// reduce: 실사용 경로 적용 — 핸들 pointer·keydown 은 위에서 즉시 반영한다.
// 아래 hopRotate()·cycle() 은 시연 게이트만(연속 각도, 스냅 없이 부드럽게 돈다) — reduce 에서는 정지.
function hopRotate(target, onDone) {
var from = angle;
var start = performance.now();
function frame(now) {
if (!scene.classList.contains('is-demo')) return;
var p = clamp((now - start) / HOP_MS, 0, 1);
applyRotate(from + (target - from) * easeOutCubic(p));
if (p < 1) requestAnimationFrame(frame);
else if (onDone) onDone();
}
requestAnimationFrame(frame);
}
function cycle(i) {
if (!scene.classList.contains('is-demo')) return;
var target = LOOP_SEQUENCE[i % LOOP_SEQUENCE.length];
hopRotate(target, function () {
timers.push(setTimeout(function () { cycle(i + 1); }, HOLD_MS));
});
}
if (!reduce) {
cycle(0);
}
})();import { useEffect, useRef } from 'react';
import styles from './XfRotateHandle.module.scss';
const ROTATE_SNAP_DEG = 15; // 변형 03 이 5 로 좁힌다 — 드래그 중 붙는 회전 스냅 각도
const HOP_MS = 620;
const HOLD_MS = 340;
const LOOP_SEQUENCE = [70, 200, 320, 360];
export default function XfRotateHandle() {
const sceneRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const scene0 = sceneRef.current;
if (!scene0) return undefined;
const scene = scene0;
const frame0 = scene.querySelector<HTMLDivElement>('#frame');
const rotator0 = scene.querySelector<HTMLDivElement>('#rotator');
const handle0 = scene.querySelector<HTMLDivElement>('#handle');
const rd0 = scene.querySelector<HTMLSpanElement>('#rd');
if (!frame0 || !rotator0 || !handle0 || !rd0) return undefined;
const frame = frame0;
const rotator = rotator0;
const handle = handle0;
const rd = rd0;
const reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
let deg = 0;
let dragging = false;
let rafId = 0;
let timers: number[] = [];
function clamp(v: number, lo: number, hi: number) { return Math.min(hi, Math.max(lo, v)); }
function easeOutCubic(t: number) { return 1 - Math.pow(1 - t, 3); }
function mod360(raw: number) { return ((raw % 360) + 360) % 360; }
// 프레임 중심→포인터 벡터의 atan2 로 절대 각도를 구한다 — 0° 가 위를 향하도록 +90 보정.
function angleFromPointer(e: PointerEvent) {
const r = frame.getBoundingClientRect();
const cx = r.left + r.width / 2;
const cy = r.top + r.height / 2;
const dx = e.clientX - cx;
const dy = e.clientY - cy;
return mod360((Math.atan2(dy, dx) * 180) / Math.PI + 90);
}
function applyRotate(a: number) {
deg = mod360(a);
rotator.style.transform = 'rotate(' + deg.toFixed(1) + 'deg)';
rd.textContent = String(Math.round(deg));
handle.setAttribute('aria-valuenow', String(Math.round(deg)));
}
function human() {
if (scene.classList.contains(styles['is-demo'])) {
scene.classList.remove(styles['is-demo']);
timers.forEach((t) => window.clearTimeout(t));
timers = [];
}
}
function onPointerDown(e: PointerEvent) {
human();
dragging = true;
handle.setPointerCapture(e.pointerId);
}
function onPointerMove(e: PointerEvent) {
if (!dragging) return;
// 실 드래그는 ROTATE_SNAP_DEG 배수로 반올림해 붙는다.
const a = angleFromPointer(e);
applyRotate(Math.round(a / ROTATE_SNAP_DEG) * ROTATE_SNAP_DEG);
}
function onPointerUp(e: PointerEvent) {
if (!dragging) return;
dragging = false;
try { handle.releasePointerCapture(e.pointerId); } catch { /* 캡처가 이미 풀린 경우 */ }
}
function onKeyDown(e: KeyboardEvent) {
if (e.key === 'ArrowLeft' || e.key === 'ArrowDown') { human(); applyRotate(deg - ROTATE_SNAP_DEG); e.preventDefault(); }
else if (e.key === 'ArrowRight' || e.key === 'ArrowUp') { human(); applyRotate(deg + ROTATE_SNAP_DEG); e.preventDefault(); }
}
handle.addEventListener('pointerdown', onPointerDown);
handle.addEventListener('pointermove', onPointerMove);
handle.addEventListener('pointerup', onPointerUp);
handle.addEventListener('pointercancel', onPointerUp);
handle.addEventListener('keydown', onKeyDown);
applyRotate(0);
// reduce: 실사용 경로 적용 — 핸들 드래그·키보드는 위에서 즉시 반영한다.
// 아래 자동 루프는 시연 게이트만 — 스냅 없이 연속 회전하며 reduce 에서는 돌지 않는다.
function hopRotate(target: number, onDone: () => void) {
const from = deg;
const delta = target - from;
const start = performance.now();
function frame(now: number) {
if (!scene.classList.contains(styles['is-demo'])) return;
const p = clamp((now - start) / HOP_MS, 0, 1);
applyRotate(from + delta * easeOutCubic(p));
if (p < 1) rafId = requestAnimationFrame(frame);
else onDone();
}
rafId = requestAnimationFrame(frame);
}
function cycle(i: number) {
if (!scene.classList.contains(styles['is-demo'])) return;
const target = LOOP_SEQUENCE[i % LOOP_SEQUENCE.length];
hopRotate(target, () => {
timers.push(window.setTimeout(() => cycle(i + 1), HOLD_MS));
});
}
if (!reduce) {
cycle(0);
}
return () => {
handle.removeEventListener('pointerdown', onPointerDown);
handle.removeEventListener('pointermove', onPointerMove);
handle.removeEventListener('pointerup', onPointerUp);
handle.removeEventListener('pointercancel', onPointerUp);
handle.removeEventListener('keydown', onKeyDown);
timers.forEach((t) => window.clearTimeout(t));
cancelAnimationFrame(rafId);
};
}, []);
return (
<main className={styles.stage}>
<div className={styles.scene + ' ' + styles['is-demo']} ref={sceneRef}>
<div id="frame" className={styles.frame}>
<div id="rotator" className={styles.rotator}>
<span className={styles.subjectShape} aria-hidden="true"></span>
<span className={styles.stick} aria-hidden="true"></span>
<div
id="handle"
className={styles.handle}
role="slider"
tabIndex={0}
aria-label="회전 핸들, 드래그하여 회전"
aria-valuemin={0}
aria-valuemax={360}
aria-valuenow={0}
></div>
</div>
</div>
<div className={styles.readout} aria-hidden="true"><span id="rd">0</span>°</div>
</div>
</main>
);
}03 Rotate Handle Stemming From the Selection Box's Top — Measured area 9.46% · moving frames 19/23Original by GODRICH · SCSS and React ship in the zip · Resize Handle UI — 9 Free-Transform Patterns
The post this demo lives in
Resize Handle UI — 9 Free-Transform PatternsAmong nine resize handle patterns, one spins an object as its rotate handle arcs, one snaps scattered icons into a grid See all demos in this post
If you like this effect - similar demos
Creative UI04 Highlight That Lights Only the Path From a Node to the TopCSS Org Chart: 9 Tidy Tree Layout Demos
Creative UI01 Rotating globe drawn from latitude/longitude lines3D Globe Website: 9 Canvas Orbit Scenes
Creative UI04 Coral growth — the reaction front reaches outward from a seed9 Reaction Diffusion Simulations, No Library