207版本:canvas电流流向功能,海外自定义电价功能
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
<template>
|
||||
<view class="warp">
|
||||
<topoCanvas cId="secondcanvas" :width="'100%'" :height="'100%'" :canvas-data="canvasData" :noloading="noloading"/>
|
||||
<topoCanvas ref="topoCanvasRef" cId="secondcanvas" :width="'100%'" :height="'100%'" :canvas-data="canvasData" :noloading="noloading"/>
|
||||
</view>
|
||||
</template>
|
||||
<script>
|
||||
@ -266,7 +266,175 @@
|
||||
isMove: false,
|
||||
r: 3,
|
||||
},
|
||||
]
|
||||
],
|
||||
animationId: null, // requestAnimationFrame ID
|
||||
movingPoints: [
|
||||
// 点1:左上角 → 向右移动
|
||||
{
|
||||
x: 60,
|
||||
y: 30,
|
||||
path: [{
|
||||
x: 60,
|
||||
y: 30
|
||||
}, // 起点
|
||||
{
|
||||
x: 240,
|
||||
y: 30
|
||||
} // 终点
|
||||
],
|
||||
currentStep: 0, // 当前在路径的哪一步
|
||||
isActive: false, // 标识动画是否激活
|
||||
id: 'point-1'
|
||||
},
|
||||
// 点2:左上角 → 向右 → 向下
|
||||
{
|
||||
x: 60,
|
||||
y: 30,
|
||||
path: [{
|
||||
x: 60,
|
||||
y: 30
|
||||
},
|
||||
{
|
||||
x: 110,
|
||||
y: 30
|
||||
},
|
||||
{
|
||||
x: 110,
|
||||
y: 85
|
||||
}
|
||||
],
|
||||
currentStep: 0,
|
||||
isActive: false, // 标识动画是否激活
|
||||
id: 'point-2'
|
||||
},
|
||||
// 点3:中间下 → 向下 → 向左 → 向下
|
||||
{
|
||||
x: 110,
|
||||
y: 127,
|
||||
path: [{
|
||||
x: 110,
|
||||
y: 127
|
||||
},
|
||||
{
|
||||
x: 110,
|
||||
y: 150
|
||||
},
|
||||
{
|
||||
x: 60,
|
||||
y: 150
|
||||
},
|
||||
{
|
||||
x: 60,
|
||||
y: 190
|
||||
}
|
||||
],
|
||||
currentStep: 0,
|
||||
isActive: false, // 标识动画是否激活
|
||||
id: 'point-3'
|
||||
},
|
||||
// 点5:中间下 → 向下 → 向右 → 向下
|
||||
{
|
||||
x: 110,
|
||||
y: 127,
|
||||
path: [{
|
||||
x: 110,
|
||||
y: 127
|
||||
},
|
||||
{
|
||||
x: 110,
|
||||
y: 150
|
||||
},
|
||||
{
|
||||
x: 270,
|
||||
y: 150
|
||||
},
|
||||
{
|
||||
x: 270,
|
||||
y: 190
|
||||
}
|
||||
],
|
||||
currentStep: 0,
|
||||
isActive: false, // 标识动画是否激活
|
||||
id: 'point-5'
|
||||
},
|
||||
// 点6:中间上 → 向上 → 向右
|
||||
{
|
||||
x: 110,
|
||||
y: 85,
|
||||
path: [{
|
||||
x: 110,
|
||||
y: 85
|
||||
},
|
||||
{
|
||||
x: 110,
|
||||
y: 30
|
||||
},
|
||||
{
|
||||
x: 240,
|
||||
y: 30
|
||||
} // 终点
|
||||
],
|
||||
currentStep: 0,
|
||||
isActive: false, // 标识动画是否激活
|
||||
id: 'point-6'
|
||||
},
|
||||
// 点7:左下角 → 向上 → 向右 → 向上
|
||||
{
|
||||
x: 60,
|
||||
y: 190,
|
||||
path: [{
|
||||
x: 60,
|
||||
y: 190
|
||||
},
|
||||
{
|
||||
x: 60,
|
||||
y: 150
|
||||
},
|
||||
{
|
||||
x: 110,
|
||||
y: 150
|
||||
},
|
||||
{
|
||||
x: 110,
|
||||
y: 127
|
||||
}
|
||||
],
|
||||
currentStep: 0,
|
||||
isActive: false, // 标识动画是否激活
|
||||
id: 'point-7'
|
||||
},
|
||||
// 点9:右下角 → 向上 → 向左 → 向上
|
||||
{
|
||||
x: 270,
|
||||
y: 190,
|
||||
path: [{
|
||||
x: 270,
|
||||
y: 190
|
||||
},
|
||||
{
|
||||
x: 270,
|
||||
y: 150
|
||||
},
|
||||
{
|
||||
x: 110,
|
||||
y: 150
|
||||
},
|
||||
{
|
||||
x: 110,
|
||||
y: 127
|
||||
}
|
||||
],
|
||||
currentStep: 0,
|
||||
isActive: false, // 标识动画是否激活
|
||||
id: 'point-9'
|
||||
}
|
||||
],
|
||||
pointSpeed: 1.5, // 移动速度(像素/帧)
|
||||
kWValues: {
|
||||
left: null, // 存储 getLeftPcs 的 kW 值
|
||||
right: null // 存储 getRightPcs 的 kW 值
|
||||
}
|
||||
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
@ -274,8 +442,117 @@
|
||||
return this.vuex_currentStation;
|
||||
},
|
||||
},
|
||||
|
||||
beforeDestroy() {
|
||||
// 强制取消动画帧
|
||||
if (this.animationId) {
|
||||
if (typeof requestAnimationFrame !== 'undefined') {
|
||||
cancelAnimationFrame(this.animationId);
|
||||
} else {
|
||||
clearTimeout(this.animationId);
|
||||
}
|
||||
this.animationId = null; // 重置ID
|
||||
}
|
||||
// 重置所有动态点状态
|
||||
this.movingPoints.forEach(point => {
|
||||
point.isActive = false;
|
||||
point.currentStep = 0;
|
||||
point.x = point.path[0].x;
|
||||
point.y = point.path[0].y;
|
||||
});
|
||||
// 清空canvasData
|
||||
this.canvasData = [];
|
||||
},
|
||||
watch:{
|
||||
stationId(newVal,oldVal){
|
||||
if (newVal) {
|
||||
this.getData(newVal);
|
||||
}
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
// 启动动画
|
||||
startAnimation() {
|
||||
const canvasRef = this.$refs.topoCanvasRef;
|
||||
if (!canvasRef) return;
|
||||
|
||||
// 清除旧动画
|
||||
if (this.animationId !== null) {
|
||||
if (requestAnimationFrame && this.animationId) {
|
||||
cancelAnimationFrame(this.animationId);
|
||||
} else {
|
||||
clearTimeout(this.animationId);
|
||||
}
|
||||
this.animationId = null;
|
||||
}
|
||||
|
||||
// 直接启动新动画
|
||||
this.animate();
|
||||
},
|
||||
|
||||
animate() {
|
||||
const canvasRef = this.$refs.topoCanvasRef;
|
||||
if (!canvasRef) return;
|
||||
|
||||
const animateFrame = () => {
|
||||
// 1. 更新移动点的位置
|
||||
this.movingPoints.forEach(point => {
|
||||
if (!point.isActive) return;
|
||||
const currentIndex = point.currentStep;
|
||||
const nextTarget = point.path[currentIndex + 1];
|
||||
|
||||
if (!nextTarget) {
|
||||
point.x = point.path[0].x;
|
||||
point.y = point.path[0].y;
|
||||
point.currentStep = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
const dx = nextTarget.x - point.x;
|
||||
const dy = nextTarget.y - point.y;
|
||||
const distance = Math.sqrt(dx * dx + dy * dy);
|
||||
|
||||
if (distance < this.pointSpeed) {
|
||||
point.x = nextTarget.x;
|
||||
point.y = nextTarget.y;
|
||||
point.currentStep++;
|
||||
} else {
|
||||
point.x += (dx / distance) * this.pointSpeed;
|
||||
point.y += (dy / distance) * this.pointSpeed;
|
||||
}
|
||||
});
|
||||
|
||||
// 2. 更新 canvasData 中的动态点
|
||||
this.updateMovingPoints();
|
||||
|
||||
// 3. 清除并重绘
|
||||
canvasRef.clear();
|
||||
canvasRef.draw(null);
|
||||
|
||||
// 4. 继续下一帧(✅ 安全判断)
|
||||
if (typeof requestAnimationFrame !== 'undefined') {
|
||||
this.animationId = requestAnimationFrame(animateFrame);
|
||||
} else {
|
||||
this.animationId = setTimeout(animateFrame, 16);
|
||||
}
|
||||
};
|
||||
|
||||
// 启动第一帧(✅ 安全判断)
|
||||
if (typeof requestAnimationFrame !== 'undefined') {
|
||||
requestAnimationFrame(animateFrame);
|
||||
} else {
|
||||
setTimeout(animateFrame, 16);
|
||||
}
|
||||
},
|
||||
updateMovingPoints() {
|
||||
this.movingPoints.forEach(point => {
|
||||
const dataIndex = this.canvasData.findIndex(item => item.id === point.id);
|
||||
if (dataIndex !== -1) {
|
||||
this.canvasData[dataIndex].coord = [
|
||||
[point.x, point.y]
|
||||
];
|
||||
}
|
||||
});
|
||||
},
|
||||
changeEnglish(){
|
||||
this.textCanvasData = [
|
||||
{
|
||||
@ -332,14 +609,66 @@
|
||||
|
||||
},
|
||||
getData(val){
|
||||
// 彻底重置画布数据
|
||||
this.canvasData = [];
|
||||
// 重置所有点状态
|
||||
this.movingPoints.forEach(point => (point.isActive = false,point.isMove = false,point.currentStep = 0,point.x = point.path[0].x,point.y = point.path[0].y));
|
||||
this.stationId = val
|
||||
this.textCanvasData.splice(3)
|
||||
const api = [this.getLeftPcs(),this.getRightPcs(),this.getAmmeter()]
|
||||
Promise.all(api).finally((result) => {
|
||||
// 获取三个 kW 值
|
||||
const {
|
||||
left,
|
||||
right
|
||||
} = this.kWValues;
|
||||
const values = [left, right];
|
||||
const allZero = values.every(v => v === 0);
|
||||
const anyPositive = values.some(v => v > 0);
|
||||
const anyNegative = values.some(v => v < 0);
|
||||
|
||||
// 重置所有点(确保干净)
|
||||
this.movingPoints.forEach(p => (p.isActive = false));
|
||||
|
||||
// 条件3:全部为0 → 只显示点1
|
||||
if (allZero) {
|
||||
this.movingPoints.find(p => p.id === 'point-1').isActive = true;
|
||||
}
|
||||
// 否则按原逻辑处理
|
||||
else {
|
||||
// 条件1:任一大于0 → 点6运动
|
||||
if (anyPositive) {
|
||||
this.movingPoints.find(p => p.id === 'point-1').isActive = true;
|
||||
this.movingPoints.find(p => p.id === 'point-2').isActive = true;
|
||||
}
|
||||
// 条件2:任一小于0 → 点1和点2运动
|
||||
if (anyNegative) {
|
||||
this.movingPoints.find(p => p.id === 'point-6').isActive = true;
|
||||
}
|
||||
|
||||
// 再设置点3~9的显示逻辑
|
||||
this.updatePcsPoints();
|
||||
}
|
||||
this.canvasData = [...this.textCanvasData, ...this.imageCanvasData, ...this
|
||||
.lineCanvasData, ...this.circleCanvasData
|
||||
]
|
||||
// 动态添加 movingPoints
|
||||
this.movingPoints.forEach(point => {
|
||||
this.canvasData.push({
|
||||
type: "circle",
|
||||
id: point.id,
|
||||
coord: [
|
||||
[point.x, point.y]
|
||||
],
|
||||
color: "#3AECA1",
|
||||
r: 3,
|
||||
isMove: true
|
||||
});
|
||||
});
|
||||
this.noloading = true
|
||||
this.$nextTick(() => {
|
||||
this.startAnimation();
|
||||
});
|
||||
});
|
||||
},
|
||||
countChineseAndEnglishCharacters(str, x) {
|
||||
@ -406,7 +735,10 @@
|
||||
.then((res) => {
|
||||
if(res.data && res.data.length){
|
||||
res.data.forEach((item,index) => {
|
||||
|
||||
if (item.name.includes('kW')) { // 检查是否包含'kW'
|
||||
const value = parseFloat(item.value);
|
||||
this.kWValues.left = value; // 只记录
|
||||
}
|
||||
this.textCanvasData.push(
|
||||
{
|
||||
type: "text",
|
||||
@ -435,9 +767,13 @@
|
||||
)
|
||||
|
||||
})
|
||||
} else {
|
||||
this.kWValues.left = 0; // 没数据视为 0
|
||||
}
|
||||
|
||||
resolve()
|
||||
}).catch(() => {
|
||||
this.kWValues.left = 0; // 出错也视为 0
|
||||
resolve();
|
||||
})
|
||||
})
|
||||
|
||||
@ -453,7 +789,10 @@
|
||||
.then((res) => {
|
||||
if(res.data && res.data.length){
|
||||
res.data.forEach((item,index) => {
|
||||
|
||||
if (item.name.includes('kW')) { // 检查是否包含'kW'
|
||||
const value = parseFloat(item.value);
|
||||
this.kWValues.right = value;
|
||||
}
|
||||
this.textCanvasData.push(
|
||||
{
|
||||
type: "text",
|
||||
@ -482,12 +821,49 @@
|
||||
)
|
||||
|
||||
})
|
||||
} else {
|
||||
this.kWValues.right = 0;
|
||||
}
|
||||
|
||||
resolve()
|
||||
})
|
||||
}).catch(() => {
|
||||
this.kWValues.right = 0;
|
||||
resolve();
|
||||
})
|
||||
})
|
||||
|
||||
},
|
||||
updatePcsPoints() {
|
||||
const { left, right } = this.kWValues;
|
||||
|
||||
// 处理左边:点3 和 点7
|
||||
const point3 = this.movingPoints.find(p => p.id === 'point-3');
|
||||
const point7 = this.movingPoints.find(p => p.id === 'point-7');
|
||||
|
||||
if (left === 0) {
|
||||
point3.isActive = false;
|
||||
point7.isActive = false;
|
||||
} else if (left > 0) {
|
||||
point3.isActive = true;
|
||||
point7.isActive = false;
|
||||
} else if (left < 0) {
|
||||
point3.isActive = false;
|
||||
point7.isActive = true;
|
||||
}
|
||||
// 处理右边:点5 和 点9
|
||||
const point5 = this.movingPoints.find(p => p.id === 'point-5');
|
||||
const point9 = this.movingPoints.find(p => p.id === 'point-9');
|
||||
|
||||
if (right === 0) {
|
||||
point5.isActive = false;
|
||||
point9.isActive = false;
|
||||
} else if (right > 0) {
|
||||
point5.isActive = true;
|
||||
point9.isActive = false;
|
||||
} else if (right < 0) {
|
||||
point5.isActive = false;
|
||||
point9.isActive = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user