模板版本:v0.4.0
react-native-gcanvas
本项目基于 原库react-native-gcanvas 开发。
该第三方库支持直接从 npm 下载,包名为:@react-native-ohos/react-native-gcanvas 版本所属关系如下:
| 三方库名称 | 三方库版本 | 发布信息 | 支持RN版本 | Autolink | 编译API版本 | 社区基线版本 | npm地址 |
|---|---|---|---|---|---|---|---|
| @react-native-ohos/react-native-gcanvas | ~6.0.25(进行中) | Github Releases | 0.72.* 、0.77.* 、0.82.* | partially(0.72/0.82) | API12+ | 6.0.24 | Npm Address(进行中) |
1、安装与使用
进入到工程目录并输入以下命令:
npm
npm install react-native-gcanvas@6.0.24
npm install @react-native-ohos/react-native-gcanvas
yarn
yarn add react-native-gcanvas@6.0.24
yarn add @react-native-ohos/react-native-gcanvas
下面的代码展示了这个库的基本使用场景:
使用时 import 的库名不变。
import React from 'react';
import { View, StyleSheet, Text, ScrollView } from 'react-native';
import {GCanvasView} from '@flyskywhy/react-native-gcanvas';
const BasicShapesScreen: React.FC = () => {
const drawRectangles = (canvas: any) => {
const ctx = canvas.getContext('2d');
ctx.fillStyle = '#FF6B6B';
ctx.fillRect(10, 10, 60, 60);
ctx.strokeStyle = '#4ECDC4';
ctx.lineWidth = 3;
ctx.strokeRect(80, 10, 60, 60);
ctx.clearRect(25, 25, 30, 30);
ctx.fillStyle = '#45B7D1';
ctx.fillRect(10, 80, 130, 40);
ctx.strokeStyle = '#FFFFFF';
ctx.lineWidth = 2;
ctx.strokeRect(20, 90, 110, 20);
};
const handleCanvasCreate = (canvas: any) => {
setTimeout(() => {
drawRectangles(canvas);
}, 150);
};
return (
<ScrollView style={styles.container}>
<View style={styles.demoContainer}>
<View style={styles.canvasWrapper}>
<GCanvasView
style={styles.canvas}
isGestureResponsible={false}
onCanvasCreate={handleCanvasCreate}
/>
</View>
</View>
</ScrollView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 16,
},
demoContainer: {
marginBottom: 20,
backgroundColor: '#FFFFFF',
borderRadius: 12,
padding: 16,
shadowColor: '#000',
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.1,
shadowRadius: 4,
elevation: 3,
},
canvasWrapper: {
alignItems: 'center',
},
canvas: {
width: 150,
height: 120,
backgroundColor: '#F8F9FA',
borderRadius: 8,
},
});
export default BasicShapesScreen;
2、Link
| 是否支持autolink | RN框架版本 | |
|---|---|---|
| ~6.0.25 | partially(0.72/0.82) | 0.72.* 、0.77.* 、0.82.* |
使用AutoLink的工程需要根据该文档配置,Autolink框架指导文档:https://gitcode.com/openharmony-sig/ohos_react_native/blob/master/docs/zh-cn/Autolinking.md
如您使用的版本支持 Autolink,并且工程已接入 Autolink,可跳过ManualLink配置。
ManualLink: 此步骤为手动配置原生依赖项的指导
首先需要使用 DevEco Studio 打开项目里的 HarmonyOS 工程 harmony
2.1. Overrides RN SDK
为了让工程依赖同一个版本的 RN SDK,需要在工程根目录的 oh-package.json5 添加 overrides 字段,指向工程需要使用的 RN SDK 版本。替换的版本既可以是一个具体的版本号,也可以是一个模糊版本,还可以是本地存在的 HAR 包或源码目录。
关于该字段的作用请阅读官方说明
{
...
"overrides": {
"@rnoh/react-native-openharmony" : "./react_native_openharmony",
}
}
2.2. 引入原生端代码
目前有两种方法:
- 通过 har 包引入;
- 直接链接源码。
方法一:通过 har 包引入(推荐)
har 包位于三方库安装路径的 `harmony` 文件夹下。
打开 entry/oh-package.json5,添加以下依赖
"dependencies": {
"@rnoh/react-native-openharmony": "file:../react_native_openharmony",
"@react-native-ohos/react-native-gcanvas": "file:../../node_modules/@react-native-ohos/react-native-gcanvas/harmony/gcanvas.har",
}
点击右上角的 sync 按钮
或者在终端执行:
cd entry
ohpm install
方法二:直接链接源码
如需使用直接链接源码,请参考直接链接源码说明
2.3. 配置 CMakeLists 和引入 RNCGCanvasPackage
打开 entry/src/main/cpp/CMakeLists.txt,添加:
project(rnapp)
cmake_minimum_required(VERSION 3.4.1)
set(RNOH_APP_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(NODE_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../node_modules")
set(OH_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules")
set(RNOH_CPP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../react-native-harmony/harmony/cpp")
add_subdirectory("${RNOH_CPP_DIR}" ./rn)
# RNOH_END: manual_package_linking_1
add_subdirectory("../../../../sample_package/src/main/cpp" ./sample-package)
+ add_subdirectory("${OH_MODULES}/@react-native-ohos/react-native-gcanvas/src/main/cpp" ./gcanvas)
# RNOH_END: manual_package_linking_1
add_library(rnoh_app SHARED
"./PackageProvider.cpp"
"${RNOH_CPP_DIR}/RNOHAppNapiBridge.cpp"
)
target_link_libraries(rnoh_app PUBLIC rnoh)
# RNOH_BEGIN: manual_package_linking_2
target_link_libraries(rnoh_app PUBLIC rnoh_sample_package)
+ target_link_libraries(rnoh_app PUBLIC rnoh_gcanvas)
# RNOH_BEGIN: manual_package_linking_2
打开 entry/src/main/cpp/PackageProvider.cpp,添加:
#include "RNOH/PackageProvider.h"
#include "SamplePackage.h"
+ #include "RNCGCanvasPackage.h"
using namespace rnoh;
std::vector<std::shared_ptr<Package>> PackageProvider::getPackages(Package::Context ctx) {
return {
std::make_shared<SamplePackage>(ctx),
+ std::make_shared<RNCGCanvasPackage>(ctx)
};
}
2.4. 在 ArkTs 侧引入 RNCGCanvasPackage
打开 entry/src/main/ets/RNPackagesFactory.ts,添加:
...
+ import RNCGCanvasPackage from '@react-native-ohos/react-native-gcanvas'
export function createRNPackages(ctx: RNPackageContext): RNPackage[] {
return [
new SamplePackage(ctx),
+ new RNCGCanvasPackage(ctx),
];
}
2.5. 运行
点击右上角的 sync 按钮
或者在终端执行:
cd entry
ohpm install
然后编译、运行即可。
3. 约束与限制
3.1.兼容性
本文档内容基于以下版本验证通过:
- RNOH: 0.72.32; SDK: HarmonyOS 6.0.1.116 Release SDK; IDE: DevEco Studio 6.0.1.260; ROM: 6.0.0.130 SP15;
- RNOH: 0.77.59; SDK: HarmonyOS 6.0.1.116 Release SDK; IDE: DevEco Studio 6.0.1.260; ROM: 6.0.0.130 SP15;
- RNOH: 0.82.18; SDK: HarmonyOS 6.0.1.116 Release SDK; IDE: DevEco Studio 6.0.1.260; ROM: 6.0.0.130 SP15;
4. 属性/API
"Platform"列表示该属性在原三方库上支持的平台。
"HarmonyOS Support"列为 yes 表示 HarmonyOS 平台支持该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。
GCanvasView
| Name | Description | Type | Required | Platform | HarmonyOS Support |
|---|---|---|---|---|---|
| isGestureResponsible | 是否响应 Canvas 内部的手势事件,开启后支持鼠标/触摸事件分发 | boolean | No | Android and iOS | yes |
| isAutoClearRectBeforePutImageData | 仅影响 2D,putImageData 前是否自动 clearRect,设为 true 可与 Web 行为完全一致 | boolean | No | Android and iOS | yes |
| isResetGlViewportAfterSetWidthOrHeight | 设置 width/height 后是否重置 GL Viewport,2D 一般为 true,WebGL 3D 一般为 false | boolean | No | Android and iOS | yes |
| devicePixelRatio | 设备像素比,undefined 时默认使用 PixelRatio.get(),设为 1 可使用物理像素 | number | No | Android and iOS | yes |
| offscreenCanvas | 是否将此 Canvas 作为离屏 Canvas 供 document.createElement('canvas') 使用 | boolean | No | Android and iOS | yes |
| disableAutoSwap | 仅影响 WebGL,禁用自动交换缓冲区,由 APP 自行控制渲染循环 | boolean | No | Android and iOS | yes |
| onCanvasCreate | Canvas 创建完成回调,参数为 canvas 对象 | function | No | Android and iOS | yes |
| onCanvasResize | Canvas 尺寸变化回调,参数为 { width, height, canvas } | function | No | Android and iOS | yes |
| onIsReady | Canvas 就绪状态回调,Android 上有实际意义,iOS 始终为 true | function | No | Android and iOS | yes |
| onMouseMove | 鼠标/手指滑动回调,参数为 PointerEvent | function | No | Android and iOS | yes |
| onMouseDown | 鼠标/手指按下回调,参数为 PointerEvent | function | No | Android and iOS | yes |
| onMouseUp | 鼠标/手指抬起回调,参数为 PointerEvent | function | No | Android and iOS | yes |
| style | 组件样式,可设置width、height 等 | object | No | Android and iOS | yes |
GCanvas
| Name | Description | Type | Required | Platform | HarmonyOS Support |
|---|---|---|---|---|---|
| id | Canvas 的唯一标识(原生组件 ref) | string | No | Android and iOS | yes |
| width | Canvas 的宽度(像素) | number | No | Android and iOS | yes |
| height | Canvas 的高度(像素) | number | No | Android and iOS | yes |
| clientWidth | Canvas 的 CSS 宽度(只读) | number | No | Android and iOS | yes |
| clientHeight | Canvas 的 CSS 高度(只读) | number | No | Android and iOS | yes |
| disabled | Canvas 是否已禁用 | boolean | No | Android and iOS | yes |
| getContext | 获取渲染上下文,type 为 '2d' 或 'webgl',返回对应的 Context 对象 | function | No | Android and iOS | yes |
| toDataURL | 将 Canvas 内容导出为 Base64 编码的 Data URL,默认 'image/png' | function | No | Android and iOS | yes |
| reset | 重置 Canvas 状态 | function | No | Android and iOS | yes |
| _swapBuffers | 手动交换缓冲区 | function | No | Android and iOS | yes |
GImage
| Name | Description | Type | Required | Platform | HarmonyOS Support |
|---|---|---|---|---|---|
| width | 图片宽度 | number | No | Android and iOS | yes |
| height | 图片高度 | number | No | Android and iOS | yes |
| src | 图片源地址 | string | No | Android and iOS | yes |
| complete | 图片是否加载完成 | boolean | No | Android and iOS | yes |
| onload | 图片加载成功回调事件 | function | No | Android and iOS | yes |
| onerror | 图片加载失败回调事件 | function | No | Android and iOS | yes |
CanvasRenderingContext2D
| Name | Description | Type | Required | Platform | HarmonyOS Support |
|---|---|---|---|---|---|
| canvas | 关联的 Canvas 对象(只读) | GCanvas | No | Android and iOS | yes |
| fillStyle | 填充样式 | string / Gradient | No | Android and iOS | yes |
| strokeStyle | 描边样式 | string / Gradient | No | Android and iOS | yes |
| globalAlpha | 全局透明度,默认 1.0 | number | No | Android and iOS | yes |
| globalCompositeOperation | 合成操作,默认 'source-over' | string | No | Android and iOS | yes |
| lineWidth | 线宽,默认 1 | number | No | Android and iOS | yes |
| lineCap | 线端样式 ('butt' / 'round' / 'square') | string | No | Android and iOS | yes |
| lineJoin | 线连接样式 ('miter' / 'round' / 'bevel') | string | No | Android and iOS | yes |
| miterLimit | 斜接限制,默认 10 | number | No | Android and iOS | yes |
| lineDashOffset | 虚线偏移,默认 0 | number | No | Android and iOS | yes |
| shadowColor | 阴影颜色 | string | No | Android and iOS | yes |
| shadowBlur | 阴影模糊度 | number | No | Android and iOS | yes |
| shadowOffsetX | 阴影 X 偏移 | number | No | Android and iOS | yes |
| shadowOffsetY | 阴影 Y 偏移 | number | No | Android and iOS | yes |
| font | 字体样式,默认 '10px sans-serif' | string | No | Android and iOS | yes |
| textAlign | 文本对齐 ('start' / 'end' / 'left' / 'right' / 'center') | string | No | Android and iOS | yes |
| textBaseline | 文本基线 ('alphabetic' / 'top' / 'hanging' / 'middle' / 'ideographic' / 'bottom') | string | No | Android and iOS | yes |
| imageSmoothingEnabled | 图片平滑,默认 1 | number | No | Android and iOS | yes |
| fillRect(x, y, w, h) | 绘制填充矩形 | function | No | Android and iOS | yes |
| strokeRect(x, y, w, h) | 绘制描边矩形 | function | No | Android and iOS | yes |
| clearRect(x, y, w, h) | 清除矩形区域 | function | No | Android and iOS | yes |
| fillText(text, x, y) | 绘制填充文本 | function | No | Android and iOS | yes |
| strokeText(text, x, y) | 绘制描边文本 | function | No | Android and iOS | yes |
| measureText(text) | 测量文本宽度,返回 TextMetrics | function | No | Android and iOS | yes |
| beginPath() | 开始新路径 | function | No | Android and iOS | yes |
| closePath() | 关闭当前路径 | function | No | Android and iOS | yes |
| moveTo(x, y) | 移动画笔到指定点 | function | No | Android and iOS | yes |
| lineTo(x, y) | 绘制直线到指定点 | function | No | Android and iOS | yes |
| arc(x, y, radius, startAngle, endAngle, anticlockwise) | 绘制圆弧 | function | No | Android and iOS | yes |
| arcTo(x1, y1, x2, y2, radius) | 根据控制点和半径绘制圆弧 | function | No | Android and iOS | yes |
| bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y) | 绘制三次贝塞尔曲线 | function | No | Android and iOS | yes |
| quadraticCurveTo(cpx, cpy, x, y) | 绘制二次贝塞尔曲线 | function | No | Android and iOS | yes |
| rect(x, y, w, h) | 创建矩形子路径 | function | No | Android and iOS | yes |
| fill() | 填充当前路径 | function | No | Android and iOS | yes |
| stroke(path) | 描边当前路径 | function | No | Android and iOS | yes |
| clip() | 将当前路径裁剪为裁剪区域 | function | No | Android and iOS | yes |
| resetClip() | 重置裁剪区域 | function | No | Android and iOS | yes |
| save() | 保存绘图状态 | function | No | Android and iOS | yes |
| restore() | 恢复绘图状态 | function | No | Android and iOS | yes |
| drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh) | 绘制图片,支持 3/5/9 参数形式 | function | No | Android and iOS | yes |
| getImageData(sx, sy, sw, sh) | 获取指定区域的像素数据,返回 ImageData | function | No | Android and iOS | yes |
| putImageData(imageData, x, y, dirtyX, dirtyY, dirtyWidth, dirtyHeight) | 将像素数据绘制到 Canvas | function | No | Android and iOS | yes |
| createLinearGradient(x0, y0, x1, y1) | 创建线性渐变,返回 FillStyleLinearGradient | function | No | Android and iOS | yes |
| createRadialGradient(x0, y0, r0, x1, y1, r1) | 创建径向渐变,返回 FillStyleRadialGradient | function | No | Android and iOS | yes |
| createPattern(img, pattern) | 创建图案填充,返回 FillStylePattern | function | No | Android and iOS | yes |
| setLineDash(value) | 设置虚线模式 | function | No | Android and iOS | yes |
| getLineDash() | 获取当前虚线模式 | function | No | Android and iOS | yes |
| setTransform(a, b, c, d, tx, ty) | 重置并设置变换矩阵 | function | No | Android and iOS | yes |
| transform(a, b, c, d, tx, ty) | 追加变换矩阵 | function | No | Android and iOS | yes |
| resetTransform() | 重置变换矩阵为单位矩阵 | function | No | Android and iOS | yes |
| scale(a, d) | 缩放变换 | function | No | Android and iOS | yes |
| rotate(angle) | 旋转变换 | function | No | Android and iOS | yes |
| translate(tx, ty) | 平移变换 | function | No | Android and iOS | yes |
WebGLRenderingContext
| Name | Description | Type | Required | Platform | HarmonyOS Support |
|---|---|---|---|---|---|
| drawingBufferWidth | 绘图缓冲区宽度 | number | No | Android and iOS | yes |
| drawingBufferHeight | 绘图缓冲区高度 | number | No | Android and iOS | yes |
| componentId | 关联的 Canvas 组件 ID | string | No | Android and iOS | yes |
| getContextAttributes() | 获取上下文属性 | function | No | Android and iOS | yes |
| createBuffer() | 创建缓冲区对象,返回 WebGLBuffer | function | No | Android and iOS | yes |
| createProgram() | 创建着色器程序,返回 WebGLProgram | function | No | Android and iOS | yes |
| createShader(type) | 创建着色器,返回 WebGLShader | function | No | Android and iOS | yes |
| createTexture() | 创建纹理对象,返回 WebGLTexture | function | No | Android and iOS | yes |
| createFramebuffer() | 创建帧缓冲对象,返回 WebGLFramebuffer | function | No | Android and iOS | yes |
| createRenderbuffer() | 创建渲染缓冲对象,返回 WebGLRenderbuffer | function | No | Android and iOS | yes |
| shaderSource(shader, source) | 设置着色器源码 | function | No | Android and iOS | yes |
| compileShader(shader) | 编译着色器 | function | No | Android and iOS | yes |
| attachShader(program, shader) | 附加着色器到程序 | function | No | Android and iOS | yes |
| linkProgram(program) | 链接着色器程序 | function | No | Android and iOS | yes |
| useProgram(program) | 使用着色器程序 | function | No | Android and iOS | yes |
| bindBuffer(target, buffer) | 绑定缓冲区 | function | No | Android and iOS | yes |
| bufferData(target, data, usage) | 设置缓冲区数据 | function | No | Android and iOS | yes |
| bindTexture(target, texture) | 绑定纹理 | function | No | Android and iOS | yes |
| texImage2D(target, level, internalformat, format, type, image) | 设置二维纹理图像 | function | No | Android and iOS | yes |
| texSubImage2D(target, level, xoffset, yoffset, format, type, image) | 更新二维纹理子图像 | function | No | Android and iOS | yes |
| viewport(x, y, width, height) | 设置视口 | function | No | Android and iOS | yes |
| clear(mask) | 清除缓冲区 | function | No | Android and iOS | yes |
| clearColor(r, g, b, a) | 设置清除颜色 | function | No | Android and iOS | yes |
| enable(cap) | 启用功能 | function | No | Android and iOS | yes |
| disable(cap) | 禁用功能 | function | No | Android and iOS | yes |
| drawArrays(mode, first, count) | 绘制图元 | function | No | Android and iOS | yes |
| drawElements(mode, count, type, offset) | 绘制索引图元 | function | No | Android and iOS | yes |
| getAttribLocation(program, name) | 获取属性变量位置,返回 number | function | No | Android and iOS | yes |
| getUniformLocation(program, name) | 获取 Uniform 变量位置,返回 WebGLUniformLocation | function | No | Android and iOS | yes |
| vertexAttribPointer(index, size, type, normalized, stride, offset) | 设置顶点属性指针 | function | No | Android and iOS | yes |
| enableVertexAttribArray(index) | 启用顶点属性数组 | function | No | Android and iOS | yes |
| uniformfamily(location, value) | 设置 Uniform 变量值 | function | No | Android and iOS | yes |
| getShaderInfoLog(shader) | 获取着色器编译日志,返回 string | function | No | Android and iOS | yes |
| getProgramInfoLog(program) | 获取程序链接日志,返回 string | function | No | Android and iOS | yes |
| activeTexture(textureUnit) | 激活纹理单元 | function | No | Android and iOS | yes |
| bindAttribLocation(program, index, name) | 绑定属性变量位置 | function | No | Android and iOS | yes |
| bindFramebuffer(target, framebuffer) | 绑定帧缓冲区 | function | No | Android and iOS | yes |
| bindRenderbuffer(target, renderbuffer) | 绑定渲染缓冲区 | function | No | Android and iOS | yes |
| blendColor(r, g, b, a) | 设置混合常量颜色 | function | No | Android and iOS | yes |
| blendEquation(mode) | 设置混合方程 | function | No | Android and iOS | yes |
| blendEquationSeparate(modeRGB, modeAlpha) | 分别设置 RGB 和 Alpha 混合方程 | function | No | Android and iOS | yes |
| blendFunc(sfactor, dfactor) | 设置混合函数 | function | No | Android and iOS | yes |
| blendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha) | 分别设置 RGB 和 Alpha 混合函数 | function | No | Android and iOS | yes |
| bufferSubData(target, offset, data) | 更新缓冲区子数据 | function | No | Android and iOS | yes |
| checkFramebufferStatus(target) | 检查帧缓冲区完整性状态,返回 number | function | No | Android and iOS | yes |
| clearDepth(depth) | 设置深度缓冲区清除值 | function | No | Android and iOS | yes |
| clearStencil(s) | 设置模板缓冲区清除值 | function | No | Android and iOS | yes |
| colorMask(r, g, b, a) | 设置颜色通道写入掩码 | function | No | Android and iOS | yes |
| compressedTexImage2D(target, level, internalformat, width, height, border, pixels) | 加载压缩纹理图像 | function | No | Android and iOS | yes |
| compressedTexSubImage2D(target, level, xoffset, yoffset, width, height, format, pixels) | 更新压缩纹理子图像 | function | No | Android and iOS | yes |
| cullFace(mode) | 设置背面剔除模式 | function | No | Android and iOS | yes |
| deleteBuffer(buffer) | 删除缓冲区对象 | function | No | Android and iOS | yes |
| deleteFramebuffer(framebuffer) | 删除帧缓冲区对象 | function | No | Android and iOS | yes |
| deleteProgram(program) | 删除着色器程序 | function | No | Android and iOS | yes |
| deleteRenderbuffer(renderbuffer) | 删除渲染缓冲区对象 | function | No | Android and iOS | yes |
| deleteShader(shader) | 删除着色器 | function | No | Android and iOS | yes |
| deleteTexture(texture) | 删除纹理对象 | function | No | Android and iOS | yes |
| depthFunc(func) | 设置深度测试函数 | function | No | Android and iOS | yes |
| depthMask(flag) | 设置深度写入掩码 | function | No | Android and iOS | yes |
| depthRange(zNear, zFar) | 设置深度范围 | function | No | Android and iOS | yes |
| detachShader(program, shader) | 从程序中分离着色器 | function | No | Android and iOS | yes |
| disableVertexAttribArray(index) | 禁用顶点属性数组 | function | No | Android and iOS | yes |
| flush() | 刷新 GL 命令队列 | function | No | Android and iOS | yes |
| framebufferRenderbuffer(target, attachment, renderbuffertarget, renderbuffer) | 将渲染缓冲区附加到帧缓冲区 | function | No | Android and iOS | yes |
| framebufferTexture2D(target, attachment, textarget, texture, level) | 将纹理附加到帧缓冲区 | function | No | Android and iOS | yes |
| frontFace(mode) | 设置正面方向 | function | No | Android and iOS | yes |
| generateMipmap(target) | 生成 Mipmap | function | No | Android and iOS | yes |
| getActiveAttrib(program, index) | 获取活跃属性变量信息,返回 WebGLActiveInfo | function | No | Android and iOS | yes |
| getActiveUniform(program, index) | 获取活跃 Uniform 变量信息,返回 WebGLActiveInfo | function | No | Android and iOS | yes |
| getAttachedShaders(program) | 获取附加到程序的着色器列表,返回 WebGLShader[] | function | No | Android and iOS | yes |
| getBufferParameter(target, pname) | 获取缓冲区参数 | function | No | Android and iOS | yes |
| getError() | 获取错误状态,返回 number | function | No | Android and iOS | yes |
| getExtension(name) | 获取 WebGL 扩展 | function | No | Android and iOS | yes |
| getFramebufferAttachmentParameter(target, attachment, pname) | 获取帧缓冲区附件参数 | function | No | Android and iOS | yes |
| getParameter(pname) | 获取 GL 参数值 | function | No | Android and iOS | yes |
| getProgramParameter(program, pname) | 获取着色器程序参数 | function | No | Android and iOS | yes |
| getRenderbufferParameter(target, pname) | 获取渲染缓冲区参数 | function | No | Android and iOS | yes |
| getShaderParameter(shader, pname) | 获取着色器参数 | function | No | Android and iOS | yes |
| getShaderPrecisionFormat(shaderType, precisionType) | 获取着色器精度格式,返回 WebGLShaderPrecisionFormat | function | No | Android and iOS | yes |
| getShaderSource(shader) | 获取着色器源码,返回 string | function | No | Android and iOS | yes |
| getSupportedExtensions() | 获取支持的扩展列表,返回 string[] | function | No | Android and iOS | yes |
| getTexParameter(target, pname) | 获取纹理参数 | function | No | Android and iOS | yes |
| getVertexAttrib(index, pname) | 获取顶点属性值 | function | No | Android and iOS | yes |
| getVertexAttribOffset(index, pname) | 获取顶点属性偏移量,返回 number | function | No | Android and iOS | yes |
| isBuffer(buffer) | 检测是否为缓冲区对象,返回 boolean | function | No | Android and iOS | yes |
| isContextLost() | 检测上下文是否丢失,返回 boolean | function | No | Android and iOS | yes |
| isEnabled(cap) | 检测功能是否启用,返回 boolean | function | No | Android and iOS | yes |
| isFramebuffer(framebuffer) | 检测是否为帧缓冲区对象,返回 boolean | function | No | Android and iOS | yes |
| isProgram(program) | 检测是否为着色器程序,返回 boolean | function | No | Android and iOS | yes |
| isRenderbuffer(renderbuffer) | 检测是否为渲染缓冲区对象,返回 boolean | function | No | Android and iOS | yes |
| isShader(shader) | 检测是否为着色器,返回 boolean | function | No | Android and iOS | yes |
| isTexture(texture) | 检测是否为纹理对象,返回 boolean | function | No | Android and iOS | yes |
| lineWidth(width) | 设置线宽 | function | No | Android and iOS | yes |
| pixelStorei(pname, param) | 设置像素存储模式 | function | No | Android and iOS | yes |
| polygonOffset(factor, units) | 设置多边形偏移因子 | function | No | Android and iOS | yes |
| readPixels(x, y, width, height, format, type, pixels) | 读取像素数据 | function | No | Android and iOS | yes |
| renderbufferStorage(target, internalFormat, width, height) | 设置渲染缓冲区存储格式 | function | No | Android and iOS | yes |
| sampleCoverage(value, invert) | 设置多重采样覆盖参数 | function | No | Android and iOS | yes |
| scissor(x, y, width, height) | 设置裁剪矩形 | function | No | Android and iOS | yes |
| stencilFunc(func, ref, mask) | 设置模板测试函数 | function | No | Android and iOS | yes |
| stencilMask(mask) | 设置模板写入掩码 | function | No | Android and iOS | yes |
| stencilOp(fail, zfail, zpass) | 设置模板操作 | function | No | Android and iOS | yes |
| texParameterf(target, pname, param) | 设置纹理浮点参数 | function | No | Android and iOS | yes |
| texParameteri(target, pname, param) | 设置纹理整数参数 | function | No | Android and iOS | yes |
| validateProgram(program) | 验证着色器程序 | function | No | Android and iOS | yes |
| vertexAttribfamily(index, value) | 设置顶点属性常量值(包括 vertexAttrib1f~4f、vertexAttrib1fv~4fv) | function | No | Android and iOS | yes |
对外导出的函数
| Name | Description | Type | Required | Platform | HarmonyOS Support |
|---|---|---|---|---|---|
| createCanvas(width, height) | 创建一个离屏 Canvas 对象,等同于 document.createElement('canvas') | function | No | Android and iOS | yes |
| getFontNames() | 获取当前设备上所有可用字体名称列表 | function | No | Android and iOS | yes |
| registerFont(src, fontFace) | 注册自定义字体(仅 Android) | function | No | Android | no |
5. 遗留问题
6. 其他
7. 开源协议
本项目基于 Apache License 2.0 ,请自由地享受和参与开源。