<!-- 工艺图 -->
<div id="svgDiv" style="width: 100%; height: 100%">
<embed
id="svgView"
name="svgView"
src
wmode="transparent"
width="100%"
height="100%"
type="image/svg+xml"
/>
</div>
export const useSvgPanZoom = () => {
let zoomVal = 0
let zoomStepSize = 20
let removeFlag = false
let zoomW = 41
let zoomH = 41
let zoomX = 0
let zoomY = 0
let startX = 0
let startY = 0
let endX = 0
let endY = 0
let moveX = 0
let moveY = 0
let vbCX = 0
let vbCY = 0
let vbCW = 0
let vbCH = 0
let svgdoc = null
let svgDom = null
function initSvg(svg) {
svgdoc = svg
svgDom = svg.getElementsByTagName('svg')[0]
svgDom.addEventListener('mousedown', moveDownMouse, { passive: false })
svgDom.addEventListener('mouseup', moveUpMouse, { passive: false })
svgDom.addEventListener('mousemove', moveMouse, { passive: false })
svgDom.addEventListener('mousewheel', MouseWheel, { passive: false })
getCurrentVB()
}
function initStyle(color) {
let titleNode = svgdoc.querySelectorAll("[id^='Title']")
titleNode.forEach((element) => {
let node = element.firstChild || element
node.style.setProperty('stroke', color, 'important')
node.style.setProperty('fill', color, 'important')
})
let textNode = svgdoc.querySelectorAll("[id^='text']")
textNode.forEach((element) => {
let node = element.firstChild || element
node.style.setProperty('stroke', color, 'important')
node.style.setProperty('fill', color, 'important')
})
let dNode = svgdoc.querySelectorAll("[id^='D']")
dNode.forEach((element) => {
let node = element
node.style.setProperty('cursor', 'pointer', 'important')
})
let scNode = svgdoc.querySelectorAll("[id^='SC']")
scNode.forEach((element) => {
let node = element.firstChild || element
node.style.setProperty('cursor', 'pointer', 'important')
})
let EcNode = svgdoc.querySelectorAll("[id^='E']")
EcNode.forEach((element) => {
let node = element.firstChild || element
node.style.setProperty('cursor', 'pointer', 'important')
})
let VcNode = svgdoc.querySelectorAll("[id^='V']")
VcNode.forEach((element) => {
let node = element.firstChild || element
node.style.setProperty('cursor', 'pointer', 'important')
})
let LNode = svgdoc.querySelectorAll("[id^='L']")
LNode.forEach((element) => {
let node = element.firstChild || element
createItem(node, element.id)
node.style.setProperty('cursor', 'pointer', 'important')
})
}
function createItem(node, id) {
if (id.indexOf('_animateMotionG') > -1) {
return
}
let path
let d = node.getAttribute('d')
if (d) {
path = d
} else if (node.getElementsByTagName('polyline').length) {
node = node.getElementsByTagName('polyline').item(0)
if (node) {
let pointStrs = node.getAttribute('points')
let points = pointStrs.split(',')
path = 'M ' + points[0] + ' ' + points[1]
for (let i = 2; i < points.length; i++) {
if (i % 2 != 0) {
path += ' ' + (points[i] - points[i - 2])
} else {
path += ' l' + (points[i] - points[i - 2])
}
}
}
}
if (path) {
let gEle = svgdoc.getElementById(id + '_animateMotionG')
if (!gEle) {
gEle = svgdoc.createElementNS('http://www.w3.org/2000/svg', 'g')
gEle.setAttribute('id', id + '_animateMotionG')
let maxTime = 6
try {
let length = node.getTotalLength()
if (length) {
maxTime = parseInt(length / 50)
if (maxTime < 2) {
maxTime = 2
} else if (maxTime > 10) {
maxTime = 10
}
}
} catch (e) {
console.log('管线' + id + '获取长度有异常~' + e)
}
for (let index = 0; index < maxTime; index++) {
let animation = svgdoc.createElementNS('http://www.w3.org/2000/svg', 'animateMotion')
animation.setAttribute('begin', index + 's')
animation.setAttribute('dur', maxTime + 's')
animation.setAttribute('rotate', 'auto')
animation.setAttribute('repeatCount', 'indefinite')
animation.setAttribute('path', path)
let polygon = svgdoc.createElementNS('http://www.w3.org/2000/svg', 'polygon')
let lineWidth = '0'
try {
lineWidth = node.getStyle().getPropertyValue('stroke-width')
} catch (e) {
lineWidth = node.style.strokeWidth.replace('px', '')
}
polygon.setAttribute(
'points',
(4.3 * lineWidth) / 10 +
',0 -' +
(4.3 * lineWidth) / 10 +
',-' +
(5 * lineWidth) / 10 +
' -' +
(4.3 * lineWidth) / 10 +
',' +
(5 * lineWidth) / 10
)
let lineColor = 'purple'
if (id && id.indexOf('LineAir') > -1) {
lineColor = '#004501'
} else if (id && id.indexOf('LineWater') > -1) {
lineColor = '#8eabff'
} else if (id && id.indexOf('LineSludge') > -1) {
lineColor = '#f78855'
} else if (id && id.indexOf('LineDrug') > -1) {
lineColor = '#676700'
}
polygon.setAttribute(
'style',
'fill:' + lineColor + ';stroke:' + lineColor + ';stroke-width:0.1'
)
polygon.appendChild(animation)
gEle.appendChild(polygon)
}
node.parentNode.appendChild(gEle)
} else {
}
}
}
function MouseWheel(e) {
let ev = e || window.event
let down = true
down = ev.wheelDelta ? ev.wheelDelta < 0 : ev.detail > 0
if (down) {
zoomOut()
} else {
zoomIn()
}
if (ev.preventDefault) {
ev.preventDefault()
}
return false
}
function zoomIn() {
if (zoomW > zoomStepSize * 2 && zoomH > zoomStepSize * 2) {
zoomVal += zoomStepSize
zoomTo('in')
}
}
function zoomOut() {
zoomVal -= zoomStepSize
if (zoomVal >= -zoomStepSize * 11) {
zoomTo('out')
} else {
zoomVal = -zoomStepSize * 11
}
}
function zoomTo(flag) {
getCurrentVB()
if (flag === 'in') {
zoomX = vbCX + zoomStepSize
zoomY = vbCY + zoomStepSize
zoomW = vbCW - zoomStepSize * 2
zoomH = vbCH - zoomStepSize * 2
} else {
zoomX = vbCX - zoomStepSize
zoomY = vbCY - zoomStepSize
zoomW = vbCW + zoomStepSize * 2
zoomH = vbCH + zoomStepSize * 2
}
svgDom.setAttributeNS(null, 'viewBox', zoomX + ' ' + zoomY + ' ' + zoomW + ' ' + zoomH)
endZoom()
}
function endZoom() {
getCurrentVB()
endX = vbCX
endY = vbCY
}
function moveDownMouse(evt) {
getCurrentVB()
removeFlag = true
startX = parseInt(evt.clientX)
startY = parseInt(evt.clientY)
}
function moveMouse(evt) {
if (removeFlag) {
svgDom.setAttributeNS(null, 'style', 'cursor: move')
moveX = parseInt(evt.clientX) - startX
moveY = parseInt(evt.clientY) - startY
vbCX = endX - moveX
vbCY = endY - moveY
vbCW = parseFloat(svgDom.viewBox.animVal.width)
vbCH = parseFloat(svgDom.viewBox.animVal.height)
svgDom.setAttributeNS(null, 'viewBox', vbCX + ' ' + vbCY + ' ' + vbCW + ' ' + vbCH)
}
}
function moveUpMouse(evt) {
svgDom.setAttributeNS(null, 'style', 'cursor: default')
endX = vbCX
endY = vbCY
removeFlag = false
}
function getCurrentVB() {
vbCX = parseFloat(svgDom.viewBox.animVal.x)
vbCY = parseFloat(svgDom.viewBox.animVal.y)
vbCW = parseFloat(svgDom.viewBox.animVal.width)
vbCH = parseFloat(svgDom.viewBox.animVal.height)
}
function addEvent(obj, xEvent, fn) {
if (obj.attachEvent) {
obj.attachEvent('on' + xEvent, fn)
} else {
obj.addEventListener(xEvent, fn, false)
}
}
return {
addEvent,
getCurrentVB,
moveUpMouse,
moveMouse,
moveDownMouse,
endZoom,
zoomTo,
zoomIn,
MouseWheel,
initSvg,
initStyle,
createItem
}
}
<!-- 工艺图 -->
<div id="svgDiv" style="width: 100%; height: 100%">
<embed
id="svgView"
name="svgView"
src
wmode="transparent"
width="100%"
height="100%"
type="image/svg+xml"
/>
</div>
let svgDom = null
const init = (info) => {
let { proDrawUrl } = info
if (!proDrawUrl) return
nextTick(() => {
let svgView = ''
if (isIE()) {
let svgDiv = document.getElementById('svgDiv')
let _svgView = document.getElementById('svgView')
if (_svgView) {
svgDiv.removeChild(_svgView)
}
svgView = document.createElement('embed')
svgView.setAttribute('id', 'svgView')
svgView.setAttribute('name', 'svgView')
svgView.setAttribute('src', proDrawUrl)
svgView.setAttribute('wmode', 'transparent')
svgView.setAttribute('width', '100%')
svgView.setAttribute('height', document.body.clientHeight - 70 + 'px')
svgView.setAttribute('type', 'image/svg+xml')
svgDiv.appendChild(svgView)
} else {
svgView = document.getElementById('svgView')
svgView.src = proDrawUrl
}
svgView.onload = function () {
svgdoc.value = null
svgdoc.value = svgView.getSVGDocument()
if (!svgdoc.value) {
svgdoc.value = svgView.contentDocument
}
const { initSvg, initStyle } = useSvgPanZoom()
initSvg(svgdoc.value)
initStyle(primaryColor.value)
svgdoc.value.addEventListener('click', (e) => {
if (
e.target.id.indexOf('E') !== -1 ||
e.target.id.indexOf('L') !== -1 ||
e.target.id.indexOf('D') !== -1
) {
showDrawer(e.target.id, id, factoryId)
}
})
}
})
}