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. ...
随机推荐
- go语言生成一张正弦图
先看效果: package main import ( "image" "image/color" "image/png" "lo ...
- SQL学习基础 => 创建表
--创建表 CREATE TABLE userinfo3 ( ID INT PRIMARY KEY NOT NULL, --ID 整数类型,设置为主键,并且不能为空 name ) NOT NULL, ...
- Beautiful Soup 解析html表格
from bs4 import BeautifulSoup import urllib.request doc = urllib.request.urlopen('http://www.bkzy.or ...
- 让网站永久拥有HTTPS - 申请免费SSL证书并自动续期 Let’s Encrypt
让网站永久拥有HTTPS - 申请免费SSL证书并自动续期 Let’s Encrypt 为什么要用HTTPS 网站没有使用HTTPS的时候,浏览器一般会报不安全,而且在别人访问这个网站的时候,很有可 ...
- C# Thread.Jion()
什么是进程? 当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源. 而一个进程又是由多个线程所组成的. 什么是线程? 线程是程序中的一个执行流,每个线程都有自己的 ...
- LINUX 编程定位工具gstack,pstack
pstack: pstack命令可显示每个进程的栈跟踪. pstack 命令必须由相应进程的属主或 root 运行. 可以使用 pstack 来确定进程挂起的位置. 此命令允许使用的唯一选项是要检查的 ...
- java线程学习之Sleep方法
sleep方法是在线程中常用到的一个方法,它是一个静态方法. sleep(long millis) 在指定的毫秒数内让当前正在执行的线程休眠(暂停执行),此操作受到系统计时器和调度程序精度 ...
- c# 将CSV文件转成list集合
//定义CSV文件所对应的实体类 public class example { public int t1; public string t2; public string t3; public st ...
- 使用session和cookie实现用户登录:一个登录页面,一个servlet,一个登录成功页面
文件目录 1.登录页面 <%@ page language="java" contentType="text/html; charset=utf-8" p ...
- 自娱自乐RN版小说APP历程记录
当前rn版本 "react": "16.6.3" "react-native": "0.58.5" 通过react-na ...