百度智能小程序 隱含的像素?cái)?shù)據(jù)

2020-09-05 14:21 更新

swan.canvasGetImageData

解釋:返回一個(gè)數(shù)組,用來(lái)描述 canvas 區(qū)域隱含的像素?cái)?shù)據(jù)。在自定義組件下,操作組件內(nèi) <canvas> 組件。

方法參數(shù)

Object object

object 參數(shù)說(shuō)明

屬性名類型必填默認(rèn)值說(shuō)明

canvasId

String

畫布標(biāo)識(shí),傳入 <canvas> 組件的 canvas-id 屬性。

x

Number

將要被提取的圖像數(shù)據(jù)矩形區(qū)域的左上角橫坐標(biāo)

y

Number

將要被提取的圖像數(shù)據(jù)矩形區(qū)域的左上角縱坐標(biāo)

width

Number

將要被提取的圖像數(shù)據(jù)矩形區(qū)域的寬度

height

Number

將要被提取的圖像數(shù)據(jù)矩形區(qū)域的高度

success

Function

接口調(diào)用成功的回調(diào)函數(shù)

fail

Function

接口調(diào)用失敗的回調(diào)函數(shù)

complete

Function

接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行)

success 返回參數(shù)說(shuō)明

參數(shù)類型說(shuō)明

width

Number

圖像數(shù)據(jù)矩形的寬度

height

Number

圖像數(shù)據(jù)矩形的高度

data

Uint8ClampedArray

圖像像素點(diǎn)數(shù)據(jù),一維數(shù)組,每四項(xiàng)表示一個(gè)像素點(diǎn)的 rgba 。

示例 

在開發(fā)者工具中打開



圖片示例

代碼示例

<canvas canvas-id="myCanvas" class="canvas" />
<button type="primary" bindtap="canvasGetImageData">canvasGetImageData</button>
Page({
    onReady: function () {
        const CanvasContext = this.createCanvasContext('myCanvas');
        CanvasContext.setFillStyle('#0f0f0f');
        CanvasContext.arc(100, 100, 50, 0, 2 * Math.PI);
        CanvasContext.fill();
        CanvasContext.beginPath();
        CanvasContext.setStrokeStyle('#0000ff');
        CanvasContext.moveTo(90, 70);
        CanvasContext.lineTo(70, 80);
        CanvasContext.lineTo(90, 80);
        CanvasContext.closePath();
        CanvasContext.stroke();
        CanvasContext.beginPath();
        CanvasContext.setStrokeStyle('#0000ff');
        CanvasContext.moveTo(130, 70);
        CanvasContext.lineTo(110, 80);
        CanvasContext.lineTo(130, 80);
        CanvasContext.closePath();
        CanvasContext.stroke();
        CanvasContext.beginPath();
        CanvasContext.setFillStyle('#00ff00');
        CanvasContext.arc(100, 100, 20, 0, 1 * Math.PI);
        CanvasContext.fill();
        CanvasContext.setFillStyle('#00ff00');
        CanvasContext.setFontSize(12);
        CanvasContext.fillText('hello', 165, 78);
        CanvasContext.moveTo(100, 50);
        CanvasContext.setStrokeStyle('#00ff00');
        CanvasContext.bezierCurveTo(100, 25, 75, 25, 50, 50);
        CanvasContext.stroke();
        CanvasContext.moveTo(100, 50);
        CanvasContext.quadraticCurveTo(75, 25, 50, 50);
        CanvasContext.stroke();
        CanvasContext.draw();
    },
    canvasGetImageData() {
        swan.canvasGetImageData({
            canvasId: 'myCanvas',
            x: 0,
            y: 0,
            width: 100,
            height: 100,
            success: res => {
                console.log('canvasGetImageData success', res);
                swan.showModal({
                    title: '返回結(jié)果',
                    content: JSON.stringify(res)
                });
            },
            fail: err => {
                console.log('canvasGetImageData fail', err);
            }
        });
    }
});

錯(cuò)誤碼

Android

錯(cuò)誤碼說(shuō)明

201

解析失敗,請(qǐng)檢查調(diào)起協(xié)議是否合法。

iOS

錯(cuò)誤碼說(shuō)明

202

解析失敗,請(qǐng)檢查參數(shù)是否正確。


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

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

編程獅公眾號(hào)