【前端】es-drager 图片同比缩放 缩放比 只修改宽 只修改高

【前端】es-drager 图片同比缩放 缩放比

ES Drager 拖拽组件 (vangleer.github.io)

核心代码

//初始宽
let width = ref(108)

//初始高
let height = ref(72)


//以下两个变量 用来区分是单独的修改宽 还是高 或者是同比
//缩放开始时的宽
let oldWidth = 0
//缩放开始时的高
let oldHeight = 0



// resize start   缩放开始
const onResizeStart = (dragData: DragData) => {

  //初始化old宽高  用来区分用户拖拽的哪个点
  oldWidth = dragData.width
  oldHeight = dragData.height

  
  console.log('缩放开始', dragData)
}

 
// resizing 缩放中...
const onResize = (dragData: DragData) => {

  
  if(dragData.width == oldWidth && dragData.height == oldHeight){
      return
  }
  
  if(dragData.width == oldWidth){
    
    //只改变高
    console.log('缩放中...只改变高', dragData)
    
  }else if(dragData.height == oldHeight){
    //只改变宽
    console.log('缩放中...只改变宽', dragData)
  }else{
    //同比
     console.log('缩放中...同比缩放', dragData)

    let 缩放比例 = (dragData.width / oldWidth)
    //进行同比缩放
//如果画布有缩放比 这里也可以用 oldHeight 或者 用缩放比之后的高度
 //dragData.height =  oldHeight * 缩放比例
    dragData.height =  height.value * 缩放比例
  }
 
}

全部代码

<template>
  <Drager
      :width="width"
      :height="height"
      :left="100"
      :top="100"
      rotatable
      @change="onChange"
      @drag="onDrag"
      @drag-start="onDragStart"
      @drag-end="onDragEnd"
      @resize="onResize"
      @resize-start="onResizeStart"
      @resize-end="onResizeEnd"
      @rotate="onRotate"
      @rotate-start="onRotateStart"
      @rotate-end="onRotateEnd"
      @focus="onFocus"
      @blur="onBlur"
  />
</template>

<script setup lang="ts">
import Drager, { type DragData } from 'es-drager'
import { ref } from 'vue';

let width = ref(108)
let height = ref(72)

let oldWidth = 0
let oldHeight = 0

// let width = 100
// let height = 100

// @change="onChange"
// drag、resize、rotate
const onChange = (dragData: DragData) => {
  console.log('onChange', dragData)
}

// draging
const onDrag = (dragData: DragData) => {
  console.log('onDrag', dragData)
}
// drag start
const onDragStart = (dragData: DragData) => {
  
  //拖拽开始
  
  console.log('拖拽开始', dragData)
}
// drag end
const onDragEnd = (dragData: DragData) => {
  console.log('onDragEnd', dragData)
}



// resizing
const onResize = (dragData: DragData) => {

  
  if(dragData.width == oldWidth && dragData.height == oldHeight){
      return
  }
  
  if(dragData.width == oldWidth){
    
    //只改变高
    console.log('缩放中...只改变高', dragData)
    
  }else if(dragData.height == oldHeight){
    //只改变宽
    console.log('缩放中...只改变宽', dragData)
  }else{
    //同比
     console.log('缩放中...同比缩放', dragData)

    let 缩放比例 = (dragData.width / width.value)
    //进行同比缩放
    dragData.height =  height.value * 缩放比例
  }
 
}
// resize start
const onResizeStart = (dragData: DragData) => {

  oldWidth = dragData.width
  oldHeight = dragData.height
  
  console.log('缩放开始', dragData)
}
// resize end
const onResizeEnd = (dragData: DragData) => {
  console.log('缩放结束', dragData)
}

// rotating
const onRotate = (dragData: DragData) => {
  console.log('onRotate', dragData)
}
// rotate start
const onRotateStart = (dragData: DragData) => {
  console.log('onRotateStart', dragData)
}
// resize end
const onRotateEnd = (dragData: DragData) => {
  console.log('onRotateEnd', dragData)
}

// focus/selected
function onFocus(val: boolean) {
  console.log('onFocus', val)
}
// blur/unselected
function onBlur(val: boolean) {
  console.log('onBlur', val)
}
</script>

相关推荐

  1. 前端】css控制背景图片

    2024-04-10 13:32:02       32 阅读
  2. 前端】原生实现图片的放大与

    2024-04-10 13:32:02       25 阅读
  3. Qt获取屏幕DPI

    2024-04-10 13:32:02       77 阅读
  4. vue中的使用

    2024-04-10 13:32:02       23 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-10 13:32:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-10 13:32:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-10 13:32:02       82 阅读
  4. Python语言-面向对象

    2024-04-10 13:32:02       91 阅读

热门阅读

  1. 网络工程师练习题(12)

    2024-04-10 13:32:02       29 阅读
  2. js手写并发请求控制

    2024-04-10 13:32:02       39 阅读
  3. Accuracy准确率,Precision精确率,Recall召回率,F1 score

    2024-04-10 13:32:02       35 阅读
  4. 属于我们Go语言的toString!

    2024-04-10 13:32:02       33 阅读
  5. golang 中 sync包

    2024-04-10 13:32:02       35 阅读