This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
nezha-nezha-fronted/nezha-fronted/src/components/common/project/L5/services/canvas.js

574 lines
14 KiB
JavaScript
Raw Normal View History

2021-03-19 18:52:19 +08:00
import { register as registerFlow } from '@topology/flow-diagram'
import { register as registerActivity } from '@topology/activity-diagram'
import { register as registerSequence } from '@topology/sequence-diagram'
import { register as registerChart } from '@topology/chart-diagram'
import { Node, Rect, Point, Direction } from '@topology/core'
// import { register as registerClass } from './class-diagram/index'
2021-03-19 18:52:19 +08:00
export let canvas
2021-03-19 18:52:19 +08:00
export function canvasRegister () {
registerFlow()
registerActivity()
registerSequence()
registerChart()
// registerClass()
}
2021-03-19 18:52:19 +08:00
export const Tools = [
{
2021-03-19 18:52:19 +08:00
group: 'General',
children: [
// {
// name: 'rectangle',
// icon: 'icon-rect',
// data: {
// text: 'rect',
// rect: {
// width: 100,
// height: 100
// },
// paddingLeft: 10,
// paddingRight: 10,
// paddingTop: 10,
// paddingBottom: 10,
// name: 'rectangleImg',
// icon: '\ue680',
// iconFamily: 'nz-icon',
// iconColor: ''
// }
// },
{
name: 'rectangle',
icon: 'icon-cube',
data: {
rect: {
width: 100,
height: 100
},
is3D: true,
z: 20,
zRotate: 15,
name: 'myCube',
iconFamily: 'topology',
iconColor: '#777',
iconSize: 30
}
},
{
2021-03-19 18:52:19 +08:00
name: 'rectangle',
icon: 'icon-rect',
data: {
text: 'rect',
rect: {
width: 100,
height: 100
},
2021-03-19 18:52:19 +08:00
paddingLeft: 10,
paddingRight: 10,
paddingTop: 10,
paddingBottom: 10,
name: 'rectangle'
}
},
{
2021-03-19 18:52:19 +08:00
name: 'rectangle',
icon: 'icon-rectangle',
data: {
text: 'rectangle',
rect: {
width: 200,
height: 50
},
2021-03-19 18:52:19 +08:00
paddingLeft: 10,
paddingRight: 10,
paddingTop: 10,
paddingBottom: 10,
borderRadius: 0.1,
name: 'rectangle'
// icon: '\ue680',
// iconFamily: 'nz-icon',
// iconColor: ''
}
},
{
2021-03-19 18:52:19 +08:00
name: 'circle',
icon: 'icon-circle',
data: {
text: 'circle',
rect: {
width: 100,
height: 100
},
2021-03-19 18:52:19 +08:00
name: 'circle',
textMaxLine: 1
}
},
{
2021-03-19 18:52:19 +08:00
name: 'triangle',
icon: 'icon-triangle',
data: {
text: 'triangle',
rect: {
width: 100,
height: 100
},
2021-03-19 18:52:19 +08:00
name: 'triangle'
}
},
{
2021-03-19 18:52:19 +08:00
name: 'diamond',
icon: 'icon-diamond',
data: {
text: 'diamond',
rect: {
width: 100,
height: 100
},
2021-03-19 18:52:19 +08:00
name: 'diamond'
}
},
{
2021-03-19 18:52:19 +08:00
name: 'pentagon',
icon: 'icon-pentagon',
data: {
text: 'pentagon',
rect: {
width: 100,
height: 100
},
2021-03-19 18:52:19 +08:00
name: 'pentagon'
}
},
{
2021-03-19 18:52:19 +08:00
name: 'hexagon',
icon: 'icon-hexagon',
data: {
text: 'hexagon',
rect: {
width: 100,
height: 100
},
2021-03-19 18:52:19 +08:00
paddingTop: 10,
paddingBottom: 10,
name: 'hexagon'
}
},
{
2021-03-19 18:52:19 +08:00
name: 'pentagram',
icon: 'icon-pentagram',
data: {
text: 'pentagram',
rect: {
width: 100,
height: 100
},
2021-03-19 18:52:19 +08:00
name: 'pentagram'
}
2021-03-19 18:52:19 +08:00
}
]
2021-03-19 18:52:19 +08:00
}
]
2021-03-19 18:52:19 +08:00
export const imageTemp = {
name: 'rectangleImg',
icon: 'icon-image',
data: {
2021-03-19 18:52:19 +08:00
type: 0,
rect: {
x: 922,
y: 406,
width: 100,
height: 100
},
2021-04-28 15:51:04 +08:00
imageRatio: true,
2021-03-19 18:52:19 +08:00
lineWidth: 0,
rotate: 0,
offsetRotate: 0,
globalAlpha: 1,
dash: 0,
strokeStyle: '#000000',
animatePos: 0,
name: 'rectangleImg',
lineDashOffset: 0,
text: '',
textOffsetX: 0,
textOffsetY: 0,
visible: true,
zRotate: 0,
animateDuration: 0,
animateFrames: [],
animateFrame: 0,
borderRadius: 0,
icon: '',
image: '',
imageAlign: 'center',
bkType: 0,
gradientAngle: 0,
gradientRadius: 0.01,
paddingTop: 5,
paddingBottom: 5,
paddingLeft: 5,
paddingRight: 5,
paddingLeftNum: 5,
paddingRightNum: 5,
paddingTopNum: 5,
paddingBottomNum: 5,
fullIconRect: { width: 80, height: 90, center: { x: 972, y: 456 }, ex: 1012, ey: 496 }
}
2021-03-19 18:52:19 +08:00
}
/* 自定义图片组件 */
export function myShape (ctx, node) { // 自定义图片组件
ctx.beginPath()
ctx.rect(node.rect.x, node.rect.y, node.rect.width, node.rect.height)
if (node.data && node.data.lineWidth <= 0) {
2021-03-19 18:52:19 +08:00
ctx.strokeStyle = 'rgba(0,0,0,0)'
}
// 必须判空再填充
2021-03-19 18:52:19 +08:00
(node.fillStyle || node.bkType) && ctx.fill()
ctx.stroke()
}
2021-03-19 18:52:19 +08:00
export function myAnchors (node) {
node.anchors.push(new Point(node.rect.x, node.rect.y + node.rect.height / 2, Direction.Left))
node.anchors.push(new Point(node.rect.x + node.rect.width / 2, node.rect.y, Direction.Up))
node.anchors.push(new Point(node.rect.x + node.rect.width, node.rect.y + node.rect.height / 2, Direction.Right))
node.anchors.push(new Point(node.rect.x + node.rect.width / 2, node.rect.y + node.rect.height, Direction.Bottom))
}
2021-03-19 18:52:19 +08:00
export function myIconRect (node) {
node.iconRect = new Rect(node.rect.x + node.paddingLeft, node.rect.y + node.paddingTop, node.rect.width - (node.paddingLeft + node.paddingRight), node.rect.height - 20 - (node.paddingTop + node.paddingBottom))
node.fullIconRect = node.rect
}
2021-03-19 18:52:19 +08:00
export function myTextRect (node) {
node.textRect = new Rect(
2021-03-19 18:52:19 +08:00
node.rect.x + node.paddingLeft,
node.rect.y + node.rect.height - 20 - node.paddingBottom,
node.rect.width - (node.paddingLeft + node.paddingRight),
20
)
node.fullTextRect = node.rect
}
2021-03-19 18:52:19 +08:00
/* 自定义图片组件 */
2021-03-19 18:52:19 +08:00
/* 自定义立方体 */
export function myCubec (ctx, node) {
// 立方体
// ctx.rect(node.rect.x,node.rect.y,node.rect.width,node.rect.height);
2021-03-19 18:52:19 +08:00
const x = node.rect.x + 10; const y = node.rect.y + 10; const w = node.rect.width - 20; const h = node.rect.height - 20
// LINE MODE
2021-03-19 18:52:19 +08:00
ctx.lineJoin = 'round'
// center face
2021-03-19 18:52:19 +08:00
ctx.beginPath()
ctx.moveTo(x, y + h / 3)
ctx.lineTo(x + w * 2 / 3, y + h / 3)
ctx.lineTo(x + w * 2 / 3, y + h)
ctx.lineTo(x, y + h)
ctx.closePath()
ctx.fillStyle = node.fillStyle
ctx.strokeStyle = node.strokeStyle
ctx.stroke();
2021-03-19 18:52:19 +08:00
(node.fillStyle || node.bkType) && ctx.fill()
// top face
2021-03-19 18:52:19 +08:00
ctx.beginPath()
ctx.moveTo(x, y + h / 3)
ctx.lineTo(x + w / 3, y)
ctx.lineTo(x + w, y)
ctx.lineTo(x + w * 2 / 3, y + h / 3)
ctx.closePath()
ctx.fillStyle = node.fillStyle
ctx.strokeStyle = node.strokeStyle
ctx.stroke();
2021-03-19 18:52:19 +08:00
(node.fillStyle || node.bkType) && ctx.fill()
// right face
2021-03-19 18:52:19 +08:00
ctx.beginPath()
ctx.moveTo(x + w * 2 / 3, y + h / 3)
ctx.lineTo(x + w, y)
ctx.lineTo(x + w, y + h * 2 / 3)
ctx.lineTo(x + w * 2 / 3, y + h)
ctx.closePath()
ctx.fillStyle = node.fillStyle
ctx.strokeStyle = node.strokeStyle
ctx.stroke();
2021-03-19 18:52:19 +08:00
(node.fillStyle || node.bkType) && ctx.fill()
// 必须判空再填充
}
2021-03-19 18:52:19 +08:00
export function myCubeAnchors (node) { // 立方体锚点
node.anchors.push(new Point(node.rect.x, node.rect.y + node.rect.height / 2, Direction.Left))
node.anchors.push(new Point(node.rect.x + node.rect.width / 2, node.rect.y, Direction.Up))
node.anchors.push(new Point(node.rect.x + node.rect.width, node.rect.y + node.rect.height / 2, Direction.Right))
node.anchors.push(new Point(node.rect.x + node.rect.width / 2, node.rect.y + node.rect.height, Direction.Bottom))
}
2021-03-19 18:52:19 +08:00
/* 自定义立方体 */
2021-03-19 18:52:19 +08:00
export function onChangeAnimate (node, animateType, fillStyle, strokeStyle) {
node.animateType = animateType
if (node.animateType === 'custom') {
2021-03-19 18:52:19 +08:00
return
}
2021-03-19 18:52:19 +08:00
node.animateFrames = []
const state = Node.cloneState(node)
switch (animateType) {
case 'upDown':
2021-03-19 18:52:19 +08:00
state.rect.y -= 10
state.rect.ey -= 10
node.animateFrames.push({
duration: 100,
linear: true,
state
2021-03-19 18:52:19 +08:00
})
node.animateFrames.push({
duration: 100,
linear: true,
state: Node.cloneState(node)
2021-03-19 18:52:19 +08:00
})
node.animateFrames.push({
duration: 200,
linear: true,
state: Node.cloneState(state)
2021-03-19 18:52:19 +08:00
})
break
case 'leftRight':
2021-03-19 18:52:19 +08:00
state.rect.x -= 10
state.rect.ex -= 10
node.animateFrames.push({
duration: 100,
linear: true,
state: Node.cloneState(state)
2021-03-19 18:52:19 +08:00
})
state.rect.x += 20
state.rect.ex += 20
node.animateFrames.push({
duration: 80,
linear: true,
state: Node.cloneState(state)
2021-03-19 18:52:19 +08:00
})
state.rect.x -= 20
state.rect.ex -= 20
node.animateFrames.push({
duration: 50,
linear: true,
state: Node.cloneState(state)
2021-03-19 18:52:19 +08:00
})
state.rect.x += 20
state.rect.ex += 20
node.animateFrames.push({
duration: 30,
linear: true,
state: Node.cloneState(state)
2021-03-19 18:52:19 +08:00
})
node.animateFrames.push({
duration: 300,
linear: true,
state: Node.cloneState(node)
2021-03-19 18:52:19 +08:00
})
break
case 'heart':
2021-03-19 18:52:19 +08:00
state.rect.x -= 5
state.rect.ex += 5
state.rect.y -= 5
state.rect.ey += 5
state.rect.width += 5
state.rect.height += 10
node.animateFrames.push({
duration: 100,
linear: true,
state
2021-03-19 18:52:19 +08:00
})
node.animateFrames.push({
duration: 400,
linear: true,
state: Node.cloneState(node)
2021-03-19 18:52:19 +08:00
})
break
case 'success':
2021-03-19 18:52:19 +08:00
state.strokeStyle = strokeStyle || '#237804'
node.animateFrames.push({
duration: 100,
linear: true,
state
2021-03-19 18:52:19 +08:00
})
node.animateFrames.push({
duration: 100,
linear: true,
state: Node.cloneState(node)
2021-03-19 18:52:19 +08:00
})
state.strokeStyle = '#237804'
node.animateFrames.push({
duration: 100,
linear: true,
state
2021-03-19 18:52:19 +08:00
})
node.animateFrames.push({
duration: 100,
linear: true,
state: Node.cloneState(node)
2021-03-19 18:52:19 +08:00
})
state.strokeStyle = strokeStyle || '#237804'
state.fillStyle = fillStyle || '#389e0d22'
node.animateFrames.push({
duration: 3000,
linear: true,
state
2021-03-19 18:52:19 +08:00
})
break
case 'warning':
2021-03-19 18:52:19 +08:00
state.strokeStyle = strokeStyle || '#fa8c16'
state.fillStyle = fillStyle || '#fa8c16'
state.lineWidth = 5
state.dash = 2
node.animateFrames.push({
duration: 500,
linear: true,
state
2021-03-19 18:52:19 +08:00
})
state.strokeStyle = strokeStyle || '#fa8c16'
state.dash = 0
state.lineWidth = 1
state.fillStyle = '#ffffff'
node.animateFrames.push({
duration: 500,
linear: true,
state: Node.cloneState(state)
2021-03-19 18:52:19 +08:00
})
state.strokeStyle = strokeStyle || '#fa8c16'
state.dash = 2
state.lineWidth = 5
state.fillStyle = fillStyle || '#fa8c16'
node.animateFrames.push({
duration: 300,
linear: true,
state: Node.cloneState(state)
2021-03-19 18:52:19 +08:00
})
break
case 'error':
2021-03-19 18:52:19 +08:00
state.strokeStyle = strokeStyle || '#cf1322'
state.fillStyle = fillStyle || '#cf132222'
state.lineWidth = 5
state.dash = 2
node.animateFrames.push({
duration: 100,
linear: true,
state
2021-03-19 18:52:19 +08:00
})
state.strokeStyle = strokeStyle || '#cf1322'
state.fillStyle = '#ffffff'
state.dash = 0
state.lineWidth = 1
node.animateFrames.push({
duration: 500,
linear: true,
state: Node.cloneState(state)
2021-03-19 18:52:19 +08:00
})
state.strokeStyle = strokeStyle || '#cf1322'
state.fillStyle = fillStyle || '#cf132222'
state.dash = 2
state.lineWidth = 5
node.animateFrames.push({
duration: 300,
linear: true,
state: Node.cloneState(state)
2021-03-19 18:52:19 +08:00
})
break
case 'show':
2021-03-19 18:52:19 +08:00
state.strokeStyle = strokeStyle || '#fa541c'
state.rotate = -5
node.animateFrames.push({
duration: 100,
linear: true,
state: Node.cloneState(state)
2021-03-19 18:52:19 +08:00
})
state.rotate = 5
node.animateFrames.push({
duration: 100,
linear: true,
state: Node.cloneState(state)
2021-03-19 18:52:19 +08:00
})
state.rotate = 0
node.animateFrames.push({
duration: 100,
linear: true,
state: Node.cloneState(state)
2021-03-19 18:52:19 +08:00
})
break
2021-02-22 11:35:37 +08:00
case 'fade':
2021-03-19 18:52:19 +08:00
state.strokeStyle = strokeStyle || '#fa541c'
state.globalAlpha = 0.3
2021-02-22 11:35:37 +08:00
node.animateFrames.push({
duration: 300,
linear: true,
state: Node.cloneState(state)
2021-03-19 18:52:19 +08:00
})
state.globalAlpha = 0.5
2021-02-22 11:35:37 +08:00
node.animateFrames.push({
duration: 300,
linear: true,
state: Node.cloneState(state)
2021-03-19 18:52:19 +08:00
})
state.globalAlpha = 0.8
2021-02-22 11:35:37 +08:00
node.animateFrames.push({
duration: 300,
linear: true,
state: Node.cloneState(state)
2021-03-19 18:52:19 +08:00
})
state.globalAlpha = 1
2021-02-22 11:35:37 +08:00
node.animateFrames.push({
duration: 300,
linear: true,
state: Node.cloneState(state)
2021-03-19 18:52:19 +08:00
})
break
}
2021-03-19 18:52:19 +08:00
node.animatePlay = true
}
2021-03-19 18:52:19 +08:00
export function onChangeAnimateLine (line, type) {
line.animateType = type
line.animatePlay = true
}
export function changeImage (dataImg, callback) {
2021-03-19 18:52:19 +08:00
// const base64Img = document.createElement('base64Img')
const canvas = document.createElement('canvas')
const context = canvas.getContext('2d')
// 创建新图片
const img = new Image()
img.src = dataImg
img.addEventListener(
'load',
function () {
// 绘制图片到canvas上
canvas.width = img.width
canvas.height = img.height
context.drawImage(img, 0, 0)
2021-03-19 18:52:19 +08:00
// 将canvas的透明背景设置成白色
const imageData = context.getImageData(
0,
0,
canvas.width,
canvas.height
)
for (let i = 0; i < imageData.data.length; i += 4) {
// rgb大于250的透明度y均设置成0
if (
imageData.data[i] > 0 &&
imageData.data[i + 1] > 0 &&
imageData.data[i + 2] > 0
2021-03-19 18:52:19 +08:00
) {
imageData.data[i + 3] = 200
}
}
context.putImageData(imageData, 0, 0)
const baseImg = canvas.toDataURL('image/png')// 返回base64
if (typeof callback !== 'undefined') {
if (callback) callback(baseImg)
}
img.remove()
},
false
)
}