/*
@name: FactoryMap
@author: diaojw
@time: 2022-10-17 08:41:24
@description:
*/
import React, {useEffect, useRef, useState} from 'react';
import * as zrender from 'zrender'
import CanvasPainter from "zrender/lib/canvas/Painter";
import {message} from "antd";
import {Props, StoreEnum} from "@pages/deviceBindWeb/typing";
import {reqGetCameraLocationInfo} from "@pages/deviceBindWeb/api2";
import {reqCameamInfo} from "@components/treeData/api";
import {treeNodeCameraInfo} from "@/utils/commonTools";
interface Interface {
paddingLeft:any;
bindSuccessStatus:any
}
let zr:any= {}
// zrender group实例
let group:any = null
let cameraPoint:any= null
let myImgInitAarr:any= []
let scale= 1
const FactoryMap: React.FC<Interface & Props> = (props) => {
const { paddingLeft,state, dispatch, bindSuccessStatus } = props
// init, Group, Image, Circle
const imageInit = useRef<any>(null)
let cameraImg = 'http://10.88.1.103/audio/img/摄像机.png'
let imgWidth= 0 // 大图宽度
let imgHeight= 0 // 大图高度
let whRatio= 0 // 大图比例
let frameScale= 0 // 缩放比
let frameScaleW= 0 // 缩放比
let position:any= [0,0] // 拖动位置
let scaleMax= 25 // 最大比例
let scaleMin= 1 // 最小比例
const [currentMapUrl, setCurrentMapUrl] = useState('')
const [allCameraForMap, setAllCameraForMap] = useState([])
const [allCameraUpdateForMap, setAllCameraUpdateForMap] = useState([])
const [clickCameraInfo, setClickCameraInfo] = useState<any>({})// 点击当前摄像头信息
const [isAddDrawCamera, setIsAddDrawCamera] = useState(false)
// 地图切换时,绑定或解绑时
useEffect(()=>{
let data = state[StoreEnum.currentMapUrlObj]
if(data ){
console.log('当前地图地址', data)
setCurrentMapUrl(data?.mapUrl)
setAllCameraForMap([])
// 查询所有摄像头位置
getGetCameraLocationInfo(data?.mapNo)
group && group.removeAll()
}
return ()=>{
sessionStorage.removeItem('posArrStr')
// dispatch({
// type: StoreEnum.currentMapUrlObj,
// value:undefined
// })
}
},[state[StoreEnum.currentMapUrlObj], bindSuccessStatus])
// 地图切换时,重新绘制地图,初始化绘制,组织树显示隐藏时
useEffect(()=>{
if(currentMapUrl && allCameraForMap.length>0){
initCanvas()
} else if(currentMapUrl && allCameraForMap.length ===0){
initCanvas()
}
return ()=>{
}
},[currentMapUrl,allCameraForMap, paddingLeft])
// 查询摄像头位置信息借口
const getGetCameraLocationInfo = async (mapNo:string)=>{
const res:any = await reqGetCameraLocationInfo({mapNo, cameraIp: ''}) // currentInfoObj?.cameraIp
setAllCameraForMap(res.data)
}
// 初始化时,必须注册zrender,否则部署线上报错
zrender.registerPainter("canvas", CanvasPainter);
// 初始化绘制,组织树显示隐藏时,获取已绑定摄像头位置时,重新绘制canvas
useEffect(()=>{
let dataPosition = state[StoreEnum.currentCameraBindPostion]
let dataDirection = state[StoreEnum.cameraDirection]
console.log('state[StoreEnum.cameraDirection]', state[StoreEnum.cameraDirection], dataPosition, clickCameraInfo)
// 如果操作是就点击时不绘制图标
if(dataPosition && dataPosition?.isRender && isAddDrawCamera){
group.remove(cameraPoint)
newDrawMark({})
} else if((dataPosition && dataPosition?.isRender || dataDirection && dataDirection?.isRender) && !isAddDrawCamera) {
let newArrClone = JSON.parse(JSON.stringify(allCameraForMap))
newArrClone.map((item:any)=>{
if(item.cameraIp == clickCameraInfo.cameraIp){
let extraInfoObj = {coordX: dataPosition.coordX, coordY: dataPosition.coordY, direction: dataDirection.direction}
item.extraInfo = JSON.stringify(extraInfoObj)
}
})
console.log('dddddddddddddd', myImgInitAarr)
myImgInitAarr.map((item:any)=>{
group.remove(item)
})
drawInitMark(newArrClone)
}
return ()=>{
console.log('destory----------------------')
// dispatch({
// type: StoreEnum.currentCameraBindPostion,
// value: undefined
// })
}
},[state[StoreEnum.currentCameraBindPostion], state[StoreEnum.cameraDirection], clickCameraInfo])
// 初始化函数
const initCanvas = () =>{
// 获取放置画布的元素
const container:any = document.getElementById('canvas')
// 初始化zr实例 zrender容器
zr = zrender.init(container)
// @ts-ignore
group = new zrender.Group({ slient: true }) // 组内子孙元素是否响应鼠标事件
// 创建图片对象
const imgs:any = imageInit.current
// 图片的src等于大图的地址
imgs.crossOrigin = 'Anonymous'
imgs.src = currentMapUrl+`?timestamp=${Date.now()}`
imgs.onerror = (_:any) => {
message.error('图片加载失败!')
return false
}
// 图片加载成功后
imgs.onload = () => {
const canvasWidth = container.clientWidth
const canvasHeight = container.clientHeight
// 画布宽高比
const canvasRatio = canvasWidth / canvasHeight
// 图片的宽高比
whRatio = imgs.width / imgs.height
const originalHeight = imgs.height
const originalWidth = imgs.width
// 图片相对于画布的尺寸进行调整
// 如果图片的宽高比大于画布的宽高比
if (whRatio > canvasRatio) {
imgs.width = canvasWidth
imgs.height = imgs.width / whRatio
} else {
imgs.height = canvasHeight
imgs.width = imgs.height * whRatio
}
// 对图片的宽高进行调整
imgWidth = imgs.width
imgHeight = imgs.height
frameScaleW = originalWidth / imgWidth
frameScale = originalHeight / imgHeight
// 绘制图片
const myImg = new zrender.Image({
style: {
image: currentMapUrl+`?timestamp=${Date.now()}`,
x: 0,
y: 0,
width: imgWidth,
height: imgHeight,
},
z: 1,
onmouseover: (_:any) => {
document.body.style.overflowY = 'hidden'
},
onmousewheel: (_:any) => {
document.body.style.overflowY = 'hidden'
},
onmouseout: (item:any) => {
document.body.style.overflowY = ''
}
})
// 将图片绘制至子节点中
group.add(myImg)
// 调整图片在canvas中的位置
if (zr.getWidth() > imgWidth) {
group.position[0] = (zr.getWidth() - imgWidth) / 2
}
if (zr.getHeight() > imgHeight) {
group.position[1] = (zr.getHeight() - imgHeight) / 2
}
// 将子节点添加至画布中
zr.add(group)
// 绘制mark
drawInitMark(allCameraForMap)
// 调用鼠标的滚动事件
handleZoom()
// 调用鼠标的拖拽事件
handleDrop()
// 绘制摄像头位置
// let dataPosition = state[StoreEnum.currentCameraBindPostion]
// dataPosition?.isRender && newDrawMark(dataPosition)
}
}
// 坐标框的转换
const AssemblyData = (newArr:any) => {
newArr[0] = Math.round(newArr[0] /= frameScaleW)
newArr[1] = Math.round(newArr[1] /= frameScale)
return JSON.parse(JSON.stringify(newArr))
const arr = newArr.map((item:any) => {
return Math.round(item /= frameScale)
})
return arr
}
// 初始化地图相机标记
const drawInitMark = (dataParams:any) =>{
let arr:any = []
dataParams.length>0 && dataParams.forEach((item:any, index:any) => {
let extraInfo = JSON.parse(item.extraInfo)
let myImgInit = new zrender.Image({
style: {
image: cameraImg,
x: extraInfo?.coordX ,
y: extraInfo?.coordY ,
width: 25,
height: 18,
},
z: 10,
draggable: false,
rotation: extraInfo?.direction * Math.PI/180, // 旋转角度
// originX: extraInfo?.coordX - 10 , // 设置中心点
// originY: extraInfo?.coordY - 25 // 设置中心点
originX: extraInfo?.coordX + 12.5/scale , // 设置中心点
originY: extraInfo?.coordY + 9/scale // 设置中心点
}).on('click',(e:any)=>{
let clickXY = {x:e.target.style.x, y: e.target.style.y}
if(dataParams.length>0){
dispatch({
type:StoreEnum.currentCameraBindPostion,
value: undefined
})
dispatch({
type:StoreEnum.cameraDirection,
value: undefined
})
let currentIpObj:any = dataParams.find((item:any)=> {
let coordXYObj = JSON.parse(item.extraInfo)
return (clickXY.x == coordXYObj.coordX) && (clickXY.y == coordXYObj.coordY)
})
console.log('绘制图片摄像头--点击图片666---', e, dataParams, currentIpObj)
group.remove(cameraPoint)
getCameraInfo(currentIpObj?.cameraIp)
}
},{test:'test'})
group.add(myImgInit)
arr.push(myImgInit)
})
myImgInitAarr = arr
}
// 点击地图上添加相机标记
const newDrawMark = (opt:any) =>{
let posXY = sessionStorage.posArrStr ? JSON.parse(sessionStorage.posArrStr) : null
let updateDirection = sessionStorage.updateDirection ? JSON.parse(sessionStorage.updateDirection) : 0
console.log('ssssssssssssssssssssss', posXY, opt?.coordX)
if(!posXY && !opt?.coordX){
return
}
cameraPoint = new zrender.Image({
style: {
image: cameraImg,
// x: posXY ? posXY[0] - 10: opt?.coordX - 10,
// y: posXY ? posXY[1] - 25: opt?.coordY - 25,
x: posXY ? posXY[0] : opt?.coordX ,
y: posXY ? posXY[1] : opt?.coordY ,
width: 25/scale,
height: 18/scale,
},
z: 10,
draggable: false,
rotation: updateDirection * Math.PI/180, // 旋转角度
originX: posXY ? posXY[0] + 12.5/scale : opt?.coordX , // 设置中心点
originY: posXY ? posXY[1] + 9/scale : opt?.coordY // 设置中心点
}).on('mouseup',(e:any)=>{
// 新建相机不能点击
// let posArr = [e.event.zrX - position[0] , e.event.zrY - position[1]]
// let newPosArr = AssemblyData(posArr)
// sessionStorage.posArrStr = JSON.stringify(newPosArr)
// dispatch({
// type: StoreEnum.cameraPos,
// value: posArr
// })
}).on('click',(e:any)=>{
// 新建相机不能点击
// let clickXY = {x:e.target.originX, y: e.target.originY}
// if(allCameraForMap.length>0){
// let currentIpObj:any = allCameraForMap.find((item:any)=> {
// let coordXYObj = JSON.parse(item.extraInfo)
// return (clickXY.x == coordXYObj.coordX) && (clickXY.y == coordXYObj.coordY)
// })
// console.log('绘制图片摄像头--点击图片666---', e, allCameraForMap, currentIpObj)
// getCameraInfo(currentIpObj?.cameraIp)
//
// }
})
// sessionStorage.setItem('waitRmoveElement',JSON.stringify(cameraPoint))
group.add(cameraPoint)
// 画完清空坐标
// sessionStorage.removeItem('posArrStr')
}
// 滚动缩放
const handleZoom = () => {
// 在画布子节点上监听鼠标滚轮事件 事件对象
group.on('mousewheel', (ev:any) => {
// wheeldata 返回值说明:正值向上滚动,负值向下滚动 均为120的倍数 缩小20倍 结果可能是 +e.wheelDelta/20 或者是 -e.wheelData/20
// 缩小20倍数 调整为10
// 监听到鼠标再
const e = ev.wheelDelta / 5
// scale原始的缩放比例是1 每次滚动在这个基础上加上或者减去滚轮缩放后的数据
scale += e
// 调用缩放的函数 将缩放的比例传入
setScale(scale)
group.eachChild(function (groupImg:any) {
console.log('groupImg', groupImg)
// 缩放时摄像头大小不会变
if(groupImg.style.image.includes('摄像机')){
groupImg.attr({
style:{ width: 25/scale, height: 18/scale}
})
}
})
})
}
/** * 缩放 */
const setScale = (sca:any) => {
if (sca > scaleMax) {
sca = scaleMax
} else if (sca < scaleMin) {
sca = scaleMin
}
scale = sca
group.attr({
scale: [scale, scale],
origin: [zr.getWidth() / 2, zr.getHeight() / 2]
})
}
// 拖拽方法
const handleDrop = () => {
zr.dragData = {
drag: false,
pos: [0, 0],
group: null,
target: null
}
// 画 摄像头
zr.on('dblclick', (e:any) => {
let tramsformX = group.transform ? Math.abs(group.transform[4])/scale : 0
let tramsformY = group.transform ? Math.abs(group.transform[5])/scale : 0
// 画布拖拽的起始位置是 事件对象中的画布的坐标位置
let posArr = [(e.event.zrX/scale + tramsformX) , (e.event.zrY/scale + tramsformY) ]
let newPosArr = AssemblyData(posArr)
console.log('zr.dragData.pos', posArr,position, newPosArr)
sessionStorage.posArrStr = JSON.stringify(newPosArr)
dispatch({
type: StoreEnum.cameraPos,
value: posArr
})
if(sessionStorage.waitRmoveElement){
console.log('removeEle',sessionStorage.waitRmoveElement)
let removeEle = JSON.parse(sessionStorage.waitRmoveElement)
group.remove(removeEle)
}
group.remove(cameraPoint)
// initCanvas()
// drawMark({})
// 重置空值,避免添加相机后在修改相机,XY数据污染
setClickCameraInfo({})
setIsAddDrawCamera(true)
dispatch({
type: StoreEnum.currentCameraBindPostion,
value: {coordX:newPosArr[0],coordY: newPosArr[1], isRender:true}
})
})
zr.on('mousedown', (e:any) => {
// 画布拖拽的起始位置是 事件对象中的画布的坐标位置
let posArr = [e.event.zrX, e.event.zrY]
zr.dragData.pos = posArr
// 画布的拖拽目标
zr.dragData.target = e.target
if (e.target === undefined) {
// !!!
zr.dragData.drag = false
} else if (e.target.parent && e.target.parent.type === 'group') {
zr.dragData.drag = true
zr.dragData.group = e.target.parent
}
})
// 鼠标抬起事件 关闭拖拽 将拖拽的目标元素设置为空
zr.on('mouseup', (e:any) => {
zr.dragData.drag = false
zr.dragData.group = null
})
// 鼠标移出事件 关闭拖拽
zr.on('mouseout', (e:any) => {
zr.dragData.drag = false
zr.dragData.group = null
})
// 鼠标移动事件
zr.on('mousemove', (e:any) => {
console.log('zr.getWidth()', zr.getWidth(),scale,)
if (zr.dragData.drag !== true) return
const new_pos = [e.event.zrX, e.event.zrY]
if (zr.dragData.group != null) {
var pos = [
new_pos[0] - zr.dragData.pos[0],
new_pos[1] - zr.dragData.pos[1]
]
zr.dragData.group.children().forEach((x:any) => {
x.position = [0, 0]
})
zr.dragData.group.position[0] += pos[0]
zr.dragData.group.position[1] += pos[1]
zr.dragData.group.dirty()
position[0] = zr.dragData.group.position[0]
position[1] = zr.dragData.group.position[1]
} else {
// eslint-disable-next-line no-redeclare
var pos = [
new_pos[0] - zr.dragData.pos[0],
new_pos[1] - zr.dragData.pos[1]
]
zr.storage.getDisplayList(true, true).forEach((x:any) => {
x.position[0] += pos[0]
x.position[1] += pos[1]
x.dirty()
})
}
zr.dragData.pos = [e.event.zrX, e.event.zrY]
})
}
// 获取摄像头信息
const getCameraInfo = async (cameraIp:any) =>{
let obj = {curPage: 1,pageSize: 1500,type: "GROUP",cameraIp, listUUID:1}
const res = await reqCameamInfo(obj)
const resdata = res.data
if(resdata.code === 200){
console.log('resdata.data',resdata.data.basicInfoList)
let node = resdata.data.basicInfoList
setIsAddDrawCamera(false)
dispatch({
type: StoreEnum.currentCameraInfo,
value: { ...node[0], isClick: true }
})
setClickCameraInfo({ ...node[0], isClick: true })
}
}
return (
<div className={`factory-map-wrap`}>
<img src="" alt="initImage" ref={imageInit} style={{position:'absolute',top:'-10000000000px',width:'100%',height:'100%'}}/>
<div id="canvas" style={{width:'100%', height:'100%', backgroundColor:'#F5F5F5'}}></div>
</div>
)
}
export default FactoryMap;/*
@name: FactoryMap
@author: diaojw
@time: 2022-10-17 08:41:24
@description:
*/
import React, {useEffect, useRef, useState} from 'react';
import * as zrender from 'zrender'
import CanvasPainter from "zrender/lib/canvas/Painter";
import {message} from "antd";
import {Props, StoreEnum} from "@pages/deviceBindWeb/typing";
import {reqGetCameraLocationInfo} from "@pages/deviceBindWeb/api2";
import {reqCameamInfo} from "@components/treeData/api";
import {treeNodeCameraInfo} from "@/utils/commonTools";
interface Interface {
paddingLeft:any;
bindSuccessStatus:any
}
let zr:any= {}
// zrender group实例
let group:any = null
let cameraPoint:any= null
let myImgInitAarr:any= []
let scale= 1
const FactoryMap: React.FC<Interface & Props> = (props) => {
const { paddingLeft,state, dispatch, bindSuccessStatus } = props
// init, Group, Image, Circle
const imageInit = useRef<any>(null)
let cameraImg = 'http://10.88.1.103/audio/img/摄像机.png'
let imgWidth= 0 // 大图宽度
let imgHeight= 0 // 大图高度
let whRatio= 0 // 大图比例
let frameScale= 0 // 缩放比
let frameScaleW= 0 // 缩放比
let position:any= [0,0] // 拖动位置
let scaleMax= 25 // 最大比例
let scaleMin= 1 // 最小比例
const [currentMapUrl, setCurrentMapUrl] = useState('')
const [allCameraForMap, setAllCameraForMap] = useState([])
const [allCameraUpdateForMap, setAllCameraUpdateForMap] = useState([])
const [clickCameraInfo, setClickCameraInfo] = useState<any>({})// 点击当前摄像头信息
const [isAddDrawCamera, setIsAddDrawCamera] = useState(false)
// 地图切换时,绑定或解绑时
useEffect(()=>{
let data = state[StoreEnum.currentMapUrlObj]
if(data ){
console.log('当前地图地址', data)
setCurrentMapUrl(data?.mapUrl)
setAllCameraForMap([])
// 查询所有摄像头位置
getGetCameraLocationInfo(data?.mapNo)
group && group.removeAll()
}
return ()=>{
sessionStorage.removeItem('posArrStr')
// dispatch({
// type: StoreEnum.currentMapUrlObj,
// value:undefined
// })
}
},[state[StoreEnum.currentMapUrlObj], bindSuccessStatus])
// 地图切换时,重新绘制地图,初始化绘制,组织树显示隐藏时
useEffect(()=>{
if(currentMapUrl && allCameraForMap.length>0){
initCanvas()
} else if(currentMapUrl && allCameraForMap.length ===0){
initCanvas()
}
return ()=>{
}
},[currentMapUrl,allCameraForMap, paddingLeft])
// 查询摄像头位置信息借口
const getGetCameraLocationInfo = async (mapNo:string)=>{
const res:any = await reqGetCameraLocationInfo({mapNo, cameraIp: ''}) // currentInfoObj?.cameraIp
setAllCameraForMap(res.data)
}
// 初始化时,必须注册zrender,否则部署线上报错
zrender.registerPainter("canvas", CanvasPainter);
// 初始化绘制,组织树显示隐藏时,获取已绑定摄像头位置时,重新绘制canvas
useEffect(()=>{
let dataPosition = state[StoreEnum.currentCameraBindPostion]
let dataDirection = state[StoreEnum.cameraDirection]
console.log('state[StoreEnum.cameraDirection]', state[StoreEnum.cameraDirection], dataPosition, clickCameraInfo)
// 如果操作是就点击时不绘制图标
if(dataPosition && dataPosition?.isRender && isAddDrawCamera){
group.remove(cameraPoint)
newDrawMark({})
} else if((dataPosition && dataPosition?.isRender || dataDirection && dataDirection?.isRender) && !isAddDrawCamera) {
let newArrClone = JSON.parse(JSON.stringify(allCameraForMap))
newArrClone.map((item:any)=>{
if(item.cameraIp == clickCameraInfo.cameraIp){
let extraInfoObj = {coordX: dataPosition.coordX, coordY: dataPosition.coordY, direction: dataDirection.direction}
item.extraInfo = JSON.stringify(extraInfoObj)
}
})
console.log('dddddddddddddd', myImgInitAarr)
myImgInitAarr.map((item:any)=>{
group.remove(item)
})
drawInitMark(newArrClone)
}
return ()=>{
console.log('destory----------------------')
// dispatch({
// type: StoreEnum.currentCameraBindPostion,
// value: undefined
// })
}
},[state[StoreEnum.currentCameraBindPostion], state[StoreEnum.cameraDirection], clickCameraInfo])
// 初始化函数
const initCanvas = () =>{
// 获取放置画布的元素
const container:any = document.getElementById('canvas')
// 初始化zr实例 zrender容器
zr = zrender.init(container)
// @ts-ignore
group = new zrender.Group({ slient: true }) // 组内子孙元素是否响应鼠标事件
// 创建图片对象
const imgs:any = imageInit.current
// 图片的src等于大图的地址
imgs.crossOrigin = 'Anonymous'
imgs.src = currentMapUrl+`?timestamp=${Date.now()}`
imgs.onerror = (_:any) => {
message.error('图片加载失败!')
return false
}
// 图片加载成功后
imgs.onload = () => {
const canvasWidth = container.clientWidth
const canvasHeight = container.clientHeight
// 画布宽高比
const canvasRatio = canvasWidth / canvasHeight
// 图片的宽高比
whRatio = imgs.width / imgs.height
const originalHeight = imgs.height
const originalWidth = imgs.width
// 图片相对于画布的尺寸进行调整
// 如果图片的宽高比大于画布的宽高比
if (whRatio > canvasRatio) {
imgs.width = canvasWidth
imgs.height = imgs.width / whRatio
} else {
imgs.height = canvasHeight
imgs.width = imgs.height * whRatio
}
// 对图片的宽高进行调整
imgWidth = imgs.width
imgHeight = imgs.height
frameScaleW = originalWidth / imgWidth
frameScale = originalHeight / imgHeight
// 绘制图片
const myImg = new zrender.Image({
style: {
image: currentMapUrl+`?timestamp=${Date.now()}`,
x: 0,
y: 0,
width: imgWidth,
height: imgHeight,
},
z: 1,
onmouseover: (_:any) => {
document.body.style.overflowY = 'hidden'
},
onmousewheel: (_:any) => {
document.body.style.overflowY = 'hidden'
},
onmouseout: (item:any) => {
document.body.style.overflowY = ''
}
})
// 将图片绘制至子节点中
group.add(myImg)
// 调整图片在canvas中的位置
if (zr.getWidth() > imgWidth) {
group.position[0] = (zr.getWidth() - imgWidth) / 2
}
if (zr.getHeight() > imgHeight) {
group.position[1] = (zr.getHeight() - imgHeight) / 2
}
// 将子节点添加至画布中
zr.add(group)
// 绘制mark
drawInitMark(allCameraForMap)
// 调用鼠标的滚动事件
handleZoom()
// 调用鼠标的拖拽事件
handleDrop()
// 绘制摄像头位置
// let dataPosition = state[StoreEnum.currentCameraBindPostion]
// dataPosition?.isRender && newDrawMark(dataPosition)
}
}
// 坐标框的转换
const AssemblyData = (newArr:any) => {
newArr[0] = Math.round(newArr[0] /= frameScaleW)
newArr[1] = Math.round(newArr[1] /= frameScale)
return JSON.parse(JSON.stringify(newArr))
const arr = newArr.map((item:any) => {
return Math.round(item /= frameScale)
})
return arr
}
// 初始化地图相机标记
const drawInitMark = (dataParams:any) =>{
let arr:any = []
dataParams.length>0 && dataParams.forEach((item:any, index:any) => {
let extraInfo = JSON.parse(item.extraInfo)
let myImgInit = new zrender.Image({
style: {
image: cameraImg,
x: extraInfo?.coordX ,
y: extraInfo?.coordY ,
width: 25,
height: 18,
},
z: 10,
draggable: false,
rotation: extraInfo?.direction * Math.PI/180, // 旋转角度
// originX: extraInfo?.coordX - 10 , // 设置中心点
// originY: extraInfo?.coordY - 25 // 设置中心点
originX: extraInfo?.coordX + 12.5/scale , // 设置中心点
originY: extraInfo?.coordY + 9/scale // 设置中心点
}).on('click',(e:any)=>{
let clickXY = {x:e.target.style.x, y: e.target.style.y}
if(dataParams.length>0){
dispatch({
type:StoreEnum.currentCameraBindPostion,
value: undefined
})
dispatch({
type:StoreEnum.cameraDirection,
value: undefined
})
let currentIpObj:any = dataParams.find((item:any)=> {
let coordXYObj = JSON.parse(item.extraInfo)
return (clickXY.x == coordXYObj.coordX) && (clickXY.y == coordXYObj.coordY)
})
console.log('绘制图片摄像头--点击图片666---', e, dataParams, currentIpObj)
group.remove(cameraPoint)
getCameraInfo(currentIpObj?.cameraIp)
}
},{test:'test'})
group.add(myImgInit)
arr.push(myImgInit)
})
myImgInitAarr = arr
}
// 点击地图上添加相机标记
const newDrawMark = (opt:any) =>{
let posXY = sessionStorage.posArrStr ? JSON.parse(sessionStorage.posArrStr) : null
let updateDirection = sessionStorage.updateDirection ? JSON.parse(sessionStorage.updateDirection) : 0
console.log('ssssssssssssssssssssss', posXY, opt?.coordX)
if(!posXY && !opt?.coordX){
return
}
cameraPoint = new zrender.Image({
style: {
image: cameraImg,
// x: posXY ? posXY[0] - 10: opt?.coordX - 10,
// y: posXY ? posXY[1] - 25: opt?.coordY - 25,
x: posXY ? posXY[0] : opt?.coordX ,
y: posXY ? posXY[1] : opt?.coordY ,
width: 25/scale,
height: 18/scale,
},
z: 10,
draggable: false,
rotation: updateDirection * Math.PI/180, // 旋转角度
originX: posXY ? posXY[0] + 12.5/scale : opt?.coordX , // 设置中心点
originY: posXY ? posXY[1] + 9/scale : opt?.coordY // 设置中心点
}).on('mouseup',(e:any)=>{
// 新建相机不能点击
// let posArr = [e.event.zrX - position[0] , e.event.zrY - position[1]]
// let newPosArr = AssemblyData(posArr)
// sessionStorage.posArrStr = JSON.stringify(newPosArr)
// dispatch({
// type: StoreEnum.cameraPos,
// value: posArr
// })
}).on('click',(e:any)=>{
// 新建相机不能点击
// let clickXY = {x:e.target.originX, y: e.target.originY}
// if(allCameraForMap.length>0){
// let currentIpObj:any = allCameraForMap.find((item:any)=> {
// let coordXYObj = JSON.parse(item.extraInfo)
// return (clickXY.x == coordXYObj.coordX) && (clickXY.y == coordXYObj.coordY)
// })
// console.log('绘制图片摄像头--点击图片666---', e, allCameraForMap, currentIpObj)
// getCameraInfo(currentIpObj?.cameraIp)
//
// }
})
// sessionStorage.setItem('waitRmoveElement',JSON.stringify(cameraPoint))
group.add(cameraPoint)
// 画完清空坐标
// sessionStorage.removeItem('posArrStr')
}
// 滚动缩放
const handleZoom = () => {
// 在画布子节点上监听鼠标滚轮事件 事件对象
group.on('mousewheel', (ev:any) => {
// wheeldata 返回值说明:正值向上滚动,负值向下滚动 均为120的倍数 缩小20倍 结果可能是 +e.wheelDelta/20 或者是 -e.wheelData/20
// 缩小20倍数 调整为10
// 监听到鼠标再
const e = ev.wheelDelta / 5
// scale原始的缩放比例是1 每次滚动在这个基础上加上或者减去滚轮缩放后的数据
scale += e
// 调用缩放的函数 将缩放的比例传入
setScale(scale)
group.eachChild(function (groupImg:any) {
console.log('groupImg', groupImg)
// 缩放时摄像头大小不会变
if(groupImg.style.image.includes('摄像机')){
groupImg.attr({
style:{ width: 25/scale, height: 18/scale}
})
}
})
})
}
/** * 缩放 */
const setScale = (sca:any) => {
if (sca > scaleMax) {
sca = scaleMax
} else if (sca < scaleMin) {
sca = scaleMin
}
scale = sca
group.attr({
scale: [scale, scale],
origin: [zr.getWidth() / 2, zr.getHeight() / 2]
})
}
// 拖拽方法
const handleDrop = () => {
zr.dragData = {
drag: false,
pos: [0, 0],
group: null,
target: null
}
// 画 摄像头
zr.on('dblclick', (e:any) => {
let tramsformX = group.transform ? Math.abs(group.transform[4])/scale : 0
let tramsformY = group.transform ? Math.abs(group.transform[5])/scale : 0
// 画布拖拽的起始位置是 事件对象中的画布的坐标位置
let posArr = [(e.event.zrX/scale + tramsformX) , (e.event.zrY/scale + tramsformY) ]
let newPosArr = AssemblyData(posArr)
console.log('zr.dragData.pos', posArr,position, newPosArr)
sessionStorage.posArrStr = JSON.stringify(newPosArr)
dispatch({
type: StoreEnum.cameraPos,
value: posArr
})
if(sessionStorage.waitRmoveElement){
console.log('removeEle',sessionStorage.waitRmoveElement)
let removeEle = JSON.parse(sessionStorage.waitRmoveElement)
group.remove(removeEle)
}
group.remove(cameraPoint)
// initCanvas()
// drawMark({})
// 重置空值,避免添加相机后在修改相机,XY数据污染
setClickCameraInfo({})
setIsAddDrawCamera(true)
dispatch({
type: StoreEnum.currentCameraBindPostion,
value: {coordX:newPosArr[0],coordY: newPosArr[1], isRender:true}
})
})
zr.on('mousedown', (e:any) => {
// 画布拖拽的起始位置是 事件对象中的画布的坐标位置
let posArr = [e.event.zrX, e.event.zrY]
zr.dragData.pos = posArr
// 画布的拖拽目标
zr.dragData.target = e.target
if (e.target === undefined) {
// !!!
zr.dragData.drag = false
} else if (e.target.parent && e.target.parent.type === 'group') {
zr.dragData.drag = true
zr.dragData.group = e.target.parent
}
})
// 鼠标抬起事件 关闭拖拽 将拖拽的目标元素设置为空
zr.on('mouseup', (e:any) => {
zr.dragData.drag = false
zr.dragData.group = null
})
// 鼠标移出事件 关闭拖拽
zr.on('mouseout', (e:any) => {
zr.dragData.drag = false
zr.dragData.group = null
})
// 鼠标移动事件
zr.on('mousemove', (e:any) => {
console.log('zr.getWidth()', zr.getWidth(),scale,)
if (zr.dragData.drag !== true) return
const new_pos = [e.event.zrX, e.event.zrY]
if (zr.dragData.group != null) {
var pos = [
new_pos[0] - zr.dragData.pos[0],
new_pos[1] - zr.dragData.pos[1]
]
zr.dragData.group.children().forEach((x:any) => {
x.position = [0, 0]
})
zr.dragData.group.position[0] += pos[0]
zr.dragData.group.position[1] += pos[1]
zr.dragData.group.dirty()
position[0] = zr.dragData.group.position[0]
position[1] = zr.dragData.group.position[1]
} else {
// eslint-disable-next-line no-redeclare
var pos = [
new_pos[0] - zr.dragData.pos[0],
new_pos[1] - zr.dragData.pos[1]
]
zr.storage.getDisplayList(true, true).forEach((x:any) => {
x.position[0] += pos[0]
x.position[1] += pos[1]
x.dirty()
})
}
zr.dragData.pos = [e.event.zrX, e.event.zrY]
})
}
// 获取摄像头信息
const getCameraInfo = async (cameraIp:any) =>{
let obj = {curPage: 1,pageSize: 1500,type: "GROUP",cameraIp, listUUID:1}
const res = await reqCameamInfo(obj)
const resdata = res.data
if(resdata.code === 200){
console.log('resdata.data',resdata.data.basicInfoList)
let node = resdata.data.basicInfoList
setIsAddDrawCamera(false)
dispatch({
type: StoreEnum.currentCameraInfo,
value: { ...node[0], isClick: true }
})
setClickCameraInfo({ ...node[0], isClick: true })
}
}
return (
<div className={`factory-map-wrap`}>
<img src="" alt="initImage" ref={imageInit} style={{position:'absolute',top:'-10000000000px',width:'100%',height:'100%'}}/>
<div id="canvas" style={{width:'100%', height:'100%', backgroundColor:'#F5F5F5'}}></div>
</div>
)
}
export default FactoryMap;
zrender应用
最新推荐文章于 2024-08-09 07:03:27 发布