Skip to content

Commit b49adcd

Browse files
committed
update: canvas use renderjs
1 parent 7e1e602 commit b49adcd

3 files changed

Lines changed: 227 additions & 100 deletions

File tree

‎pages/component/canvas/animate.js‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
function Ball({
2+
x,
3+
y,
4+
vx,
5+
vy,
6+
canvasWidth,
7+
canvasHeight,
8+
ctx
9+
}) {
10+
this.canvasWidth = canvasWidth
11+
this.canvasHeight = canvasHeight
12+
this.ctx = ctx
13+
this.x = x
14+
this.y = y
15+
this.vx = vx
16+
this.vy = vy
17+
this.radius = 5
18+
}
19+
20+
Ball.prototype.draw = function() {
21+
this.ctx.beginPath()
22+
this.ctx.fillStyle = '#007AFF'
23+
this.ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI)
24+
this.ctx.closePath()
25+
this.ctx.fill()
26+
}
27+
28+
Ball.prototype.move = function() {
29+
this.x += this.vx
30+
this.y += this.vy
31+
// 回到中心
32+
// if (getDistance(this.x - this.canvasWidth / 2, this.y - this.canvasHeight / 2) >
33+
// getDistance(this.canvasWidth / 2, this.canvasHeight / 2) + this.radius) {
34+
// this.x = this.canvasWidth / 2
35+
// this.y = this.canvasHeight / 2
36+
// }
37+
38+
// 边框反弹
39+
if (this.x < this.radius) {
40+
this.vx = Math.abs(this.vx)
41+
return
42+
}
43+
if (this.x > this.canvasWidth - this.radius) {
44+
this.vx = -Math.abs(this.vx)
45+
}
46+
if (this.y < this.radius) {
47+
this.vy = Math.abs(this.vy)
48+
return
49+
}
50+
if (this.y > this.canvasWidth - this.radius) {
51+
this.vy = -Math.abs(this.vy)
52+
}
53+
}
54+
55+
function getDistance(x, y) {
56+
return Math.pow(Math.pow(x, 2) + Math.pow(y, 2), 0.5)
57+
}
58+
59+
export default {
60+
data() {
61+
return {}
62+
},
63+
methods: {
64+
start(newVal, oldVal, owner, ins) {
65+
let canvasWidth = ins.getDataset().width,
66+
canvasHeight = ins.getDataset().height,
67+
canvasEle = document.querySelectorAll('.canvas>canvas')[0],
68+
ctx = canvasEle.getContext('2d'),
69+
speed = 3,
70+
ballList = [],
71+
layer = 3,
72+
ballInlayer = 20
73+
for (let i = 0; i < layer; i++) {
74+
let radius = getDistance(canvasWidth / 2, canvasHeight / 2) / layer * i
75+
for (let j = 0; j < ballInlayer; j++) {
76+
let deg = j * 2 * Math.PI / ballInlayer,
77+
sin = Math.sin(deg),
78+
cos = Math.cos(deg),
79+
x = radius * cos + canvasWidth / 2,
80+
y = radius * sin + canvasHeight / 2,
81+
vx = speed * cos,
82+
vy = speed * sin
83+
ballList.push(new Ball({
84+
x,
85+
y,
86+
vx,
87+
vy,
88+
canvasWidth,
89+
canvasHeight,
90+
ctx,
91+
radius: 5
92+
}))
93+
}
94+
}
95+
96+
function animate(ballList) {
97+
ctx.clearRect(0, 0, canvasEle.width, canvasEle.height)
98+
ballList.forEach(function(item) {
99+
item.move()
100+
item.draw()
101+
})
102+
requestAnimationFrame(function() {
103+
animate(ballList)
104+
})
105+
}
106+
animate(ballList)
107+
}
108+
}
109+
}

‎pages/component/canvas/animate.wxs‎

Lines changed: 0 additions & 89 deletions
This file was deleted.

‎pages/component/canvas/canvas.vue‎

Lines changed: 118 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
<view class="page-body">
55
<view class="page-body-wrapper">
66
<!-- #ifdef APP-PLUS || H5 -->
7-
<canvas canvas-id="canvas" class="canvas" :start="start" :change:start="animate.start" :data-width="canvasWidth"
8-
:data-height="canvasWidth"></canvas>
7+
<canvas canvas-id="canvas" class="canvas" :start="startStatus" :change:start="animate.start"
8+
:data-width="canvasWidth" :data-height="canvasWidth"></canvas>
99
<!-- #endif -->
1010
<!-- #ifndef APP-PLUS || H5 -->
1111
<canvas canvas-id="canvas" id="canvas" class="canvas"></canvas>
@@ -15,12 +15,119 @@
1515
</view>
1616
</template>
1717

18-
<script module="animate" lang="wxs" src="./animate.wxs"></script>
18+
<script module="animate" lang="renderjs">
19+
function Ball({
20+
x,
21+
y,
22+
vx,
23+
vy,
24+
canvasWidth,
25+
canvasHeight,
26+
ctx
27+
}) {
28+
this.canvasWidth = canvasWidth
29+
this.canvasHeight = canvasHeight
30+
this.ctx = ctx
31+
this.x = x
32+
this.y = y
33+
this.vx = vx
34+
this.vy = vy
35+
this.radius = 5
36+
}
37+
38+
Ball.prototype.draw = function() {
39+
this.ctx.beginPath()
40+
this.ctx.fillStyle = '#007AFF'
41+
this.ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI)
42+
this.ctx.closePath()
43+
this.ctx.fill()
44+
}
45+
46+
Ball.prototype.move = function() {
47+
this.x += this.vx
48+
this.y += this.vy
49+
// 回到中心
50+
// if (getDistance(this.x - this.canvasWidth / 2, this.y - this.canvasHeight / 2) >
51+
// getDistance(this.canvasWidth / 2, this.canvasHeight / 2) + this.radius) {
52+
// this.x = this.canvasWidth / 2
53+
// this.y = this.canvasHeight / 2
54+
// }
55+
56+
// 边框反弹
57+
if (this.x < this.radius) {
58+
this.vx = Math.abs(this.vx)
59+
return
60+
}
61+
if (this.x > this.canvasWidth - this.radius) {
62+
this.vx = -Math.abs(this.vx)
63+
}
64+
if (this.y < this.radius) {
65+
this.vy = Math.abs(this.vy)
66+
return
67+
}
68+
if (this.y > this.canvasWidth - this.radius) {
69+
this.vy = -Math.abs(this.vy)
70+
}
71+
}
72+
73+
function getDistance(x, y) {
74+
return Math.pow(Math.pow(x, 2) + Math.pow(y, 2), 0.5)
75+
}
76+
77+
export default {
78+
methods: {
79+
start(newVal, oldVal, owner, ins) {
80+
let canvasWidth = ins.getDataset().width,
81+
canvasHeight = ins.getDataset().height,
82+
canvasEle = document.querySelectorAll('.canvas>canvas')[0],
83+
ctx = canvasEle.getContext('2d'),
84+
speed = 3,
85+
ballList = [],
86+
layer = 3,
87+
ballInlayer = 20
88+
for (let i = 0; i < layer; i++) {
89+
let radius = getDistance(canvasWidth / 2, canvasHeight / 2) / layer * i
90+
for (let j = 0; j < ballInlayer; j++) {
91+
let deg = j * 2 * Math.PI / ballInlayer,
92+
sin = Math.sin(deg),
93+
cos = Math.cos(deg),
94+
x = radius * cos + canvasWidth / 2,
95+
y = radius * sin + canvasHeight / 2,
96+
vx = speed * cos,
97+
vy = speed * sin
98+
ballList.push(new Ball({
99+
x,
100+
y,
101+
vx,
102+
vy,
103+
canvasWidth,
104+
canvasHeight,
105+
ctx,
106+
radius: 5
107+
}))
108+
}
109+
}
110+
111+
function animate(ballList) {
112+
ctx.clearRect(0, 0, canvasEle.width, canvasEle.height)
113+
ballList.forEach(function(item) {
114+
item.move()
115+
item.draw()
116+
})
117+
requestAnimationFrame(function() {
118+
animate(ballList)
119+
})
120+
}
121+
animate(ballList)
122+
}
123+
}
124+
}
125+
</script>
19126

20127
<script>
21128
// #ifndef APP-PLUS || H5
22129
23-
var ctx = null,
130+
let ctx = null,
24131
interval = null;
25132
26133
function Ball(x, y, vx, vy, canvasWidth, canvasHeight, ctx) {
@@ -80,7 +187,7 @@
80187
return {
81188
title: 'canvas',
82189
canvasWidth: 0,
83-
start: false,
190+
startStatus: false,
84191
ballList: []
85192
}
86193
},
@@ -89,7 +196,7 @@
89196
uni.createSelectorQuery().select(".canvas").boundingClientRect(data => {
90197
this.canvasWidth = data.width
91198
// #ifdef APP-PLUS || H5
92-
this.start = true
199+
this.startStatus = true
93200
// #endif
94201
// #ifndef APP-PLUS || H5
95202
ctx = uni.createCanvasContext('canvas')
@@ -104,16 +211,16 @@
104211
},
105212
methods: {
106213
drawBall: function() {
107-
var canvasWidth = this.canvasWidth,
214+
let canvasWidth = this.canvasWidth,
108215
canvasHeight = this.canvasWidth,
109216
speed = 3,
110217
ballList = [],
111218
layer = 3,
112219
ballInlayer = 20
113-
for (var i = 0; i < layer; i++) {
114-
var radius = getDistance(canvasWidth / 2, canvasHeight / 2) / layer * i
115-
for (var j = 0; j < ballInlayer; j++) {
116-
var deg = j * 2 * Math.PI / ballInlayer,
220+
for (let i = 0; i < layer; i++) {
221+
let radius = getDistance(canvasWidth / 2, canvasHeight / 2) / layer * i
222+
for (let j = 0; j < ballInlayer; j++) {
223+
let deg = j * 2 * Math.PI / ballInlayer,
117224
sin = Math.sin(deg),
118225
cos = Math.cos(deg),
119226
x = radius * cos + canvasWidth / 2,

0 commit comments

Comments
 (0)