W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
解釋:停止監(jiān)聽加速度數(shù)據(jù)。
Object object
屬性名 | 類型 | 必填 | 默認值 | 說明 |
---|---|---|---|---|
success | Function | 否 | 接口調(diào)用成功的回調(diào)函數(shù) | |
fail | Function | 否 | 接口調(diào)用失敗的回調(diào)函數(shù) | |
complete | Function | 否 | 接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會執(zhí)行) |
<view class="wrap">
<view class="page-top">
<view class="page-top-text">{{title}}</view>
<view class="page-top-line"></view>
</view>
<view class="page-body-canvas">
<image mode="aspectFit" src="https://b.bdstatic.com/searchbox/icms/searchbox/img/gravity.png" rel="external nofollow" class="page-body-ball"></image>
<canvas class="page-body-ball" show="{{true}}" canvas-id="small-ball"></canvas>
</view>
<view class="page-body-xyz">
<text class="page-body-title">X: {{x}}</text>
<text class="page-body-title">Y: {{y}}</text>
<text class="page-body-title">Z: {{z}}</text>
</view>
<view class="page-body-btn">
<view class="middle-btn {{!isWatch ? '' : 'disabled'}}" bind:tap="startWatch">開始監(jiān)聽</view>
<view class="middle-btn {{!isWatch ? 'disabled' : ''}}" bind:tap="stopWatch">停止監(jiān)聽</view >
</view>
</view>
Page({
data: {
title: '傾斜手機即可移動下方小球',
isWatch: true,
x: 0,
y: 0,
z: 0
},
onReady() {
this.position = {
x: 151,
y: 151,
vx: 0,
vy: 0,
ax: 0,
ay: 0
};
swan.onAccelerometerChange(res => {
this.setData({
x: res.x.toFixed(2),
y: res.y.toFixed(2),
z: res.z.toFixed(2)
});
this.position.ax = Math.sin(res.x * Math.PI / 2);
this.position.ay = -Math.sin(res.y * Math.PI / 2);
});
this.interval = setInterval(() => {
this.drawSmallBall();
}, 20);
},
onShow() {
let isWatch = this.data.isWatch;
isWatch
? swan.startAccelerometer()
: swan.stopAccelerometer();
},
drawSmallBall() {
let p = this.position;
p.x = p.x + p.vx;
p.y = p.y + p.vy;
p.vx = p.vx + p.ax;
p.vy = p.vy + p.ay;
if (Math.sqrt(Math.pow(Math.abs(p.x) - 151, 2) + Math.pow(Math.abs(p.y) - 151, 2)) >= 115) {
if (p.x > 151 && p.vx > 0) {
p.vx = 0;
}
if (p.x < 151 && p.vx < 0) {
p.vx = 0;
}
if (p.y > 151 && p.vy > 0) {
p.vy = 0;
}
if (p.y < 151 && p.vy < 0) {
p.vy = 0;
}
}
let stx = this.createCanvasContext('small-ball');
stx.beginPath(0);
stx.arc(p.x, p.y, 15, 0, Math.PI * 2);
stx.setFillStyle('#3388FF');
stx.fill();
stx.draw();
},
startWatch() {
this.setData('isWatch', true);
swan.startAccelerometer();
},
stopWatch() {
this.setData('isWatch', false);
swan.stopAccelerometer();
},
onHide() {
clearInterval(this.interval);
app.globalData.openParams = '';
},
onUnload() {
clearInterval(this.interval);
}
});
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: