首页 > 其他分享 >react定义图片预览

react定义图片预览

时间:2023-03-17 10:14:30浏览次数:33  
标签:const 定义 预览 imgScale react state parseInt imgCurrent setState

1.控制图片的信息

imgScale: '100%', // 图片放大缩小
imgTransform: '', // 旋转
imgCurrent: 0, // 当前的旋转
x: 0, // x移动
y: 0, // y移动  

2.执行用的函数

     // 放大
    imgToBigger() {
      this.setState({ imgScale: `${parseInt(this.state.imgScale, 10) + 25}%` });
    }

    // 缩小
    imgToSmaller() {
      this.setState({ imgScale: `${parseInt(this.state.imgScale, 10) - 25}%` });
    }

    // 滚轮缩放
    handleZoom(e) {
      if (e.nativeEvent.deltaY <= 0) {
        this.setState({ imgScale: `${parseInt(this.state.imgScale, 10) + 18}%` }); // 图片宽度每次增加18
      } else if (e.nativeEvent.deltaY > 0) {
        this.setState({ imgScale: `${parseInt(this.state.imgScale, 10) - 18 > 0 ? parseInt(this.state.imgScale, 10) - 18 : parseInt(this.state.imgScale, 10)}%` }); // 图片宽度
      }
    }

    // 拖拽
    moveImg = (ev) => {
      const { x, y } = this.state;
      ev.preventDefault();
      const disx = ev.pageX - x;
      const disy = ev.pageY - y;
      document.onmousemove = (e) => {
        this.setState({
          x: e.pageX - disx,
          y: e.pageY - disy,
        });
      };
      document.onmouseup = () => {
        document.onmousemove = null;
        document.onmousedown = null;
      };
    }

    // 左旋转
    imgToLeftRoll() {
      const a = (this.state.imgCurrent - 90) % 360;
      this.setState({ imgTransform: `rotate(${a}deg)`, imgCurrent: a });
    }

    // 右旋转
    imgToRightRoll() {
      const a = (this.state.imgCurrent + 90) % 360;
      this.setState({ imgTransform: `rotate(${a}deg)`, imgCurrent: a });
    }

3.运用的组件

             <Modal
                width="auto"
                centered="true"
                overflow="hidden"
                visible={previewVisible}
                footer={[
                  <scmp.Div margin="0 auto" textAlign="center" key="Modal">
                    <scmp.AntdButton
                      border="none"
                      padding="5px 8px"
                      title="amplify"
                      onClick={() => this.imgToBigger()} // 点击放大
                    >
                      <ZoomInOutlined />
                    </scmp.AntdButton>
                    <scmp.AntdButton
                      border="none"
                      padding="5px 8px"
                      title="reduce"
                      onClick={() => this.imgToSmaller()} // 点击缩小
                    >
                      <ZoomOutOutlined />
                    </scmp.AntdButton>
                    <scmp.AntdButton
                      border="none"
                      padding="5px 8px"
                      title="Counterclockwise rotation"
                      onClick={() => this.imgToLeftRoll()} // 左旋转
                    >
                      <UndoOutlined />
                    </scmp.AntdButton>
                    <scmp.AntdButton
                      border="none"
                      padding="5px 8px"
                      title="Clockwise rotation"
                      onClick={() => this.imgToRightRoll()} // 右旋转
                    >
                      <RedoOutlined />
                    </scmp.AntdButton>
                  </scmp.Div>,
                ]}
                onCancel={this.handleCancel} // 作废的时候把改的参数还原
                title="Preview"
              >
                <div
                  width="800px"
                  height="600px"
                  position="relative"
                  onm ouseDown={this.moveImg} // 拖拽事件
                  onWheel={this.handleZoom} // 缩放事件
                >
                  <div
                    width="wrap_content"
                    height="wrap_content"
                    position="absolute"
                    top="50%"
                    left="50%"
                    transform="translate(-50%, -50%)"
                  >
                    <img
                      width="wrap_content"
                      maxWidth="800px"
                      height="wrap_content"
                      position="relative"
                      // onm ouseMove={this.handleMouseMove}
                      style={{
                        imageRendering: 'pixelated',
                        scale: imgScale,
                        transform: imgTransform,
                        left: `${x}px`,
                        top: `${y}px`,
                        // transformOrigin: `${centreX}px ${centreY}px`,
                      }}
                      alt="example"
                      src={previewImage}
                    />
                  </div>
                </div>
              </Modal>

 

标签:const,定义,预览,imgScale,react,state,parseInt,imgCurrent,setState
From: https://www.cnblogs.com/Dluea/p/17225593.html

相关文章

  • react hooks + ts 封装组件
    reacthooks+ts组件封装简介在react使用ts封装组件,需要注意类型,使用forwardRef方法包起来子组件import*asReactfrom"react";import"./style.scss";interf......
  • Android Studio预览Markdown文件办法
     01、安装Markdown插件打开File>>Settings>>Plugins>>Marketplace菜单,输入Markdown搜索,点击Install安装。02、下载ChooseRuntime插件JetBrains插件市场......
  • ALV带有自定义事件
    *&---------------------------------------------------------------------**&ReportZ2023015105*&*&---------------------------------------------------------......
  • PebbleTemplates 自定义tag&filter&function 开发简单说明
    PebbleTemplates的扩展能力还是很强大的,开发起来也比较方便,以下是一个简单的说明,更加复杂的推荐查看官方源码学习filter开发参考代码此filter直接返回rongfenglian......
  • leaflet 自定义事件
    leaflet 自定义事件主要使用fire()方法,如下图所示,我在区域热力层点击方法中自定义一个handClick方法,并传值。  再去进行监听  预览单击就会触发我们自定义的方......
  • 五、自定义登录逻辑
    ​ 当进行自定义登录逻辑时需要用到之前讲解的UserDetailsService和PasswordEncoder。但是SpringSecurity要求:当进行自定义登录逻辑时容器内必须有PasswordEncoder实例......
  • 五、自定义登录逻辑
    ​ 当进行自定义登录逻辑时需要用到之前讲解的UserDetailsService和PasswordEncoder。但是SpringSecurity要求:当进行自定义登录逻辑时容器内必须有PasswordEncoder实例......
  • react警告:Warning: [antd: Menu] children will be removed in next major version. P
    这是由于antv4.20+优化了导航菜单Menu的使用方式,采用优化后的方式使用菜单组件就可以了。更新前:<MenuonClick={this.handleClick}style={{wi......
  • Vue 引入PDF文件预览,可下载,插件vue-pdf
    最近的一个项目中用到了读取本地文件PDF,先是用了PDF组件,没有达到想要的效果,后来查找官网用iframe标签来替代PDF组件,达到想要的效果了, 1、第一步先安装vue-pdf插件......
  • cnblogs 博客园自定义样式
    在博客园搜索资料时,发现很多博友的园子装饰的非常炫酷。想着也装饰一下自己的园子,搜索后发现一个不错的插件,记录一下。使用、配置教程:https://bndong.github.io/Cnblog......