@tarojs/taro-h5
Version:
Taro h5 framework
31 lines (28 loc) • 1.28 kB
JavaScript
import { findDOM } from '../../utils/index.js';
import { MethodHandler } from '../../utils/handler.js';
/**
* 将像素数据绘制到画布。在自定义组件下,第二个参数传入自定义组件实例 this,以操作组件内 <canvas> 组件。
*/
const canvasPutImageData = ({ canvasId, data, x, y, width, height, success, fail, complete }, inst) => {
const handle = new MethodHandler({ name: 'canvasPutImageData', success, fail, complete });
try {
const el = findDOM(inst);
const canvas = el === null || el === void 0 ? void 0 : el.querySelector(`canvas[canvas-id="${canvasId}"]`);
const ctx = canvas === null || canvas === void 0 ? void 0 : canvas.getContext('2d');
if (!ctx)
throw new Error(`canvas ${canvasId} is not available`);
if (data.length !== width * height * 4)
throw new Error('pixel data length does not match width and height');
const imageData = ctx.createImageData(width, height);
imageData.data.set(data);
ctx.putImageData(imageData, x, y);
return handle.success();
}
catch (e) {
return handle.fail({
errMsg: e.message
});
}
};
export { canvasPutImageData };
//# sourceMappingURL=canvasPutImageData.js.map