这里提供一种思路,可以使用canvas绘制6个不规则运动的圆:
1. 在uniapp页面中添加canvas组件:
```
<canvas canvas-id="myCanvas" style="width: 100%; height: 100vh"></canvas>
```
2. 在页面的onLoad生命周期函数中获取canvas上下文:
```
onLoad() {
const ctx = uni.createCanvasContext('myCanvas', this)
this.ctx = ctx
},
```
3. 定义6个不规则运动的圆的初始位置和速度:
```
data() {
return {
circles: [
{ x: 50, y: 50, vx: 2, vy: 1 },
{ x: 100, y: 100, vx: -1, vy: 2 },
{ x: 150, y: 150, vx: 1, vy: -2 },
{ x: 200, y: 200, vx: -2, vy: -1 },
{ x: 250, y: 250, vx: 2, vy: -1 },
{ x: 300, y: 300, vx: -1, vy: -2 },
]
}
},
```
4. 在canvas的绘制函数中循环绘制每个圆:
```
draw() {
const { circles } = this
const ctx = this.ctx
for (let i = 0; i < circles.length; i++) {
const { x, y } = circles[i]
ctx.beginPath()
// 绘制不规则圆
ctx.arc(x, y, 20, 0, 2 * Math.PI)
ctx.fillStyle = '#f00'
ctx.fill()
// 更新圆的位置
circles[i].x += circles[i].vx
circles[i].y += circles[i].vy
}
ctx.draw()
// 每隔一段时间重新执行绘制函数
setTimeout(this.draw, 30)
},
```
5. 在页面的onReady生命周期函数中调用绘制函数:
```
onReady() {
this.draw()
},
```
完整代码如下:
```
<template>
<canvas canvas-id="myCanvas" style="width: 100%; height: 100vh"></canvas>
</template>
<script>
export default {
data() {
return {
circles: [
{ x: 50, y: 50, vx: 2, vy: 1 },
{ x: 100, y: 100, vx: -1, vy: 2 },
{ x: 150, y: 150, vx: 1, vy: -2 },
{ x: 200, y: 200, vx: -2, vy: -1 },
{ x: 250, y: 250, vx: 2, vy: -1 },
{ x: 300, y: 300, vx: -1, vy: -2 },
]
}
},
onLoad() {
const ctx = uni.createCanvasContext('myCanvas', this)
this.ctx = ctx
},
onReady() {
this.draw()
},
methods: {
draw() {
const { circles } = this
const ctx = this.ctx
for (let i = 0; i < circles.length; i++) {
const { x, y } = circles[i]
ctx.beginPath()
// 绘制不规则圆
ctx.arc(x, y, 20, 0, 2 * Math.PI)
ctx.fillStyle = '#f00'
ctx.fill()
// 更新圆的位置
circles[i].x += circles[i].vx
circles[i].y += circles[i].vy
}
ctx.draw()
// 每隔一段时间重新执行绘制函数
setTimeout(this.draw, 30)
}
}
}
</script>
```