<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Zhuo Jinggang</title>
    <description>The latest articles on DEV Community by Zhuo Jinggang (@zhuojg).</description>
    <link>https://dev.to/zhuojg</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1241915%2F8a2d3d33-ed18-4cbd-8fa2-90f8515a5c77.jpeg</url>
      <title>DEV Community: Zhuo Jinggang</title>
      <link>https://dev.to/zhuojg</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zhuojg"/>
    <language>en</language>
    <item>
      <title>Killing the "Lollipop": Rebuilding Rotation UX in React Konva</title>
      <dc:creator>Zhuo Jinggang</dc:creator>
      <pubDate>Sun, 14 Dec 2025 15:32:19 +0000</pubDate>
      <link>https://dev.to/zhuojg/killing-the-lollipop-rebuilding-rotation-ux-in-react-konva-1lo7</link>
      <guid>https://dev.to/zhuojg/killing-the-lollipop-rebuilding-rotation-ux-in-react-konva-1lo7</guid>
      <description>&lt;p&gt;When I started building a canvas-heavy product with React Konva, I ran into a UX problem almost immediately.&lt;/p&gt;

&lt;p&gt;Konva is powerful, stable, and well thought-out — but its &lt;strong&gt;interaction model is old&lt;/strong&gt;. In particular, rotation is controlled by the classic &lt;em&gt;lollipop handle&lt;/em&gt;: a small stick protruding from the bounding box.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fufgh2ly2v77zc1r0ni18.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fufgh2ly2v77zc1r0ni18.webp" alt="Konva Default Rotation Handle"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That handle no longer exists in modern design tools.&lt;/p&gt;

&lt;p&gt;In tools like Figma or Sketch, users have strong muscle memory:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;grab a corner to resize&lt;/li&gt;
&lt;li&gt;hover just outside a corner to rotate&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There’s no visible widget, no extra UI, and no explanation required. Rotation is discovered spatially, not visually.&lt;/p&gt;

&lt;p&gt;I wanted that same interaction model in my canvas app. Instead of tweaking &lt;code&gt;Konva.Transformer&lt;/code&gt;, I decided to &lt;strong&gt;remove it entirely&lt;/strong&gt; and rebuild rotation from first principles.&lt;/p&gt;

&lt;p&gt;This post walks through the architecture and geometry behind that decision — starting from the interaction idea, moving through the type system, and ending with the rotation math itself.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgsn1gpsjpu8e5plakxzk.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgsn1gpsjpu8e5plakxzk.webp" alt="Improved Rotation Handle"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Not Customize &lt;code&gt;Konva.Transformer&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;At first glance, the transformer seems like the obvious place to start. But the real problem isn’t its visuals — it’s its &lt;strong&gt;encapsulation&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Konva.Transformer&lt;/code&gt; bundles together:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;hit testing&lt;/li&gt;
&lt;li&gt;cursor logic&lt;/li&gt;
&lt;li&gt;rotation math&lt;/li&gt;
&lt;li&gt;interaction state&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;inside Konva’s internal implementation. That makes small UX changes — like &lt;em&gt;where&lt;/em&gt; rotation activates or &lt;em&gt;how&lt;/em&gt; the cursor behaves — disproportionately hard.&lt;/p&gt;

&lt;p&gt;What I wanted instead was:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;hit testing that lives in application code&lt;/li&gt;
&lt;li&gt;rotation math I can reason about&lt;/li&gt;
&lt;li&gt;cursor feedback that actually reflects what’s happening&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So rather than fighting the transformer, I ejected interaction entirely out of it.&lt;/p&gt;

&lt;p&gt;You can find the whole implementation and try it for yourself:&lt;/p&gt;

&lt;p&gt;

&lt;iframe src="https://stackblitz.com/edit/rebuilt-rotation-in-react-konva?embed=1" width="100%" height="500"&gt;
&lt;/iframe&gt;


&lt;/p&gt;

&lt;h2&gt;
  
  
  The Strategy: “Ejecting” Interaction to the Stage
&lt;/h2&gt;

&lt;p&gt;The core idea is simple:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The stage owns all interaction. Shapes are dumb.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Instead of letting Konva manage rotation internally, we listen to &lt;code&gt;onMouseDown&lt;/code&gt;, &lt;code&gt;onMouseMove&lt;/code&gt;, and &lt;code&gt;onMouseUp&lt;/code&gt; on the &lt;strong&gt;stage&lt;/strong&gt;, track interaction state ourselves, and update shape state explicitly.&lt;/p&gt;

&lt;p&gt;That immediately raises a question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;What exactly is the user doing right now?&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To answer that cleanly, I model interaction as a discriminated union.&lt;/p&gt;

&lt;h3&gt;
  
  
  Defining Interaction as State
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;TransformMode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;none&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;rotate&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;center&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
      &lt;span class="nl"&gt;corner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
      &lt;span class="nl"&gt;base&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;drag&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This type does a lot of work:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;none&lt;/code&gt; means no active interaction&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;drag&lt;/code&gt; means pointer movement translates the shape&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;rotate&lt;/code&gt; carries &lt;em&gt;everything rotation needs&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the rectangle’s center&lt;/li&gt;
&lt;li&gt;the corner that initiated rotation&lt;/li&gt;
&lt;li&gt;a &lt;code&gt;base&lt;/code&gt; angle used for cursor orientation&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;A &lt;code&gt;useRef&amp;lt;TransformMode&amp;gt;&lt;/code&gt; holds the current mode. It changes &lt;strong&gt;only&lt;/strong&gt; on &lt;code&gt;mouseDown&lt;/code&gt; and &lt;code&gt;mouseUp&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;mouseMove&lt;/code&gt; never decides &lt;em&gt;what&lt;/em&gt; the interaction is — it only reacts to the current mode.&lt;/p&gt;

&lt;p&gt;That separation is crucial.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hit Testing as a Pure Function
&lt;/h2&gt;

&lt;p&gt;With interaction state defined, the next problem is intent detection.&lt;/p&gt;

&lt;p&gt;When the pointer is here — what &lt;em&gt;would&lt;/em&gt; happen if the user clicked?&lt;/p&gt;

&lt;p&gt;I encode that logic in a single function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;checkHitArea&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;pointer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;TransformMode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function does &lt;strong&gt;no side effects&lt;/strong&gt;. Given a pointer position, it returns the interaction mode that &lt;em&gt;should&lt;/em&gt; activate if the user clicks right now.&lt;/p&gt;

&lt;p&gt;It’s used in two places:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;onMouseMove&lt;/code&gt;&lt;/strong&gt; → preview intent (cursor changes)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;onMouseDown&lt;/code&gt;&lt;/strong&gt; → commit intent (lock interaction state)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That symmetry keeps the mental model clean.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resolving Drag vs Rotate
&lt;/h2&gt;

&lt;p&gt;There’s a subtle UX edge case:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If the pointer is &lt;em&gt;inside&lt;/em&gt; the rectangle but near a corner, users expect to &lt;strong&gt;drag&lt;/strong&gt;, not rotate.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Modern design tools resolve this spatially. Rotation only activates &lt;em&gt;outside&lt;/em&gt; the shape.&lt;/p&gt;

&lt;p&gt;I replicate that logic by comparing distances to the rectangle’s center:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;corner&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;cornerList&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;distToCorner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;pointsDistance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pointer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;corner&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pointerToCenter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;pointsDistance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pointer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;center&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cornerToCenter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;pointsDistance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;corner&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;center&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Pointer must be farther from center than the corner itself&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cornerToCenter&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;pointerToCenter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;distToCorner&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;ROTATE_HOTZONE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;rotate&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;center&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;corner&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;base&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;corner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;base&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;rectState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;rotation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If rotation doesn’t match, I fall back to a point-in-polygon test to detect dragging.&lt;/p&gt;

&lt;p&gt;This mirrors how Figma resolves ambiguous intent — and it feels immediately familiar.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rotation Is About the Center, Not the Corner
&lt;/h2&gt;

&lt;p&gt;Once rotation starts, the math begins.&lt;/p&gt;

&lt;p&gt;The key insight is this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;You cannot rotate a rectangle in place by changing &lt;code&gt;rotation&lt;/code&gt; alone.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Canvas rotation happens around the shape’s local origin (top-left by default). If you don’t adjust position, the rectangle will orbit instead of spinning.&lt;/p&gt;

&lt;p&gt;To get a stable rotation, the rectangle’s &lt;strong&gt;center must remain fixed&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Measure Angular Delta
&lt;/h3&gt;

&lt;p&gt;We measure how much the pointer has moved &lt;em&gt;around the center&lt;/em&gt;, relative to the original corner.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;angle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;atan2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pointer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;center&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pointer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;center&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;angle2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;atan2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;corner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;center&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;corner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;center&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;delta&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;angle&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;angle2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;180&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PI&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This &lt;code&gt;delta&lt;/code&gt; is the rotation change since the interaction began.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Rotate the Top-Left Around the Center
&lt;/h3&gt;

&lt;p&gt;To keep the center fixed, we rotate the rectangle’s &lt;strong&gt;top-left point&lt;/strong&gt; around the center.&lt;/p&gt;

&lt;p&gt;Conceptually:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Translate the rectangle so the center is at &lt;code&gt;(0, 0)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Rotate the top-left vector&lt;/li&gt;
&lt;li&gt;Translate back
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;deltaRad&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;delta&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PI&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;180&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cos&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;deltaRad&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;deltaRad&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;initialRect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;center&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;initialRect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;center&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newX&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;center&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dx&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;cos&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;dy&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;sin&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;center&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dx&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;sin&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;dy&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;cos&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the entire trick.&lt;/p&gt;

&lt;p&gt;Rotation becomes a pure geometric transformation instead of a Konva side effect.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Commit the New State
&lt;/h3&gt;

&lt;p&gt;Finally, we apply both position and rotation together:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;setRectState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;newX&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;newY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;initialRect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;initialRect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;height&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;rotation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;initialRect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;rotation&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;delta&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The rectangle now spins exactly in place — no drifting, no wobble, no surprises.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cursor Feedback Is Part of the Interaction
&lt;/h2&gt;

&lt;p&gt;Most rotation implementations stop once the math works.&lt;/p&gt;

&lt;p&gt;But cursor feedback matters.&lt;/p&gt;

&lt;p&gt;Each corner has an inherent orientation, and the rectangle itself may already be rotated. I combine both:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;base&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;corner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;base&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;rectState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;rotation&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That value feeds into a small hook, &lt;code&gt;useRotationCursor&lt;/code&gt;, which:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;generates a tiny SVG arrow&lt;/li&gt;
&lt;li&gt;rotates it to the correct angle&lt;/li&gt;
&lt;li&gt;encodes it as a data URI&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As the user rotates the shape, the cursor rotates &lt;em&gt;with it&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;This is a small detail — but it’s the difference between something that works and something that feels native.&lt;/p&gt;

&lt;h2&gt;
  
  
  What This Enables
&lt;/h2&gt;

&lt;p&gt;This demo intentionally leaves out:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;resizing&lt;/li&gt;
&lt;li&gt;snapping&lt;/li&gt;
&lt;li&gt;multi-selection&lt;/li&gt;
&lt;li&gt;keyboard modifiers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But the architectural shift is already complete.&lt;/p&gt;

&lt;p&gt;Hit testing, interaction state, geometry, and rendering are now &lt;strong&gt;orthogonal concerns&lt;/strong&gt;. Adding resizing doesn’t require rewriting rotation. Snapping doesn’t interfere with dragging.&lt;/p&gt;

&lt;p&gt;And most importantly:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The lollipop is gone.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What’s left is an interaction model that matches modern design tools — built on simple math, explicit state, and predictable behavior.&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
