moveable README

The project's README from github.com/daybrush/moveable. As one markdown file: README.md.

Moveable

npm version Github actions React Preact Angular Vue Vue 3 Svelte Lit

Moveable is Draggable, Resizable, Scalable, Rotatable, Warpable, Pinchable, Groupable, Snappable

Github / Demo / Storybook / API / Main Project

Moveable
Draggable Resizable Scalable Rotatable
Warpable Pinchable Groupable Snappable
Clippable Roundable OriginDraggable Selecto

🔥 Features

⚙️ Installation

npm

$ npm i moveable

scripts

<script src="//daybrush.com/moveable/release/latest/dist/moveable.min.js"></script>

📄 Documents

🚀 How to use

import Moveable from "moveable";

const moveable = new Moveable(document.body, {
    target: document.querySelector(".target"),
    // If the container is null, the position is fixed. (default: parentElement(document.body))
    container: document.body,
    draggable: true,
    resizable: true,
    scalable: true,
    rotatable: true,
    warpable: true,
    // Enabling pinchable lets you use events that
    // can be used in draggable, resizable, scalable, and rotateable.
    pinchable: true, // ["resizable", "scalable", "rotatable"]
    origin: true,
    keepRatio: true,
    // Resize, Scale Events at edges.
    edge: false,
    throttleDrag: 0,
    throttleResize: 0,
    throttleScale: 0,
    throttleRotate: 0,
});
/* draggable */
moveable.on("dragStart", ({ target, clientX, clientY }) => {
    console.log("onDragStart", target);
}).on("drag", ({
    target, transform,
    left, top, right, bottom,
    beforeDelta, beforeDist, delta, dist,
    clientX, clientY,
}) => {
    console.log("onDrag left, top", left, top);
    target!.style.left = `${left}px`;
    target!.style.top = `${top}px`;
    // console.log("onDrag translate", dist);
    // target!.style.transform = transform;
}).on("dragEnd", ({ target, isDrag, clientX, clientY }) => {
    console.log("onDragEnd", target, isDrag);
});

/* resizable */
moveable.on("resizeStart", ({ target, clientX, clientY }) => {
    console.log("onResizeStart", target);
}).on("resize", ({ target, width, height, dist, delta, clientX, clientY }) => {
    console.log("onResize", target);
    delta[0] && (target!.style.width = `${width}px`);
    delta[1] && (target!.style.height = `${height}px`);
}).on("resizeEnd", ({ target, isDrag, clientX, clientY }) => {
    console.log("onResizeEnd", target, isDrag);
});

/* scalable */
moveable.on("scaleStart", ({ target, clientX, clientY }) => {
    console.log("onScaleStart", target);
}).on("scale", ({
    target, scale, dist, delta, transform, clientX, clientY,
}: OnScale) => {
    console.log("onScale scale", scale);
    target!.style.transform = transform;
}).on("scaleEnd", ({ target, isDrag, clientX, clientY }) => {
    console.log("onScaleEnd", target, isDrag);
});

/* rotatable */
moveable.on("rotateStart", ({ target, clientX, clientY }) => {
    console.log("onRotateStart", target);
}).on("rotate", ({ target, beforeDelta, delta, dist, transform, clientX, clientY }) => {
    console.log("onRotate", dist);
    target!.style.transform = transform;
}).on("rotateEnd", ({ target, isDrag, clientX, clientY }) => {
    console.log("onRotateEnd", target, isDrag);
});

/* warpable */
this.matrix = [
    1, 0, 0, 0,
    0, 1, 0, 0,
    0, 0, 1, 0,
    0, 0, 0, 1,
];
moveable.on("warpStart", ({ target, clientX, clientY }) => {
    console.log("onWarpStart", target);
}).on("warp", ({
    target,
    clientX,
    clientY,
    delta,
    dist,
    multiply,
    transform,
}) => {
    console.log("onWarp", target);
    // target.style.transform = transform;
    this.matrix = multiply(this.matrix, delta);
    target.style.transform = `matrix3d(${this.matrix.join(",")})`;
}).on("warpEnd", ({ target, isDrag, clientX, clientY }) => {
    console.log("onWarpEnd", target, isDrag);
});

/* pinchable */
// Enabling pinchable lets you use events that
// can be used in draggable, resizable, scalable, and rotateable.
moveable.on("pinchStart", ({ target, clientX, clientY }) => {
    // pinchStart event occur before dragStart, rotateStart, scaleStart, resizeStart
    console.log("onPinchStart");
}).on("pinch", ({ target, clientX, clientY, datas }) => {
    // pinch event occur before drag, rotate, scale, resize
    console.log("onPinch");
}).on("pinchEnd", ({ isDrag, target, clientX, clientY, datas }) => {
    // pinchEnd event occur before dragEnd, rotateEnd, scaleEnd, resizeEnd
    console.log("onPinchEnd");
});

📦 Packages

⚙️ Developments

The moveable repo is managed as a monorepo with yarn.

yarn config set registry https://registry.npmjs.org/

The main project was made with react and I used croact to make it lighter with umd.

For development and testing, check in packages/react-moveable.

npm run storybook

$ yarn
$ npm run packages:build
$ npm run storybook

Runs the app in the development mode.
Open http://localhost:6006 to view it in the browser.

The page will reload if you make edits.
You will also see any lint errors in the console.

⭐️ Show Your Support

Please give a ⭐️ if this project helped you!

👏 Contributing

If you have any questions or requests or want to contribute to moveable or other packages, please write the issue or give me a Pull Request freely.

Code Contributors

This project exists thanks to all the people who contribute. [Contribute].

🐞 Bug Report

If you find a bug, please report to us opening a new Issue on GitHub.

Sponsors

Open Collective Financial Contributors

Become a financial contributor and help us sustain our community. [Contribute]

Individuals

Organizations

Support this project with your organization. Your logo will show up here with a link to your website. [Contribute]

📝 License

This project is MIT licensed.

MIT License

Copyright (c) 2019 Daybrush

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

moveable (daybrush/moveable)

Searching Database…

10.8K ·GitHub
这是什么?Moveable 是一个 JavaScript/TypeScript 库,可以让 DOM 和 SVG 元素实现拖拽、调整大小、缩放、旋转、变形、双指缩放、分组和吸附,并提供现成的 React、Vue、Vue 3、Angular、Svelte、Lit 和 Preact 组件。
这是单个组件,还是一组针对不同框架的包?它是一个多包代码仓库(monorepo),核心是一个原生 JS 组件(moveable),另外在 npm 上单独发布了轻量的框架封装包:react-moveable、vue-moveable、vue3-moveable、ngx-moveable、svelte-moveable、lit-moveable 和 preact-moveable。
我该怎么把它加到我的项目里?安装与你的技术栈对应的 npm 包(例如 react-moveable、vue-moveable、vue3-moveable、ngx-moveable),然后用 Moveable 组件包裹你要操作的目标元素。
它会引入其他运行时库吗?核心包除了 tslib 之外基本没有第三方运行时依赖,因此非常轻量。
有没有需要注意的命名陷阱?Moveable 所有控制框和以 'able' 命名的元素 class 都使用 moveable- 前缀,所以你不能在自己的目标元素 class 名称中使用同样的前缀,否则会发生冲突。
它在所有主流浏览器中都能用吗?可以。它的设计支持主流浏览器,而且不只是基础的 2D 定位,也支持 3D CSS 变换。
它支持 SVG 元素,而不只是 HTML 的 div 吗?支持。Moveable 明确支持 SVG 元素,例如 svg、path、line、ellipse、g 和 rect,同时也支持普通的 HTML 元素。
能把多个元素当作一个整体一起移动吗?可以。Groupable 功能让你可以把多个目标作为一个组,一起拖拽、调整大小、缩放和旋转。
它能自动把元素吸附到对齐参考线上吗?可以。Snappable 功能让元素在被拖拽、调整大小或旋转时,自动吸附到已配置的参考线上。
为什么不用一个通用包,而要拆成这么多 npm 包?核心的原生 'moveable' 组件为每个框架(React、Vue、Vue 3、Angular、Svelte、Lit、Preact)分别做了封装,让每个生态都能获得原生、轻量的组件,而不是一个通用适配器。
Creator
daybrush
Size
79.1 MB
Stars
10.8K
Age
7y
Commits · 90 Days