百度智能小程序組件 畫布

2020-08-28 15:36 更新

canvas


解釋:畫布

屬性說明:

屬性名 類型 默認(rèn)值 說明
canvas-id String canvas 組件的唯一標(biāo)識(shí)符
disable-scroll Boolean false 當(dāng)在 canvas 中移動(dòng)時(shí)且有綁定手勢事件時(shí),禁止屏幕滾動(dòng)以及下拉刷新
bindtouchstart EventHandle 手指觸摸動(dòng)作開始
bindtouchmove EventHandle 手指觸摸后移動(dòng)
bindtouchend EventHandle 手指觸摸動(dòng)作結(jié)束
bindtouchcancel EventHandle 手指觸摸動(dòng)作被打斷,如來電提醒,彈窗
bindlongtap EventHandle 手指長按 500ms 之后觸發(fā),觸發(fā)了長按事件后進(jìn)行移動(dòng)不會(huì)觸發(fā)屏幕的滾動(dòng)
binderror EventHandle 當(dāng)發(fā)生錯(cuò)誤時(shí)觸發(fā) error 事件,detail = {errMsg: ‘something wrong’}

canvas 組件默認(rèn)寬度 300px、高度 225px。

示例:

<canvas canvas-id="myCanvas" class="canvas-testing" bindtouchend="canvas" bindtouchmove="mvcan">
</canvas>
// canvas.js
Page({
    canvasIdErrorCallback: function (e) {
        console.error(e.detail.errMsg);
    },
    onReady: function (e) {
        const ctx = this.createCanvasContext('myCanvas');
        // 或swan.createCanvasContext('myCanvas');
        ctx.setFillStyle('#0f0f0f');
        ctx.arc(100, 100, 50, 0, 2 * Math.PI);
        ctx.fill();
        ctx.beginPath();
        ctx.setStrokeStyle('#0000ff');
        ctx.moveTo(90, 70);
        ctx.lineTo(70, 80);
        ctx.lineTo(90, 80);
        ctx.closePath();
        ctx.stroke();
        ctx.beginPath();
        ctx.setStrokeStyle('#0000ff');
        ctx.moveTo(130, 70);
        ctx.lineTo(110, 80);
        ctx.lineTo(130, 80);
        ctx.closePath();
        ctx.stroke();
        ctx.beginPath();
        ctx.setFillStyle('#00ff00');
        ctx.arc(100, 100, 20, 0, 1 * Math.PI);
        ctx.fill();
        ctx.setFillStyle('#00ff00');
        ctx.setFontSize(12);
        ctx.fillText('haha', 165, 78);
        ctx.moveTo(100, 50);
        ctx.setStrokeStyle('#00ff00');
        ctx.bezierCurveTo(100, 25, 75, 25, 50, 50);
        ctx.stroke();
        ctx.moveTo(100, 50);
        ctx.quadraticCurveTo(75, 25, 50, 50);
        ctx.stroke();
        ctx.draw();
    };
});
Bug & Tip: 1、canvas 組件是原生組件; 2、canvas 組件不能使用動(dòng)畫進(jìn)行控制;
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)