|
4 | 4 | <view class="page-body"> |
5 | 5 | <view class="page-body-wrapper"> |
6 | 6 | <!-- #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> |
9 | 9 | <!-- #endif --> |
10 | 10 | <!-- #ifndef APP-PLUS || H5 --> |
11 | 11 | <canvas canvas-id="canvas" id="canvas" class="canvas"></canvas> |
|
15 | 15 | </view> |
16 | 16 | </template> |
17 | 17 |
|
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> |
19 | 126 |
|
20 | 127 | <script> |
21 | 128 | // #ifndef APP-PLUS || H5 |
22 | 129 |
|
23 | | - var ctx = null, |
| 130 | + let ctx = null, |
24 | 131 | interval = null; |
25 | 132 |
|
26 | 133 | function Ball(x, y, vx, vy, canvasWidth, canvasHeight, ctx) { |
|
80 | 187 | return { |
81 | 188 | title: 'canvas', |
82 | 189 | canvasWidth: 0, |
83 | | - start: false, |
| 190 | + startStatus: false, |
84 | 191 | ballList: [] |
85 | 192 | } |
86 | 193 | }, |
|
89 | 196 | uni.createSelectorQuery().select(".canvas").boundingClientRect(data => { |
90 | 197 | this.canvasWidth = data.width |
91 | 198 | // #ifdef APP-PLUS || H5 |
92 | | - this.start = true |
| 199 | + this.startStatus = true |
93 | 200 | // #endif |
94 | 201 | // #ifndef APP-PLUS || H5 |
95 | 202 | ctx = uni.createCanvasContext('canvas') |
|
104 | 211 | }, |
105 | 212 | methods: { |
106 | 213 | drawBall: function() { |
107 | | - var canvasWidth = this.canvasWidth, |
| 214 | + let canvasWidth = this.canvasWidth, |
108 | 215 | canvasHeight = this.canvasWidth, |
109 | 216 | speed = 3, |
110 | 217 | ballList = [], |
111 | 218 | layer = 3, |
112 | 219 | 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, |
117 | 224 | sin = Math.sin(deg), |
118 | 225 | cos = Math.cos(deg), |
119 | 226 | x = radius * cos + canvasWidth / 2, |
|
0 commit comments