react项目中实现元素的拖动和缩放实例
在react项目中实现此功能可借助 react-rnd 库,文档地址:https://github.com/bokuweb/react-rnd#Screenshot 。下面是实例运用:
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import assign from 'object-assign'
import classNames from 'classnames'
import _ from 'lodash'
import { Rnd } from 'react-rnd'
import './index.less'
class ScreenShareCapture extends Component {
static propTypes = {
style: PropTypes.object,
}
static defaultProps = {
style: {},
}
state = {
x: 0,
y: 0,
width: 500,
height: 281.25,
}
componentDidMount() {
}
componentWillUnmount() {
}
updateRndElemLastSizePos = () => {
this.rndElemLastSizePos = {
x: this.state.x,
y: this.state.y,
width: this.state.width,
height: this.state.height,
}
}
calculateRndElemSizePos = (dir, ref, sizeDelta, position) => {
const sizePos = _.clone(this.rndElemLastSizePos)
const heightWidthRatio = 0.5625
sizePos.x = position.x
sizePos.y = position.y
sizePos.width = ref.offsetWidth
sizePos.height = ref.offsetWidth * heightWidthRatio
this.setState(sizePos)
return sizePos
}
handleDragStart = () => {
this.updateRndElemLastSizePos()
}
handleDrag = (e, data) => {
this.setState({
x: data.x,
y: data.y,
})
}
handleDragStop = (e, data) => {
this.setState({
x: data.x,
y: data.y,
})
}
handleResizeStart = () => {
this.updateRndElemLastSizePos()
}
handleResize = (e, dir, ref, delta, position) => {
console.log('beibei handleresize', e, dir, ref, delta, position)
this.calculateRndElemSizePos(dir, ref, delta, position)
}
rndElemLastSizePos
rnd
render() {
const wrapStyles = assign({}, this.props.style)
return (
<Rnd
style={wrapStyles}
bounds=".teacher-layout-component-wrap"
dragHandleClassName="screen-share-bottom-content-wrap"
enableResizing={{
top: false,
right: false,
bottom: false,
left: false,
topRight: true,
bottomRight: true,
bottomLeft: true,
topLeft: true,
}}
position={{
x: this.state.x,
y: this.state.y,
}}
size={{
width: this.state.width,
height: this.state.height,
}}
lockAspectRatio={16 / 9}
maxWidth="100%"
maxHeight="100%"
minWidth={400}
minHeight={225}
onDragStart={this.handleDragStart}
onDrag={this.handleDrag}
onDragStop={this.handleDragStop}
onResizeStart={this.handleResizeStart}
onResize={this.handleResize}
onResizeStop={_.noop}
ref={(r) => { this.rnd = r }}
>
<div className="screen-share-capture-component-wrap">
<div className="screen-share-main-content-wrap">
<span className="start-screen-share-button">开始屏幕共享</span>
</div>
<div className="screen-share-bottom-content-wrap">
<div className="screen-share-bottom-left">
<i
className="preview-icon"
/>
<span className="preview-text">预览</span>
</div>
<div className="screen-share-bottom-right">
<i
className={classNames({
'device-icon': true,
'camera-icon': true,
active: false,
})}
/>
<i
className={classNames({
'device-icon': true,
'mic-icon': true,
active: false,
})}
/>
<i
className={classNames({
'device-icon': true,
'speaker-icon': true,
active: false,
})}
/>
<i
className={classNames({
'device-icon': true,
'wifi-icon': true,
active: false,
})}
/>
{/*<i*/}
{/*className={classNames({*/}
{/*'device-icon': true,*/}
{/*'restart-icon': true,*/}
{/*active: false,*/}
{/*})}*/}
{/*/>*/}
<i
className={classNames({
'device-icon': true,
'exit-icon': true,
active: false,
})}
/>
</div>
</div>
</div>
</Rnd>
)
}
}
export default ScreenShareCapture
下面是css样式:
.screen-share-capture-component-wrap{
box-sizing: border-box;
width: 100%;
height: 100%;
position: relative;
//top:;
//left:;
//width: 100%;
//height: 100%;
//display: flex;
//flex-direction: column;
//align-items: center;
//justify-content: center;
//background-color: red;
.screen-share-main-content-wrap{
box-sizing: border-box;
width: 100%;
height: 100%;
border-radius: 2px;
border: solid 2px #ff4343;
background-color: #ffffff;
display: flex;
justify-content: center;
align-items: center;
.start-screen-share-button{
display: inline-block;
width: 120px;
height: 36px;
line-height: 36px;
text-align: center;
border-radius: 2px;
background-color: #ff4343;
color: #ffffff;
font-size: 14px;
opacity: 0.8;
cursor: pointer;
}
}
.screen-share-bottom-content-wrap{
width: 100%;
height: 36px;
border-radius: 2px;
background-color: #5d6674;
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
position: absolute;
bottom: -36px;
left:;
.screen-share-bottom-left{
width: 72px;
height: 32px;
border-radius: 2px;
border: solid 1px #ffffff;
background-color: #5d6674;
margin: 3px;
display: flex;
flex-direction: row;
justify-content: space-around;
align-items: center;
cursor: pointer;
.preview-icon{
width: 24px;
height: 24px;
outline: none;
cursor: pointer;
background: url('~ROOT/shared/assets/image/vn-screen-preview-42-42.png') no-repeat center;
background-size: 16px 16px;
}
.preview-text{
color: #ffffff;
font-size: 14px;
}
}
.screen-share-bottom-right{
display: flex;
flex-direction: row;
align-items: center;
.device-icon {
width: 24px;
height: 24px;
outline: none;
cursor: pointer;
margin-right: 11px;
&:nth-last-child(2){
margin-right: 38px;
}
}
.camera-icon {
background: url('~ROOT/shared/assets/image/vn-screen-camera-off-66-66.png') no-repeat center;
background-size: 22px 22px;
}
.mic-icon {
background: url('~ROOT/shared/assets/image/vn-screen-voice-on-54-72.png') no-repeat center;
background-size: 18px 24px;
}
.speaker-icon {
background: url('~ROOT/shared/assets/image/vn-screen-speaker-60-66.png') no-repeat center;
background-size: 20px 22px;
}
.wifi-icon {
background: url('~ROOT/shared/assets/image/vn-screen-wifi-72-57.png') no-repeat center;
background-size: 24px 19px;
}
.restart-icon{
background: url('~ROOT/shared/assets/image/vn-screen-restart-60-60.png') no-repeat center;
background-size: 19px 19px;
}
.exit-icon{
background: url('~ROOT/shared/assets/image/vn-screen-exit-60-60.png') no-repeat center;
background-size: 19px 19px;
}
}
}
}
react项目中实现元素的拖动和缩放实例的更多相关文章
- 如何在非 React 项目中使用 Redux
本文作者:胡子大哈 原文链接:https://scriptoj.com/topic/178/如何在非-react-项目中使用-redux 转载请注明出处,保留原文链接和作者信息. 目录 1.前言 2. ...
- react项目中实现搜索关键字呈现高亮状态(一)
最近有个需求,在一个react项目中,实现搜索关键字呈现高亮状态.这个在普通的html文件中还好操作些,在react项目中有点懵逼了,因为react项目中很少操作dom,有点无从下手.但最后还是实现了 ...
- 如何优雅地在React项目中使用Redux
前言 或许你当前的项目还没有到应用Redux的程度,但提前了解一下也没有坏处,本文不会安利大家使用Redux 概念 首先我们会用到哪些框架和工具呢? React UI框架 Redux 状态管理工具,与 ...
- React项目中实现右键自定义菜单
最近在react项目中需要实现一个,右键自定义菜单功能.找了找发现纯react项目里没有什么工具可以实现这样的功能,所以在网上搜了搜相关资料.下面我会附上完整的组件代码. (注:以下代码非本人原创,具 ...
- React项目中使用Mobx状态管理(二)
并上一节使用的是普通的数据状态管理,不过官方推荐使用装饰器模式,而在默认的react项目中是不支持装饰器的,需要手动启用. 官方参考 一.添加配置 官方提供了四种方法, 方法一.使用TypeScrip ...
- 在react项目中使用ECharts
这里我们要在自己搭建的react项目中使用ECharts,我们可以在ECharts官网上看到有一种方式是在 webpack 中使用 ECharts,我们需要的就是这种方法. 我们在使用ECharts之 ...
- 优雅的在React项目中使用Redux
概念 首先我们会用到哪些框架和工具呢? React UI框架 Redux 状态管理工具,与React没有任何关系,其他UI框架也可以使用Redux react-redux React插件,作用:方便在 ...
- 深入浅出TypeScript(5)- 在React项目中使用TypeScript
前言 在第二小节中,我们讨论了利用TypeScript创建Web项目的实现,在本下节,我们讨论一下如何结合React创建一个具备TypeScript类型的应用项目. 准备 Webpack配置在第二小节 ...
- redux在react项目中的应用
今天想跟大家分享一下redux在react项目中的简单使用 1 1.redux使用相关的安装 yarn add redux yarn add react-redux(连接react和redux) 2. ...
随机推荐
- Keras RetinaNet github项目安装
在存储库目录/keras-retinanet/中,执行pip install . --user 后,出现错误: D:\>cd D:\JupyterWorkSpace\keras-retinane ...
- 在C#中,Newtonsoft.Json + dynamic动态解析jsonString,jsonString转实体
记录一下 引用 using Newtonsoft.Json; using Newtonsoft.Json.Linq; var jsonString = "{\"ApiResourc ...
- 从零开始一起学习SLAM | 点云到网格的进化
点击公众号"计算机视觉life"关注,置顶星标更快接收消息! 本文编程练习框架及数据获取方法见文末获取方式 菜单栏点击"知识星球"查看「从零开始学习SLAM」一 ...
- Js数组去重方法总结
//方法一 var arr = [1,23,1,1,1,3,23,5,6,7,9,9,8,5]; function removeDuplicatedItem(arr) { for(var i = 0; ...
- sql多行多列重复
在sql的查询中我们会遇到查询的结果比如这样的: 查询这张表的sql语句: select r.ROLE_NAME,u.USERID,u.USERNAME,u.TrueName from BASE_ ...
- jpa报错:Table 'dev-test.hibernate_sequence' doesn't exist
Hibernate 能够出色地自动生成主键.Hibernate/EBJ 3 注释也可以为主键的自动生成提供丰富的支持,允许实现各种策略.其生成规则由@GeneratedValue设定的.这里的@id和 ...
- Vue系列之 => 使用webpack-dev-server工具实现自动打包编译
安装webpack-dev-server (webpack版本3.6.0,webpack-dev-server版本2.11.3)注意版本兼容问题,不然会有N多错误. npm i webpack-dev ...
- Linux 系统的启动过程
1.基本概念 BIOS 中文名 主板BIOS 外文名 Basic Input/Output System 全 称 基本输入输出系统(全称是ROM-BIOS,是只读存储器基本输入/输出 ...
- Java基础语法-Lambda表达式
1.Lambda表达式主要用于简写接口的操作等出现匿名内部类的地方,如下:我这里先定义一个Swim接口,以及它所定义的swim()方法 interface Swim{ void swim(); } 2 ...
- Java面试题整理---Redis篇
1.redis支持五种数据结构类型? 2.redis内部结构? 3.redis持久化机制? 4.redis集群方案与实现? 5.redis为什么是单线程的? 6.redis常见回收 ...