W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
解釋:畫(huà)布。畫(huà)布是一個(gè)矩形區(qū)域,開(kāi)發(fā)者可以在頁(yè)面上繪制圖形。canvas 擁有多種繪制路徑、矩形、圖形、字符以及添加圖像的方法。相關(guān) api:swan.createCanvasContext 該組件是客戶端創(chuàng)建的 原生組件,使用時(shí)請(qǐng)注意相關(guān)限制。
屬性名 | 類型 | 默認(rèn)值 | 必填 | 說(shuō)明 | ||
---|---|---|---|---|---|---|
canvas-id |
String |
是 |
canvas 組件的唯一標(biāo)識(shí)符 |
|||
disable-scroll |
Boolean |
false |
否 |
當(dāng)在 canvas 中移動(dòng)且有綁定手勢(shì)事件時(shí),禁止屏幕滾動(dòng)以及下拉刷新 |
||
bindtouchstart |
EventHandle |
否 |
手指觸摸動(dòng)作開(kāi)始 |
|||
bindtouchmove |
EventHandle |
否 |
手指觸摸后移動(dòng) |
|||
bindtouchend |
EventHandle |
否 |
手指觸摸動(dòng)作結(jié)束 |
|||
bindtouchcancel |
EventHandle |
否 |
手指觸摸動(dòng)作被打斷,如來(lái)電提醒,彈窗 |
|||
bindlongtap |
EventHandle |
否 |
手指長(zhǎng)按 350ms 之后觸發(fā),觸發(fā)了長(zhǎng)按事件后進(jìn)行移動(dòng)不會(huì)觸發(fā)屏幕的滾動(dòng) |
|||
binderror |
EventHandle |
否 |
當(dāng)發(fā)生錯(cuò)誤時(shí)觸發(fā) error 事件,detail = {errMsg: ‘something wrong’} |
<view class="wrap">
<view class="card-area">
<canvas
class="canvas"
canvas-id="myCanvas"
disable-scroll="false"
bindtouchstart="touchstart"
bindtouchmove="touchmove"
bindtouchend="touchend"
bindtouchcancel="touchcancel"
bindlongtap="longtap"
binderror="error">
</canvas>
</view>
</view>
Page({
data: {},
onUnload() {
this.interval && clearInterval(this.interval);
},
onReady() {
this.point = {
x: Math.random() * 305,
y: Math.random() * 305,
dx: Math.random() * 10,
dy: Math.random() * 10,
r: Math.round(Math.random() * 255 | 0),
g: Math.round(Math.random() * 255 | 0),
b: Math.round(Math.random() * 255 | 0)
};
this.interval = setInterval(this.draw.bind(this), 17);
// 使用 swan.createContext 獲取繪圖上下文 context
this.ctx = swan.createCanvasContext('myCanvas');
},
draw() {
const {ctx} = this;
ctx.setFillStyle('#FFF');
ctx.fillRect(0, 0, 610, 610);
ctx.beginPath();
ctx.arc(this.point.x, this.point.y, 14, 0, 2 * Math.PI);
ctx.setFillStyle('rgb(' + this.point.r + ', ' + this.point.g + ', ' + this.point.b + ')');
ctx.fill();
ctx.draw();
this.point.x += this.point.dx;
this.point.y += this.point.dy;
if (this.point.x <= 10 || this.point.x >= 305) {
this.point.dx = -this.point.dx;
this.point.r = Math.round(Math.random() * 255 | 0);
this.point.g = Math.round(Math.random() * 255 | 0);
this.point.b = Math.round(Math.random() * 255 | 0);
}
if (this.point.y <= 10 || this.point.y >= 305) {
this.point.dy = -this.point.dy;
this.point.r = Math.round(Math.random() * 255 | 0);
this.point.g = Math.round(Math.random() * 255 | 0);
this.point.b = Math.round(Math.random() * 255 | 0);
}
},
touchstart(e) {
console.log('touchstart', e);
},
touchmove(e) {
console.log('touchmove', e);
},
touchend(e) {
console.log('touchend', e);
},
touchcancel(e) {
console.log('touchcancel', e);
},
longtap(e) {
console.log('longtap', e);
},
error(e) {
console.log('error', e.detail.errMsg);
}
});
.canvas {
width: 610rpx;
height: 610rpx;
background-color: #fff;
margin-left: .3rem;
}
參考示例 1: 用 canvas 繪制一個(gè)圓形比例圖
<view class="wrap">
<view class="circlePostion">
<canvas class="circle" canvas-id="mycanvas"></canvas>
<view class="centerText">{{resultComment}}</view>
</view>
</view>
Page({
data: {
timer: ''
},
onLoad() {
let totalItems = 100;
let rightItems = 80;
let completePercent = parseInt((rightItems / totalItems) * 100);
this.getResultComment(completePercent);
this.showScoreAnimation(rightItems, totalItems);
},
showScoreAnimation(rightItems, totalItems) {
let that = this;
let copyRightItems = 0;
that.setData({
timer: setInterval(function () {
copyRightItems++;
if (copyRightItems == rightItems) {
clearInterval(that.data.timer)
} else {
// 灰色底層
let ctx = swan.createCanvasContext('mycanvas');
ctx.setLineWidth(6);
ctx.setStrokeStyle('#DCDCDC');
ctx.setLineCap('round');
ctx.beginPath();
ctx.arc(53, 53, 50, 0, 2 * Math.PI, false);
ctx.stroke();
ctx.setLineWidth(6);
ctx.setStrokeStyle('#38f');
ctx.setLineCap('round')
ctx.beginPath();
ctx.arc(53, 53, 50, -Math.PI * 1 / 2, 2 * Math.PI * (copyRightItems / totalItems) - Math.PI * 1 / 2, false);
ctx.stroke();
ctx.draw();
}
}, 20)
})
},
getResultComment(completePercent) {
let that = this;
switch (true) {
case completePercent < 60:
that.setData({
resultComment: "不及格"
})
break;
case completePercent >= 60 && completePercent <= 80:
that.setData({
resultComment: "中等"
})
break;
case completePercent >= 80 && completePercent < 90:
that.setData({
resultComment: "良好"
})
break;
case completePercent >= 90 && completePercent < 100:
that.setData({
resultComment: "優(yōu)秀"
})
}
},
})
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: