ES Drager Drag Component

November 18, 2024 ยท View on GitHub

ES Drager
Draggable Resizable Rotatable

๐ŸŒˆ Introduction

A component for draggable, resizable, and rotatable elements based on vue3.x + CompositionAPI + TypeScript + Vite.

  • Draggable & Area Drag
  • Supports resizing
  • Rotation
  • Grid-based drag and resize

Comprehensive Example

Below is a simple visual drag-and-drop editor implemented using es-drager.

ES Drager Editor

โšก Usage Instructions

Install Dependencies

npm i es-drager

Global Registration

import { createApp } from 'vue'
import App from './App.vue'

import 'es-drager/lib/style.css'
import Drager from 'es-drager'

createApp(App)
  .component('es-drager', Drager)
  .mount('#app')
  • Usage
<template>
  <es-drager>
    drager
  </es-drager>
</template>

Use Directly in Components

<template>
  <Drager>
    drager
  </Drager>
</template>

<script setup lang='ts'>
import Drager from 'es-drager'
</script>

Browser Direct Import

Directly import es-drager using the HTML script tag, then you can use the global variable ESDrager.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="https://unpkg.com/es-drager/lib/style.css">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <es-drager>drager</es-drager>
  </div>

  <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  <script src="https://unpkg.com/es-drager"></script>
  <script>
    const { createApp } = Vue
    const app = createApp({})
    app.use(ESDrager)
    app.mount('#app')
  </script>
</body>
</html>

Drager API

Drager Properties

Property NameDescriptionTypeDefault
tagComponent's is attribute^[string]div
typerect, text, image^[string]rect
widthWidth^[number]100
heightHeight^[number]100
leftHorizontal offset^[number]0
topVertical offset^[number]0
angleRotation angle^[number]0
colorColor^[string]#3a7afe
resizableResizability^[boolean]true
rotatableRotatability^[boolean]-
boundaryCheck boundary (nearest positioned parent)^[boolean]-
disabledDisable^[boolean]-
minWidthMinimum width^[number]-
minHeightMinimum height^[number]-
maxWidthMaximum width^[number]-
maxHeightMaximum height^[number]-
selectedControl selection^[boolean]-
checkCollisionEnable collision detection^[boolean]-
snapToGridEnable grid^[boolean]-
gridXGrid size in X^[number]50
gridYGrid size in Y^[number]50
snapOpen adsorption^[boolean]-
snapThresholdAdsorption threshold^[number]10
marklinemarkline(custom)^[boolean]^
scaleRatioScaling ratio^[number]1
disabledKeyEventDisable arrow key movement^[boolean]-
borderShow border^[boolean]true
aspectRatioWidth-to-height ratio^[boolean]-
equalProportionEqual width and height^[boolean]-
resizeListDisplayed resize handle list, e.g., top, bottom, left, right, top-left, top-right, bottom-left, bottom-right^[string[]]-

Drager Events

Event NameDescriptionType
changePosition and size change^[Function](dragData) => void
dragDragging in progress^[Function](dragData) => void
drag-startDrag start^[Function](dragData) => void
drag-endDrag end^[Function](dragData) => void
resizeResizing in progress^[Function

](dragData) => void | | resize-start | Resize start | ^[Function](dragData) => void | | resize-end | Resize end | ^[Function](dragData) => void | | rotate | Rotation in progress | ^[Function](dragData) => void | | rotate-start | Rotation start | ^[Function](dragData) => void | | rotate-end | Rotation end | ^[Function](dragData) => void |

  • dragData type
export type DragData = {
  width: number
  height: number
  left: number
  top: number
  angle: number
}

Drager Slots

Slot NameDescription
defaultCustom default content
resizeResize handle
rotateRotate handle