4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【React】React Zoom Pan Pinch を使ってみた

Posted at

はじめに

この記事では、React 用の Zoom / Pan / Pinch ライブラリである React Zoom Pan Pinch の利用手順を記載します。

開発環境

開発環境は以下の通りです。

  • Windows 11
  • React 18.3.1
  • TypeScript 5.5.3
  • React Zoom Pan Pinch 3.6.1

インストール

まずは以下のコマンドでインストールします。

npm install --save react-zoom-pan-pinch

基本的な利用方法

TransformWrapperTransformComponent で Zoom / Pan / Pinch したい対象を囲むと実現できます。

以下の例では、img タグを囲んでいます。

sample.tsx
import { FC } from "react";
import { TransformComponent, TransformWrapper } from "react-zoom-pan-pinch";

export const ZoomPanPinch: FC = () => {
  return (
    <TransformWrapper>
      <TransformComponent>
        <img src="./snowman.jpg" alt="snowman" />
      </TransformComponent>
    </TransformWrapper>
  );
};

動作確認をします。

Vite + React + TS - Google Chrome 2024-12-03 09-31-41.gif

Zoom / Pan / Pinch の対象

img タグ以外も Zoom / Pan / Pinch することができます。

テキスト

sample.tsx
import { FC } from "react";
import { TransformComponent, TransformWrapper } from "react-zoom-pan-pinch";

export const ZoomPanPinch: FC = () => {
  return (
    <TransformWrapper>
      <TransformComponent>
        <h1>Zoom Pan Pinch</h1>
      </TransformComponent>
    </TransformWrapper>
  );
};

Vite-React-TS-Google-Chrome-2024-12-03-08-48-19.gif

SVG

sample.tsx
import { FC } from "react";
import { TransformComponent, TransformWrapper } from "react-zoom-pan-pinch";

export const ZoomPanPinch: FC = () => {
  return (
    <TransformWrapper>
      <TransformComponent>
        <svg width="600" height="400" xmlns="http://www.w3.org/2000/svg">
          <image href="./snowman.jpg" width="600" height="400" />
        </svg>
      </TransformComponent>
    </TransformWrapper>
  );
};

Vite-React-TS-Google-Chrome-2024.gif

参考

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?