记录react 视频和 预览拖动

news/2024/10/17 13:37:09/

一、react 视频

##1、循环播放

import React, { useEffect, useState, useRef } from "react";
const videoRef= useRef(null);
const showVideoClass = { display: "block", width: "100%", height: "100%" }
const hindVideoClass = { display: "none", width: "100%", height: "100%" }//布局中使用: 主要是 loop 
<videostyle={showVideoClass}src={require("../../../static/video/0.mp4")}mutedloopautoPlayref={videoRef}/>

##2、两个视频交替播放

import React, { useEffect, useState, useRef } from "react";const oneVideo = "/static/video/1.mp4";
const twoVideo ="/static/video/2.mp4";function VideoPlay() {const [vidIndex, setVidIndex] = useState(0);const oneRef = useRef(null);const twoRef = useRef(null);useEffect(() => {console.log("当前index",vidIndex)if (vidIndex === 0 && oneRef.current) {oneRef.current.play();}if (vidIndex === 1 && twoRef.current) {twoRef.current.play();}}, [oneRef,twoRef, vidIndex]);return (<div className="videoShowDiv"><videostyle={{ display: vidIndex === 1 ? "none" : "block", width: "100%", height: "100%" }}src={oneVideo }mutedonEnded={() => setVidIndex((idx) => idx + 1)}ref={oneRef}/><videostyle={{ display: vidIndex === 0 ? "none" : "block", width: "100%", height: "100%" }}src={twoVideo}mutedonEnded={() => setVidIndex((idx) => idx -1)}ref={twoRef}/></div>);
}export default VideoPlay

 ##3、选择播放

import React, { useEffect, useState, useRef } from "react";function VideoPlay(props) {const { menuClickIndex } = props;const [vidIndex, setVidIndex] = useState(0);const oneRef = useRef(null);const twoRef = useRef(null);useEffect(() => {console.log("当前index", vidIndex)console.log("当前menuClickIndex", menuClickIndex)//menuClickIndex 为指定数,视频就指定播放那个视频if (menuClickIndex != undefined) {if (menuClickIndex == 0) {if (oneRef.current) {oneRef.current.play();}}if (menuClickIndex == 1) {if (twoRef.current) {twoRef.current.play();}}}//menuClickIndex 为空,视频就循环切换else {if (vidIndex === 0 && oneRef.current) {oneRef.current.play();}if (vidIndex === 1 && twoRef.current) {twoRef.current.play();}}}, [oneRef, twoRef, vidIndex]);const showVideoClass = { display: "block", width: "100%", height: "100%" }const hindVideoClass = { display: "none", width: "100%", height: "100%" }return (<div>{menuClickIndex == undefined &&<div className="videoShowDiv"><videostyle={{ display: vidIndex === 1 ? "none" : "block", width: "100%", height: "100%" }}src={require("../../../static/video/one.mp4")}mutedonEnded={() => setVidIndex((idx) => idx + 1)}ref={oneRef}/><videostyle={{ display: vidIndex === 0 ? "none" : "block", width: "100%", height: "100%" }}src={require("../../../static/video/two.mp4")}mutedonEnded={() => setVidIndex((idx) => idx - 1)}ref={twoRef}/></div>}{menuClickIndex == 0 &&<div className="videoShowDiv"><videostyle={showVideoClass}src={require("../../../static/video/one.mp4")}mutedloopautoPlayref={oneRef}/></div>}{menuClickIndex == 1 &&<div className="videoShowDiv"><videostyle={showVideoClass}src={require("../../../static/video/two.mp4")}mutedloopautoPlayref={oneRef}/></div>}</div>);
}export default VideoPlay

 


overAndOutMenu = (index) => {this.setState({menuClickIndex: index})
}//在布局中 
<divonMouseOver={() => {this.overAndOutMenu(0)}}>菜单0
</div><divonMouseOver={() => {this.overAndOutMenu(1)}}>菜单1
</div>

二、预览拖动 

transform的使用 :CSS 变形(CSS3) transform_css transform_快乐de馒头的博客-CSDN博客

##1、缩放 

给图片 加  transform: scale(1.4)

onWheel={(e) => { if (isModelPreview) { handleWheelImage(e) } }} const handleWheelImage = (event) => {const bigger = event.deltaY > 0 ? -1 : 1;//向上为负,向下为正if (bigger > 0 && rate < 5) {const enlargeRate = rate + SCALE;setImgStyle({...imgStyle,'transformOrigin': 'center','transform': `scale( ${enlargeRate}) rotate(${oldRotate}deg)`});setRate(enlargeRate);} else if (bigger < 0 && rate > 1) {const shrinkRate = rate - SCALE;setImgStyle({...imgStyle,'transformOrigin': 'center','transform': `scale( ${shrinkRate}) rotate(${oldRotate}deg)`});setRate(shrinkRate);}}

加动画:

 

 ##2、旋转

给图片 加  transform: rotate(90deg)  90,180,270,360 为选择方向

const childHandleClickRotate = () => {if (rotate == 90) {setOldRotate(90)setRotate(90 * 2)}if (rotate == 180) {setOldRotate(180)setRotate(90 * 3)}if (rotate == 270) {setOldRotate(270)setRotate(90 * 4)}if (rotate == 360) {setOldRotate(360)setRotate(90)}setImgStyle({...imgStyle,'transformOrigin': 'center','transform': `scale(${rate}) rotate(${rotate}deg)`});}

 ##3、matrix 平移

给图片加 

transform: matrix(2.2, 0, 0, 2.2, -89, 186)

matrix(2.2, 0, 0, 2.2, -89, 186)的值 ,2,2为 缩放, -89 和 186 为移动的偏移位置

 拖动时,结合鼠标事件,结合matrix 做图片平移

onMouseDown={(e) => { if (isModelPreview) { handleMouseDown(e) } }}
onMouseMove={(e) => { if (isModelPreview) { handleMouseMove(e) } }}
onMouseUp={(e) => { if (isModelPreview) { handleMouseUp(e) } }}const handleMouseDown = (event) => {event.preventDefault();event.stopPropagation();const { pageX, pageY } = event;setMouseDowmFlag(true); // 控制只有在鼠标按下后才会执行mousemovesetMouseDowmPos({x: pageX,y: pageY,});}const handleMouseMove = (event) => {// console.log("获取的move对象:",event)event.preventDefault()event.stopPropagation()const { pageX, pageY } = event;const diffX = pageX - mouseDowmPos.x;const diffY = pageY - mouseDowmPos.y;if (!mouseDowmFlag || (diffX === 0 && diffY === 0)) return;const offsetX = parseInt(`${diffX}`);const offsetY = parseInt(`${diffY}`);//具体移动 偏移 x和y 可自行计算setImgStyle({...imgStyle,'cursor': 'move','transformOrigin': 'center','transform': `matrix(${rate}, 0, 0, ${rate}, ${offsetX + matrixPos.x}, ${offsetY + matrixPos.y})  rotate(${oldRotate}deg)`});setMatrixPos({x: offsetX,y: offsetY,});}const handleMouseUp = (event) => {event.preventDefault();event.stopPropagation();setMouseDowmFlag(false);}


学习文章:react移动端svg等图片拖拽缩放 - 灰信网(软件开发博客聚合) (freesion.com)

 学习文章:https://www.saoniuhuo.com/question/detail-2609566.htmlreact图片缩放、平移(position、transform实现) - React那点事儿 - SegmentFault 思否


http://www.ppmy.cn/news/629684.html

相关文章

Linux系统:进程控制

文章目录 1 创建进程2 进程终止2.1 进程退出情况2.2 进程终止的常见方式2.2.1 return语句2.2.2 exit()函数2.2.3 _exit()函数 3进程等待3.1 进程等待的重要性3.2 进程等待的方法3.2.1 wait()方法3.2.2 waitpid()方法 4 进程替换4.1 替换原理4.2 替换函数 1 创建进程 fork()函数…

kafka线上环境搭建对软硬件要求

1. 操作系统 kafka推荐使用linux操作系统&#xff0c;原因&#xff1a;kafka的client端的网络模型采用的是java的selector方式&#xff0c;而java的selector在linux系统上使用的是linux的epoll模型&#xff08;回调机制&#xff09;&#xff0c;在windows上采用的是select方式&…

python项目运行的软硬件环境_开发时的软硬件环境和运行时的软硬件环境分别是什么...

展开全部 开发时的软硬件环境是产品开发时所涉及到的编程语言e69da5e887aa62616964757a686964616f31333431373235以及用到的硬件设备。 运行过程中的软硬件环境指涉及到的编程语言支持的编译环境及软件运行时所应该具有的设备和相关硬件设施。 对于开发来说&#xff0c;所有电脑…

辨析:开发环境、测试环境、准生产环境、生产环境

对于刚接触程序猿这个行业的小猿们&#xff0c;刚听到这些名词是不是感觉有点高大上&#xff0c;但是很懵逼啊&#xff0c;完全分不清楚&#xff0c;今天就以我个人的理解来给大家辨析一下这几个名词的区别。 开发环境&#xff1a;开发环境顾名思义就是我们程序猿自己把项目放…

测试环境的搭建和维护_软硬件测试环境

测试环境的搭建和维护 只要做过一次测试&#xff0c;就一定知道测试环境&#xff0c;但测试环境是如何搭建和维护的呢&#xff1f;就不一定所有人都明白了。 测试环境的搭建&#xff0c;每个公司都有不一样的流程和方法。一种是运维或者开发负责搭建和维护&#xff0c;另一种…

PUPANVR-软硬件板端开发环境配置(4)

直接在硬件上跑编译程序时报C,C库的版本不对的警告信息&#xff0c;看来是编译器的版本和当前板端系统上的不一致导致的&#xff0c;需要更新一下rootfs,使用编译器对应的BSP包&#xff1a;Hi3536C V100R001C02SPC040&#xff0c;编译一下rootfs&#xff0c;为了方便开发&#…

软硬件协同设计的系统级开发环境~BPS软件介绍

软硬件协同设计的系统级开发环境~BPS软件介绍 0 赞 发表于 2010/7/19 14:38:11 阅读&#xff08;32881&#xff09; 评论&#xff08;0&#xff09; 1&#xff09;BPS简介&#xff1a; BEEcube Platform Studio&#xff08;BPS&#xff09;工具是一个软硬件协同设计的系统级开…

阐述软硬件结合

硬件和软件的融合已经到了一个转折点——两者不再相互独立,而是越来越多地呈现出一种镜像依赖关系。硬件和软件系统的合作比以往任何时候都要更加密切,它们之间的界限也日趋模糊,两者的功能彼此关联。作为开发人员,重要的是要引领趋势,确保我们掌握的技能不落后于最新技术…