|
1 | 1 | <template> |
2 | | - <view> |
3 | | - <page-head :title="title"></page-head> |
4 | | - <view class="page-body"> |
5 | | - <view class="page-body-wrapper"> |
6 | | - <canvas canvas-id="canvas" class="canvas"></canvas> |
7 | | - </view> |
8 | | - </view> |
9 | | - </view> |
| 2 | + <view> |
| 3 | + <page-head :title="title"></page-head> |
| 4 | + <view class="page-body"> |
| 5 | + <view class="page-body-wrapper"> |
| 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> |
| 9 | + <!-- #endif --> |
| 10 | + <!-- #ifndef APP-PLUS || H5 --> |
| 11 | + <canvas canvas-id="canvas" id="canvas" class="canvas"></canvas> |
| 12 | + <!-- #endif --> |
| 13 | + </view> |
| 14 | + </view> |
| 15 | + </view> |
10 | 16 | </template> |
| 17 | + |
| 18 | +<script module="animate" lang="wxs" src="./animate.wxs"></script> |
| 19 | + |
11 | 20 | <script> |
12 | | - var context = null,interval = null; |
13 | | - |
14 | | - export default { |
15 | | - data() { |
16 | | - return { |
17 | | - title: 'canvas', |
18 | | - screenWidth:uni.getSystemInfoSync().windowWidth, |
19 | | - canvasWidth:0, |
20 | | - position: { |
21 | | - x: 150, |
22 | | - y: 150, |
23 | | - vx: 2, |
24 | | - vy: 2 |
25 | | - } |
26 | | - } |
27 | | - }, |
28 | | - onReady: function () { |
29 | | - context = uni.createCanvasContext('canvas'); |
30 | | - this.canvasWidth = this.screenWidth / 750 * 610; |
31 | | - this.position = { |
32 | | - x:this.canvasWidth/2, |
33 | | - y:this.canvasWidth/2, |
34 | | - vx:2, |
35 | | - vy:2 |
36 | | - } |
37 | | - this.drawBall(); |
38 | | - interval = setInterval(this.drawBall, 17) |
39 | | - }, |
40 | | - onUnload: function () { |
41 | | - clearInterval(interval); |
42 | | - this.position = { |
43 | | - x: 0, |
44 | | - y: 0, |
45 | | - vx: 0, |
46 | | - vy: 0 |
47 | | - } |
48 | | - }, |
49 | | - methods: { |
50 | | - drawBall: function () { |
51 | | - var p = this.position |
52 | | - p.x += p.vx |
53 | | - p.y += p.vy |
54 | | - if (p.x >= this.canvasWidth) { |
55 | | - p.vx = -2 |
56 | | - } |
57 | | - if (p.x <= 7) { |
58 | | - p.vx = 2 |
59 | | - } |
60 | | - if (p.y >= this.canvasWidth) { |
61 | | - p.vy = -2 |
62 | | - } |
63 | | - if (p.y <= 7) { |
64 | | - p.vy = 2 |
65 | | - } |
66 | | - function ball(x, y) { |
67 | | - context.beginPath(0) |
68 | | - context.arc(x, y, 5, 0, Math.PI * 2) |
69 | | - context.setFillStyle('#1aad19') |
70 | | - context.setStrokeStyle('rgba(1,1,1,0)') |
71 | | - context.fill() |
72 | | - context.stroke() |
73 | | - } |
| 21 | + // #ifndef APP-PLUS || H5 |
| 22 | +
|
| 23 | + var ctx = null, |
| 24 | + interval = null; |
| 25 | +
|
| 26 | + function Ball(x, y, vx, vy, canvasWidth, canvasHeight, ctx) { |
| 27 | + this.canvasWidth = canvasWidth |
| 28 | + this.canvasHeight = canvasHeight |
| 29 | + this.ctx = ctx |
| 30 | + this.x = x |
| 31 | + this.y = y |
| 32 | + this.vx = vx |
| 33 | + this.vy = vy |
| 34 | + this.radius = 5 |
| 35 | + } |
| 36 | +
|
| 37 | + Ball.prototype.draw = function() { |
| 38 | + this.ctx.setFillStyle('#007AFF') |
| 39 | + this.ctx.beginPath() |
| 40 | + this.ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI) |
| 41 | + this.ctx.closePath() |
| 42 | + this.ctx.fill() |
| 43 | + } |
| 44 | +
|
| 45 | + Ball.prototype.move = function() { |
| 46 | + this.x += this.vx |
| 47 | + this.y += this.vy |
| 48 | + // 回到中心 |
| 49 | + // if (getDistance(this.x - this.canvasWidth / 2, this.y - this.canvasHeight / 2) > |
| 50 | + // getDistance(this.canvasWidth / 2, this.canvasHeight / 2) + this.radius) { |
| 51 | + // this.x = this.canvasWidth / 2 |
| 52 | + // this.y = this.canvasHeight / 2 |
| 53 | + // } |
74 | 54 |
|
75 | | - ball(p.x, this.canvasWidth/2) |
76 | | - ball(this.canvasWidth/2, p.y) |
77 | | - ball(this.canvasWidth - p.x, this.canvasWidth/2) |
78 | | - ball(this.canvasWidth/2, this.canvasWidth - p.y) |
79 | | - ball(p.x, p.y) |
80 | | - ball(this.canvasWidth - p.x, this.canvasWidth - p.y) |
81 | | - ball(p.x, this.canvasWidth - p.y) |
82 | | - ball(this.canvasWidth - p.x, p.y) |
| 55 | + // 边框反弹 |
| 56 | + if (this.x < this.radius) { |
| 57 | + this.vx = Math.abs(this.vx) |
| 58 | + return |
| 59 | + } |
| 60 | + if (this.x > this.canvasWidth - this.radius) { |
| 61 | + this.vx = -Math.abs(this.vx) |
| 62 | + } |
| 63 | + if (this.y < this.radius) { |
| 64 | + this.vy = Math.abs(this.vy) |
| 65 | + return |
| 66 | + } |
| 67 | + if (this.y > this.canvasWidth - this.radius) { |
| 68 | + this.vy = -Math.abs(this.vy) |
| 69 | + } |
| 70 | + } |
83 | 71 |
|
84 | | - context.draw() |
85 | | - } |
86 | | - } |
87 | | - } |
| 72 | + function getDistance(x, y) { |
| 73 | + return Math.pow(Math.pow(x, 2) + Math.pow(y, 2), 0.5) |
| 74 | + } |
| 75 | +
|
| 76 | + // #endif |
| 77 | +
|
| 78 | + export default { |
| 79 | + data() { |
| 80 | + return { |
| 81 | + title: 'canvas', |
| 82 | + canvasWidth: 0, |
| 83 | + start: false, |
| 84 | + ballList: [] |
| 85 | + } |
| 86 | + }, |
| 87 | + onReady: function() { |
| 88 | + this.$nextTick(() => { |
| 89 | + uni.createSelectorQuery().select(".canvas").boundingClientRect(data => { |
| 90 | + this.canvasWidth = data.width |
| 91 | + // #ifdef APP-PLUS || H5 |
| 92 | + this.start = true |
| 93 | + // #endif |
| 94 | + // #ifndef APP-PLUS || H5 |
| 95 | + ctx = uni.createCanvasContext('canvas') |
| 96 | + this.drawBall() |
| 97 | + // #endif |
| 98 | + }).exec() |
| 99 | + }) |
| 100 | + }, |
| 101 | + // #ifndef APP-PLUS || H5 |
| 102 | + onUnload: function() { |
| 103 | + clearInterval(interval); |
| 104 | + }, |
| 105 | + methods: { |
| 106 | + drawBall: function() { |
| 107 | + var canvasWidth = this.canvasWidth, |
| 108 | + canvasHeight = this.canvasWidth, |
| 109 | + speed = 3, |
| 110 | + ballList = [], |
| 111 | + layer = 3, |
| 112 | + 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, |
| 117 | + sin = Math.sin(deg), |
| 118 | + cos = Math.cos(deg), |
| 119 | + x = radius * cos + canvasWidth / 2, |
| 120 | + y = radius * sin + canvasHeight / 2, |
| 121 | + vx = speed * cos, |
| 122 | + vy = speed * sin |
| 123 | + ballList.push(new Ball(x, y, vx, vy, canvasWidth, canvasHeight, ctx)) |
| 124 | + } |
| 125 | + } |
| 126 | +
|
| 127 | + function animate(ballList) { |
| 128 | + // ctx.clearRect(0, 0, canvasWidth, canvasHeight) |
| 129 | + ballList.forEach(function(item) { |
| 130 | + item.move() |
| 131 | + item.draw() |
| 132 | + }) |
| 133 | + ctx.draw() |
| 134 | + } |
| 135 | +
|
| 136 | + interval = setInterval(function() { |
| 137 | + animate(ballList) |
| 138 | + }, 17) |
| 139 | + } |
| 140 | + } |
| 141 | + // #endif |
| 142 | + } |
88 | 143 | </script> |
89 | 144 |
|
90 | 145 | <style> |
91 | | - .canvas { |
92 | | - width: 610upx; |
93 | | - height: 610upx; |
94 | | - background-color: #fff; |
95 | | - } |
96 | | -</style> |
| 146 | + .page-body-wrapper { |
| 147 | + text-align: center; |
| 148 | + } |
| 149 | +
|
| 150 | + .canvas { |
| 151 | + width: 610rpx; |
| 152 | + height: 610rpx; |
| 153 | + margin: auto; |
| 154 | + background-color: #fff; |
| 155 | + } |
| 156 | +</style> |
0 commit comments