Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="root"></div>
              
            
!

CSS

              
                html {
  height: 100%;
  width: 100%;
  * {
    box-sizing: border-box;
  }
}
body {
  width: 100%;
  min-height: 100%;
  background-color: #000;
  overflow: hidden;
}
#root {
  height: 100vh;
  background-color: #fff;
  * {
    touch-action: none;
  }
}
#root > div {
  width: 100%;
  min-height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

              
            
!

JS

              
                // * ===========================================================
// # Point
// =========================================================== */
class Point {
  x: number;
  y: number;
  constructor(x: number = 0, y: number = 0) {
    this.x = x || 0;
    this.y = y || 0;
  }
  static distance(a: Point, b: Point) {
    const dx = a.x - b.x;
    const dy = a.y - b.y;
    return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
  }
  static interpolate(p1: Point, p2: Point, t: number) {
    let x = p1.x * (1 - t) + p2.x * t;
    let y = p1.y * (1 - t) + p2.y * t;
    return new Point(x, y);
  }
  add(p: Point) {
    this.x += p.x;
    this.y += p.y;
    return this;
  }
  subtract(p: Point) {
    this.x -= p.x;
    this.y -= p.y;
    return this;
  }
}
// * ===========================================================
// # touch系
// =========================================================== */
const disableTouch = (e: TouchEvent) => {
  e.preventDefault();
};
const touchPosition = (e: React.TouchEvent | React.MouseEvent) => {
  if (e.nativeEvent instanceof MouseEvent) return;
  const points = Object.entries(e.nativeEvent.touches).map(([i, v]: any) => {
    return new Point(v.clientX || 0, v.clientY || 0);
  });
  return points;
};
const touchDeg = (touchPoint: Point[]) => {
  // 2点間の中心を原点
  // その原点とt1(t2でも問題ない)との角度
  const t1 = touchPoint[0];
  const t2 = touchPoint[1];
  const origin = Point.interpolate(t1, t2, 0.5);
  const angle = (Math.atan2(origin.y - t1.y, origin.x - t1.x) * 180) / Math.PI;
  return angle;
};
// * ===========================================================
// # コンポーネント
// =========================================================== */
const App: React.FC = () => {
  const [isDown, setIsDown] = React.useState(false);
  const [startPoints, setStartPoints] = React.useState([new Point(), new Point()]);
  const [tempScale, setTempScale] = React.useState(1.0);
  const [startDeg, setStartDeg] = React.useState(0);
  const [tempRotate, setTempRotate] = React.useState(0);
  const [data, setData] = React.useState({
    x: 0,
    y: 0,
    scale: 1,
    rotate: 0,
  });

  React.useEffect(() => {
    document.addEventListener("touchstart", disableTouch, { passive: false });
    return () => {
      document.removeEventListener("touchstart", disableTouch);
    };
  }, []);

  const onDown = React.useCallback(
    (e: React.TouchEvent | React.MouseEvent) => {
      e.preventDefault();
      if (e.nativeEvent instanceof MouseEvent) return;
      if (e.nativeEvent.touches.length <= 1) return;
      setIsDown(true);
      const start = touchPosition(e);
      const startDeg = touchDeg(start);
      setStartPoints(start);
      setStartDeg(startDeg);
      setTempRotate(+data.rotate);
      setTempScale(+data.scale);
    },
    [isDown, startDeg, tempRotate, data]
  );

  const onMove = React.useCallback(
    (e: React.TouchEvent | React.MouseEvent) => {
      e.preventDefault();
      if (e.nativeEvent instanceof MouseEvent || !isDown) return;
      if (e.nativeEvent.touches.length <= 1) return;
      const currentPoints = touchPosition(e);
      data.rotate = rotate(e);
      data.scale = scale(currentPoints);
      setData({ ...data });
    },
    [isDown, startDeg, tempRotate, data]
  );

  const onUp = React.useCallback((e: React.TouchEvent | React.MouseEvent) => {
    e.preventDefault();
    setIsDown(false);
  }, []);

  const rotate = React.useCallback(
    (e: React.TouchEvent | React.MouseEvent) => {
      const currentPoints = touchPosition(e);
      const currentDeg = touchDeg(currentPoints);
      const diffDeg = currentDeg - startDeg;
      const result = tempRotate + diffDeg;
      return result;
    },
    [startDeg, tempRotate]
  );

  const scale = React.useCallback(
    (currentPoints: Point[]) => {
      const startDistance = Point.distance(startPoints[0], startPoints[1]);
      const currentDistance = Point.distance(currentPoints[0], currentPoints[1]);
      let scale = (currentDistance / startDistance) * tempScale;
      scale = Number.isNaN(scale) ? 1.0 : scale;
      scale = Math.max(scale, 0.5);
      scale = Math.min(scale, 4.0);
      return scale;
    },
    [startPoints, tempScale]
  );

  const transformStyle = React.useMemo(() => {
    return {
      transform: `translate(${data.x}px, ${data.y}px) rotate(${data.rotate}deg) scale(${data.scale})`,
    };
  }, [data]);

  return (
    <div onTouchStart={onDown} onTouchMove={onMove} onTouchEnd={onUp}>
      <img src="https://placehold.jp/500x500.png" alt="これを回転する" style={transformStyle} />
    </div>
  );
};

ReactDOM.render(<App />, document.getElementById("root"));

              
            
!
999px

Console