diff --git a/OAT.xml b/OAT.xml index 3102751180ecb46fa7ad5989803b97c5d341b400..aa4cac139d8e5f475a35a347006c5e84ba58210c 100644 --- a/OAT.xml +++ b/OAT.xml @@ -464,6 +464,14 @@ Note:If the text contains special characters, please escape them according to th + + + + + + + + diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/AppScope/app.json5 b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c50fa4afd3ab02e03fc77b49509070182e322712 --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/AppScope/app.json5 @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +{ + "app": { + "bundleName": "com.samples.tsdrawingsample", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/AppScope/resources/base/element/string.json b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..033d458e0145c84181fd9507c7e3d06f6ae35e71 --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "tsdrawingsample" + } + ] +} diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/AppScope/resources/base/media/app_icon.png b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/AppScope/resources/base/media/app_icon.png differ diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/LICENSE b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..18795a48d6b12fcdc1aa7bac9a9cb99f83815267 --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/LICENSE @@ -0,0 +1,78 @@ + Copyright (c) 2025 Huawei Device Co., Ltd. All rights reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +Apache License, Version 2.0 +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + +"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. + +"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. + +"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. + +"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. + +"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. + +"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. + +"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). + +"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. + +"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." + +"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. + +2. Grant of Copyright License. + +Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. + +Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. + +4. Redistribution. + +You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: +1.You must give any other recipients of the Work or Derivative Works a copy of this License; and +2.You must cause any modified files to carry prominent notices stating that You changed the files; and +3.You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and +4.If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. + +You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. + +5. Submission of Contributions. + +Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. + +6. Trademarks. + +This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. + +Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. + +In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. + +While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS \ No newline at end of file diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/README.md b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/README.md new file mode 100644 index 0000000000000000000000000000000000000000..51dd0f08ff9703b595afb9da56faedde74810be7 --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/README.md @@ -0,0 +1,71 @@ +# 图形绘制与显示的示例(TSDrawingSample) + +#### 介绍 + +本示例使用Drawing进行图形绘制与显示时,使用[@ohos.graphics.drawing](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkgraphics2d/js-apis-graphics-drawing.md)模块的画布画笔绘制基本的2D图形和文字,调用绘制和显示的逻辑,在应用上显示图形和文字。 + +#### 使用说明 + +1. 使用DevEco Studio 5.0.0及以上版本打开此工程,编译运行; + +2. 点击页面底部“画布操作及状态”按钮,页面将绘制与画布裁剪、矩阵变换等相关内容; + +3. 点击页面底部“几何形状绘制”按钮,页面将绘制几何形状(矩形、圆角矩形、椭圆等); + +4. 点击页面底部“路径绘制”按钮,页面将绘制路径(路径填充、路径描边、曲线闭合、曲线未闭合等); + +5. 点击页面底部“图片和字块绘制”按钮,页面将绘制图片和文字等相关内容; + +6. 点击页面底部“复杂绘制效果”按钮,页面将复杂绘制效果等相关内容。 + +#### 效果预览 + +| 主页 | 画布操作及状态 | 几何形状绘制 | 路径绘制 | 图片和字块绘制 | 复杂绘制效果 | +| ------------- |-----------|----------------|------------ |------------ |------------ | +| ![main](screenshots/device/Index.png) | ![Draw Canvas](screenshots/device/Canvas.png) | ![Draw Geometry](screenshots/device/GeometricShape.png) | ![Draw Path](screenshots/device/Path.png) | ![Draw Image and Font](screenshots/device/ImageAndFont.png) | ![Draw Complex](screenshots/device/Complex.png) | + +#### 工程目录 + +``` +├──entry/src/main +│ ├──ets // ets代码区 +│ │ ├──common +│ │ │ └──Logger.ts // 日志封装定义文件 +│ │ │ └──CanvasRenderNode.ets // 画布操作及状态相关的RenderNode +│ │ │ └──ShapeRenderNode.ets // 几何形状绘制相关的RenderNode +│ │ │ └──PathRenderNode.ets // 路径绘制相关的RenderNode +│ │ │ └──ImageRenderNode.ets // 图片绘制相关的RenderNode +│ │ │ └──TextBlobRenderNode.ets // 字块绘制相关的RenderNode +│ │ │ └──ComplexRenderNode.ets // 复杂绘制效果相关的RenderNode +│ │ ├──entryability // 不涉及 +│ │ ├──entrybackupability // 不涉及 +│ │ └──pages // 页面文件 +│ │ └──Index.ets // 主界面 +| ├──resources // 资源文件目录 +``` + +#### 相关权限 + +暂无 + +#### 依赖 + +暂无 + +#### 约束与限制 + +1. 本示例仅支持标准系统上运行,支持设备:Phone; +2. 本示例为Stage模型,已适配API version 14版本SDK,SDK版本号(API Version 14 Release),镜像版本号(5.0.2 Release); +3. 本示例需要使用DevEco Studio 版本号(5.0 Release)及以上版本才可编译运行。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +``` +git init +git config core.sparsecheckout true +echo code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/ > .git/info/sparse-checkout +git remote add origin https://gitee.com/openharmony/applications_app_samples.git +git pull origin master +``` \ No newline at end of file diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/build-profile.json5 b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..009f13d5d53e77518361865b68e4a7f5dd66b3b8 --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/build-profile.json5 @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compileSdkVersion": 14, + "compatibleSdkVersion": 14, + "targetSdkVersion": 14, + "runtimeOS": "OpenHarmony", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "entry", + "srcPath": "./entry", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/code-linter.json5 b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..28586467ee7a761c737d8654a73aed6fddbc3c71 --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/code-linter.json5 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/.gitignore b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/build-profile.json5 b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e7569e3056e27af38e9991b7ea73ec10f3ba8a05 --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/build-profile.json5 @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/hvigorfile.ts b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/obfuscation-rules.txt b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/oh-package.json5 b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c9cb6c8174858277c9b0d465a51547dcab16d5ff --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "entry", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": {} +} + diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/common/CanvasRenderNode.ets b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/common/CanvasRenderNode.ets new file mode 100644 index 0000000000000000000000000000000000000000..237f9d038b5fe64aec853e663f46f7a178959219 --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/common/CanvasRenderNode.ets @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { RenderNode, DrawContext } from '@ohos.arkui.node'; +import drawing from '@ohos.graphics.drawing'; +import common2D from '@ohos.graphics.common2D'; +import display from '@ohos.display'; + +let screen = display.getDefaultDisplaySync(); +let screenWidth: number = screen.width; + +export class CanvasRenderNodeCropping extends RenderNode { + async draw(context: DrawContext) { + const canvas = context.canvas; + let startX = 100.0; + let startY = 100.0; + let w = screenWidth / 8; + let h = screenWidth / 8; + let textSize = screenWidth / 25; + let font = new drawing.Font(); + font.setSize(textSize); + let text = getContext().resourceManager.getStringSync($r('app.string.crop')); + const textBlob = drawing.TextBlob.makeFromString(text, font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(textBlob, startX, startY - textSize); + + // 画布裁剪矩形 交集(INTERSECT) + let brush1 = new drawing.Brush(); + brush1.setColor(0xFF, 0x00, 0x00, 0xFF); + canvas.attachBrush(brush1); + canvas.save(); + let rect1: common2D.Rect = { left: startX, top: startY, right: startX + w, bottom: startY + h }; + canvas.clipRect(rect1); + canvas.drawCircle(startX + w, startY + h, w); + canvas.restore(); + canvas.detachBrush(); + + // 画布裁剪矩形 差集(DIFFERENCE) + let brush2 = new drawing.Brush(); + brush2.setColor(0xFF, 0x00, 0x00, 0xFF); + canvas.attachBrush(brush2); + canvas.save(); + let rect2: common2D.Rect = { left: startX + 2 * w, top: startY, right: startX + 3 * w, bottom: startY + h }; + canvas.clipRect(rect2, drawing.ClipOp.DIFFERENCE); + canvas.drawCircle(startX + 3 * w, startY + h, w); + canvas.restore(); + canvas.drawCircle(startX + 6 * w, startY + h, w); // 原始图 + canvas.detachBrush(); + } +} + +export class CanvasRenderNodeMatrix extends RenderNode { + async draw(context: DrawContext) { + const canvas = context.canvas; + let startX = 100.0; + let startY = 100.0 + screenWidth / 3; + let textSize = screenWidth / 25; + let font = new drawing.Font(); + font.setSize(textSize); + let text2 = getContext().resourceManager.getStringSync($r('app.string.scaling')); + let startBlob = drawing.TextBlob.makeFromString(text2, font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(startBlob, startX, startY + textSize); + // 矩阵变换-缩放 + let pen = new drawing.Pen(); + pen.setColor({alpha:0xFF, red:0xFF, green:0x00, blue:0x00}); + pen.setStrokeWidth(20); + canvas.attachPen(pen); + canvas.save(); + canvas.scale(1.5, 1.5) + canvas.drawCircle(startX + 60, startY + textSize + 100, 60); + canvas.restore(); + canvas.drawCircle(startX + 60, startY + textSize + 100, 60); + canvas.detachPen(); + + let startX2: number = startX + screenWidth / 3; + let text3 = getContext().resourceManager.getStringSync($r('app.string.translation_and_rotation')); + let startBlob2 = drawing.TextBlob.makeFromString(text3, font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(startBlob2, startX2, startY + textSize); + // 矩阵变换-平移和旋转 + canvas.attachPen(pen); + canvas.save(); + canvas.translate(150, 150); // 平移 + canvas.rotate(15, startX + screenWidth / 2, startY + textSize + 100); // 旋转 + canvas.drawRect({left: startX2, top: startY + textSize + 50, + right: startX2 + 200, bottom: startY + textSize + 200}); + canvas.restore(); + canvas.drawRect({left: startX2, top: startY + textSize + 50, + right: startX2 + 200, bottom: startY + textSize + 200}); + canvas.detachPen(); + } +} + +export class CanvasRenderNodeCombination extends RenderNode { + async draw(context: DrawContext) { + const canvas = context.canvas; + let startX = 100.0; + let startY = 100.0+screenWidth; + let w = screenWidth / 8; + let h = screenWidth / 8; + let textSize = screenWidth / 25; + let font = new drawing.Font(); + font.setSize(textSize); + let text4 = getContext().resourceManager.getStringSync($r('app.string.tailor_and_translation')); + let startBlob = drawing.TextBlob.makeFromString(text4, font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(startBlob, startX, startY - textSize); + // 裁剪+矩阵——矩形裁剪+平移 + let brush1 = new drawing.Brush(); + brush1.setColor(0xFF, 0x00, 0x00, 0xFF); + canvas.attachBrush(brush1); + canvas.save(); + let rect1: common2D.Rect = { left: startX, top: startY, right: startX + w, bottom: startY + h }; + canvas.translate(100, 100); // 平移 + canvas.clipRect(rect1); // 裁剪 + canvas.drawCircle(startX + w, startY + h, w); + canvas.restore(); + canvas.drawCircle(startX + 3 * w, startY + h, w); + canvas.detachBrush(); + } +} diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/common/ComplexRenderNode.ets b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/common/ComplexRenderNode.ets new file mode 100644 index 0000000000000000000000000000000000000000..28a8caa6fcc1dc90741896955c39812cb25a173b --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/common/ComplexRenderNode.ets @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { RenderNode, DrawContext } from '@ohos.arkui.node'; +import drawing from '@ohos.graphics.drawing'; +import common2D from '@ohos.graphics.common2D'; +import display from '@ohos.display'; + +let screen = display.getDefaultDisplaySync(); +let screenWidth: number = screen.width; + +export class ComplexRenderNode extends RenderNode { + async draw(context: DrawContext) { + const canvas = context.canvas; + let startX = 100.0; + let startY = 50.0; + let font = new drawing.Font(); + font.setSize(screenWidth / 25); + let text = getContext().resourceManager.getStringSync($r('app.string.ShaderEffect')); + const stringShaderEffect = drawing.TextBlob.makeFromString(text, font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(stringShaderEffect, startX, startY); + // ShaderEffect + let startPt: common2D.Point = { x: 100, y: 100 }; + let endPt: common2D.Point = { x: 400, y: 400 }; + let colors = [0xFFFFFF00, 0xFFFF0000, 0xFF0000FF]; + let shaderEffect = drawing.ShaderEffect.createLinearGradient(startPt, endPt, colors, drawing.TileMode.CLAMP); + let brush = new drawing.Brush(); + brush.setShaderEffect(shaderEffect); + canvas.attachBrush(brush); + let rect1: common2D.Rect = { left: 100, top: 100, right: 400, bottom: 400 }; + canvas.drawRect(rect1); + canvas.detachBrush(); + + let text2 = getContext().resourceManager.getStringSync($r('app.string.ImageFilter')); + const stringImageFilter = drawing.TextBlob.makeFromString(text2, font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(stringImageFilter, startX + 600, startY); + // ImageFilter + let pen = new drawing.Pen(); + pen.setStrokeWidth(10.0); + pen.setColor(0xFF, 0xFF, 0x00, 0x00); + let filter = drawing.ImageFilter.createBlurImageFilter(20, 20, drawing.TileMode.CLAMP); + pen.setImageFilter(filter); + canvas.attachPen(pen); + let rect2: common2D.Rect = { left: 700, top: 100, right: 1000, bottom: 400 }; + canvas.drawRect(rect2); + canvas.detachPen(); + + let text3 = getContext().resourceManager.getStringSync($r('app.string.PathEffect')); + const stringPathEffect = drawing.TextBlob.makeFromString(text3, font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(stringPathEffect, startX, startY + 600); + let iPen = new drawing.Pen(); + iPen.setStrokeWidth(10.0); + let iPenColor: common2D.Color = { + alpha: 0xFF, + red: 0xFF, + green: 0x00, + blue: 0x00 + }; + iPen.setColor(iPenColor); + canvas.attachPen(iPen); + let intervals = [20.0, 10.0]; + let pathEffect = drawing.PathEffect.createDashPathEffect(intervals, 0); + iPen.setPathEffect(pathEffect); + canvas.attachPen(iPen); + canvas.drawLine(100, 700, 400, 1000); + canvas.detachPen(); + } +} diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/common/ImageRenderNode.ets b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/common/ImageRenderNode.ets new file mode 100644 index 0000000000000000000000000000000000000000..e228a6970050d98467d4042c7fc2a564680c2b12 --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/common/ImageRenderNode.ets @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { RenderNode, DrawContext } from '@ohos.arkui.node'; +import drawing from '@ohos.graphics.drawing'; +import common2D from '@ohos.graphics.common2D'; +import image from '@ohos.multimedia.image'; +import display from '@ohos.display'; + +let screen = display.getDefaultDisplaySync(); +let screenWidth: number = screen.width; + +export class ImageRenderNode extends RenderNode { + private pMap: image.PixelMap | undefined = undefined; + + async draw(context: DrawContext) { + const canvas = context.canvas; + let font = new drawing.Font(); + font.setSize(23); + // DrawImage + let text = getContext().resourceManager.getStringSync($r('app.string.DrawImage')); + const textBlob1 = drawing.TextBlob.makeFromString(text, font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(textBlob1, 20, 300); + canvas.drawImage(this.pMap, 60, 320); + + // DrawPixelMapMesh + let text2 = getContext().resourceManager.getStringSync($r('app.string.DrawPixelMapMesh')); + const textBlob2 = drawing.TextBlob.makeFromString(text2, + font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(textBlob2, 320, 300); + const brush = new drawing.Brush(); + canvas.attachBrush(brush); + let verts: number[] = + [320, 320, 390, 320, 460, 320, 320, 380, 390, 380, 460, 380, 320, 450, 390, 450, 460, 450]; + canvas.drawPixelMapMesh(this.pMap, 2, 2, verts, 0, null, 0); + canvas.detachBrush(); + } + + setPixelMap(p: PixelMap) { + this.pMap = p; + } +} + +export class ImageRectRenderNode extends RenderNode { + private pMap: image.PixelMap | undefined = undefined; + + async draw(context: DrawContext) { + const canvas = context.canvas; + // DrawImageRectWithSrc + let font = new drawing.Font(); + font.setSize(23); // 23 is font size; + let fontTitle = new drawing.Font(); + fontTitle.setSize(screenWidth / 25); + + let text3 = getContext().resourceManager.getStringSync($r('app.string.DrawPicture')); + const stringImage = drawing.TextBlob.makeFromString(text3, + fontTitle, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(stringImage, screenWidth / 3, 50.0); + let text4 = getContext().resourceManager.getStringSync($r('app.string.DrawImageRectWithSrc')); + const textBlob1 = drawing.TextBlob.makeFromString(text4, + font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(textBlob1, 20, 80.0); + canvas.detachBrush(); + + // DrawImageRectWithSrc + let srcRect: common2D.Rect = { + left: 0, + top: 0, + right: 140, + bottom: 140 + }; + let dstRect: common2D.Rect = { + left: 60, + top: 100, + right: 200, + bottom: 240 + }; + canvas.drawImageRectWithSrc(this.pMap, srcRect, dstRect); + + // DrawImageRect + let font1 = new drawing.Font(); + font1.setSize(23); // 23 is font size; + let text5 = getContext().resourceManager.getStringSync($r('app.string.DrawImageRect')); + const textBlob2 = drawing.TextBlob.makeFromString(text5, font1, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(textBlob2, 320, 80.0); + canvas.detachBrush(); + let rect: common2D.Rect = { + left: 330, + top: 100, + right: 470, + bottom: 240 + }; + canvas.drawImageRect(this.pMap, rect); + } + + setPixelMap(p: PixelMap) { + this.pMap = p; + } +} \ No newline at end of file diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/common/Logger.ts b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/common/Logger.ts new file mode 100644 index 0000000000000000000000000000000000000000..77aed43054d22feec9b7348b43323e0727b3e9af --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/common/Logger.ts @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import hilog from '@ohos.hilog'; + +class Logger { + private domain: number + private prefix: string + private format: string = '%{public}s, %{public}s' + + constructor(prefix: string) { + this.prefix = prefix + this.domain = 0x0001 + } + + debug(...args: string[]) { + hilog.debug(this.domain, this.prefix, this.format, args) + } + + info(...args: string[]) { + hilog.info(this.domain, this.prefix, this.format, args) + } + + warn(...args: string[]) { + hilog.warn(this.domain, this.prefix, this.format, args) + } + + error(...args: string[]) { + hilog.error(this.domain, this.prefix, this.format, args) + } + + fatal(...args: string[]) { + hilog.fatal(this.domain, this.prefix, this.format, args) + } + + isLoggable(level: number) { + hilog.isLoggable(this.domain, this.prefix, level) + } +} + +export default new Logger('[Sample_ScreenRecorder]') diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/common/MyRenderNodePath.ets b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/common/MyRenderNodePath.ets new file mode 100644 index 0000000000000000000000000000000000000000..585443345e37e0ec5054d1a3311973259dca1fd3 --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/common/MyRenderNodePath.ets @@ -0,0 +1,303 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { RenderNode, DrawContext } from '@ohos.arkui.node'; +import drawing from '@ohos.graphics.drawing'; +import common2D from '@ohos.graphics.common2D'; +import display from '@ohos.display'; + +let screen = display.getDefaultDisplaySync(); +let screenWidth: number = screen.width; + +export class MyRenderNodePathBrush extends RenderNode { + async draw(context: DrawContext) { + // title + const canvas = context.canvas; + canvas.detachBrush(); + const startX = 200; + const startY = 60; + const dis = 1000; + const w = screenWidth / 6; + const font = new drawing.Font(); + font.setSize(screenWidth / 25); + let text = getContext().resourceManager.getStringSync($r('app.string.DrawPathWithBrush')); + const textBlob = + drawing.TextBlob.makeFromString(text, font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(textBlob, startX, startY + dis); + // set brush + const brush = new drawing.Brush(); + const color: common2D.Color = { + alpha: 0xFF, + red: 0x44, + green: 0x72, + blue: 0xC3 + }; + brush.setColor(color); + canvas.attachBrush(brush); + // rect + const pathRect = new drawing.Path(); + const rect: common2D.Rect = { + left: w + 10, + top: 100 + dis, + right: w * 2 - 10, + bottom: 160 + dis + }; + pathRect.addRect(rect); + canvas.drawPath(pathRect); + // oval + const pathOval = new drawing.Path(); + const rectOval: common2D.Rect = { + left: w * 2 + 10, + top: 100 + dis, + right: w * 3 - 10, + bottom: 160 + dis + }; + pathOval.addOval(rectOval, 0); + canvas.drawPath(pathOval); + // roundrect + const pathRoundRect = new drawing.Path(); + const rectRoundRect: common2D.Rect = { + left: w * 3 + 10, + top: 100 + dis, + right: w * 4 - 10, + bottom: 160 + dis + }; + const roundRect = new drawing.RoundRect(rectRoundRect, 20, 20); + pathRoundRect.addRoundRect(roundRect); + canvas.drawPath(pathRoundRect); + // polygon + const pathPolygon = new drawing.Path(); + let points: common2D.Point[] = []; + const point1: common2D.Point = { x: w * 4 + 10, y: 160 + dis }; + const point2: common2D.Point = { x: w * 5 - 10, y: 160 + dis }; + const point3: common2D.Point = { x: w * 9 / 2, y: 100 + dis }; + points.push(point1); + points.push(point2); + points.push(point3); + pathPolygon.addPolygon(points, true); + canvas.drawPath(pathPolygon); + // arc + const pathArc = new drawing.Path(); + const rectArc: common2D.Rect = { + left: w * 5 - 50, + top: 100 + dis, + right: w * 6 - 10, + bottom: 220 + dis + }; + pathArc.addArc(rectArc, 0, -90); + canvas.drawPath(pathArc); + // detach + canvas.detachBrush(); + } +} + +export class MyRenderNodePathPen extends RenderNode { + async draw(context: DrawContext) { + // title + const canvas = context.canvas; + canvas.detachBrush(); + const startX = 200; + const startY = 230; + const dis = 1000; + const w = screenWidth / 6; + const font = new drawing.Font(); + font.setSize(screenWidth / 25); + let text1 = getContext().resourceManager.getStringSync($r('app.string.DrawPathWithPen')); + const textBlob = drawing.TextBlob.makeFromString(text1, + font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(textBlob, startX, startY + dis); + // set pen + const pen = new drawing.Pen(); + const color: common2D.Color = { + alpha: 0xFF, + red: 0x44, + green: 0x72, + blue: 0xC3 + }; + pen.setColor(color); + pen.setAntiAlias(true); + pen.setStrokeWidth(3); + canvas.attachPen(pen); + // lineto + const pathLine = new drawing.Path(); + pathLine.moveTo(20, 290 + dis); + pathLine.lineTo(w - 20, 290 + dis); + canvas.drawPath(pathLine); + // rect + const pathRect = new drawing.Path(); + const rect: common2D.Rect = { + left: w + 10, + top: 260 + dis, + right: w * 2 - 10, + bottom: 320 + dis + }; + pathRect.addRect(rect); + canvas.drawPath(pathRect); + // oval + const pathOval = new drawing.Path(); + const rectOval: common2D.Rect = { + left: w * 2 + 10, + top: 260 + dis, + right: w * 3 - 10, + bottom: 320 + dis + }; + pathOval.addOval(rectOval, 0); + canvas.drawPath(pathOval); + // roundRect + const pathRoundRect = new drawing.Path(); + const rectRoundRect: common2D.Rect = { + left: w * 3 + 10, + top: 260 + dis, + right: w * 4 - 10, + bottom: 320 + dis + }; + const roundRect = new drawing.RoundRect(rectRoundRect, 20, 20); + pathRoundRect.addRoundRect(roundRect); + canvas.drawPath(pathRoundRect); + // polygon + const pathPolygon = new drawing.Path(); + let points: common2D.Point[] = []; + const point1: common2D.Point = { x: w * 4 + 10, y: 320 + dis }; + const point2: common2D.Point = { x: w * 5 - 10, y: 320 + dis }; + const point3: common2D.Point = { x: w * 9 / 2, y: 260 + dis }; + points.push(point1); + points.push(point2); + points.push(point3); + pathPolygon.addPolygon(points, true); + canvas.drawPath(pathPolygon); + // arc + const pathArc = new drawing.Path(); + const rectArc: common2D.Rect = { + left: w * 5 - 50, + top: 260 + dis, + right: w * 6 - 10, + bottom: 380 + dis + }; + pathArc.addArc(rectArc, 0, -90); + canvas.drawPath(pathArc); + // detach + canvas.detachPen(); + } +} + +export class MyRenderNodePathShader extends RenderNode { + async draw(context: DrawContext) { + // title + const canvas = context.canvas; + canvas.detachBrush(); + const startX = 200; + const startY = 380; + const dis = 1000; + const w = screenWidth / 6; + const font = new drawing.Font(); + font.setSize(screenWidth / 25); + let text2 = getContext().resourceManager.getStringSync($r('app.string.DrawPathWithShader')); + const textBlob = drawing.TextBlob.makeFromString(text2, + font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(textBlob, startX, startY + dis); + // rect + const brushRect = new drawing.Brush(); + const startPt1: common2D.Point = { x: w * 3 / 2, y: 410 + dis }; + const endPt1: common2D.Point = { x: w * 3 / 2, y: 470 + dis }; + const shaderEffectRect = + drawing.ShaderEffect.createLinearGradient(startPt1, endPt1, [0xFFF8FA12, 0xFFACC3DD], drawing.TileMode.CLAMP); + brushRect.setShaderEffect(shaderEffectRect); + canvas.attachBrush(brushRect); + const pathRect = new drawing.Path(); + const rect: common2D.Rect = { + left: w + 10, + top: 410 + dis, + right: w * 2 - 10, + bottom: 470 + dis + }; + pathRect.addRect(rect); + canvas.drawPath(pathRect); + canvas.detachBrush(); + // oval + const brushOval = new drawing.Brush(); + const startPt2: common2D.Point = { x: w * 5 / 2, y: 410 + dis }; + const endPt2: common2D.Point = { x: w * 5 / 2, y: 470 + dis }; + const shaderEffectOval = + drawing.ShaderEffect.createLinearGradient(startPt2, endPt2, [0xFFF8FA12, 0xFFACC3DD], drawing.TileMode.CLAMP); + brushOval.setShaderEffect(shaderEffectOval); + canvas.attachBrush(brushOval); + const pathOval = new drawing.Path(); + const rectOval: common2D.Rect = { + left: w * 2 + 10, + top: 410 + dis, + right: w * 3 - 10, + bottom: 470 + dis + }; + pathOval.addOval(rectOval, 0); + canvas.drawPath(pathOval); + canvas.detachBrush(); + // roundrect + const brushRoundRect = new drawing.Brush(); + const startPt3: common2D.Point = { x: w * 7 / 2, y: 410 + dis }; + const endPt3: common2D.Point = { x: w * 7 / 2, y: 470 + dis }; + const shaderEffectRoundRect = + drawing.ShaderEffect.createLinearGradient(startPt3, endPt3, [0xFFF8FA12, 0xFFACC3DD], drawing.TileMode.CLAMP); + brushRoundRect.setShaderEffect(shaderEffectRoundRect); + canvas.attachBrush(brushRoundRect); + const pathRoundRect = new drawing.Path(); + const rectRoundRect: common2D.Rect = { + left: w * 3 + 10, + top: 410 + dis, + right: w * 4 - 10, + bottom: 470 + dis + }; + const roundRect = new drawing.RoundRect(rectRoundRect, 20, 20 + dis); + pathRoundRect.addRoundRect(roundRect); + canvas.drawPath(pathRoundRect); + canvas.detachBrush(); + // polygon + const brushPolygon = new drawing.Brush(); + const startPt4: common2D.Point = { x: w * 9 / 2, y: 410 + dis }; + const endPt4: common2D.Point = { x: w * 9 / 2, y: 470 + dis }; + const shaderEffectPolygon = + drawing.ShaderEffect.createLinearGradient(startPt4, endPt4, [0xFFF8FA12, 0xFFACC3DD], drawing.TileMode.CLAMP); + brushPolygon.setShaderEffect(shaderEffectPolygon); + canvas.attachBrush(brushPolygon); + const pathPolygon = new drawing.Path(); + let points: common2D.Point[] = []; + const point1: common2D.Point = { x: w * 4 + 10, y: 470 + dis }; + const point2: common2D.Point = { x: w * 5 - 10, y: 470 + dis }; + const point3: common2D.Point = { x: w * 9 / 2, y: 410 + dis }; + points.push(point1); + points.push(point2); + points.push(point3); + pathPolygon.addPolygon(points, true); + canvas.drawPath(pathPolygon); + canvas.detachBrush(); + // arc + const brushArc = new drawing.Brush(); + const startPt5: common2D.Point = { x: w * 11 / 2, y: 410 + dis }; + const endPt5: common2D.Point = { x: w * 6 - 10, y: 470 + dis }; + const shaderEffectArc = + drawing.ShaderEffect.createLinearGradient(startPt5, endPt5, [0xFFF8FA12, 0xFFACC3DD], drawing.TileMode.CLAMP); + brushArc.setShaderEffect(shaderEffectArc); + canvas.attachBrush(brushArc); + const pathArc = new drawing.Path(); + const rectArc: common2D.Rect = { + left: w * 5 - 50, + top: 410 + dis, + right: w * 6 - 10, + bottom: 530 + dis + }; + pathArc.addArc(rectArc, 0, -90); + canvas.drawPath(pathArc); + canvas.detachBrush(); + } +} \ No newline at end of file diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/common/PathRenderNode.ets b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/common/PathRenderNode.ets new file mode 100644 index 0000000000000000000000000000000000000000..12b82c579b08bf4ac41e21464b76c45507c24b0a --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/common/PathRenderNode.ets @@ -0,0 +1,566 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { RenderNode, DrawContext } from '@ohos.arkui.node'; +import drawing from '@ohos.graphics.drawing'; +import common2D from '@ohos.graphics.common2D'; +import display from '@ohos.display'; + +let screen = display.getDefaultDisplaySync(); +let screenWidth: number = screen.width; + +export class PathRenderNode extends RenderNode { + async draw(context: DrawContext) { + const canvas = context.canvas; + let startX = 80.0; + let startY = 50.0; + let font = new drawing.Font(); + font.setSize(screenWidth / 20); + let text = getContext().resourceManager.getStringSync($r('app.string.PathWithBrush')); + const textBlob = drawing.TextBlob.makeFromString(text, font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(textBlob, startX, startY); + // 路径填充 + let brush = new drawing.Brush(); + let brush_color: common2D.Color = { + alpha: 0xFF, + red: 0x00, + green: 0x00, + blue: 0xFF + }; + brush.setColor(brush_color); + canvas.attachBrush(brush); + let len = 120.0; + let aX = 150.0; + let aY = 80.0; + let dX = aX - len * Math.sin(18.0); + let dY = aY + len * Math.cos(18.0); + let cX = aX + len * Math.sin(18.0); + let cY = dY; + let bX = aX + (len / 2.0); + let bY = aY + Math.sqrt((cX - dX) * (cX - dX) + (len / 2.0) * (len / 2.0)); + let eX = aX - (len / 2.0); + let eY = bY; + let cPath = new drawing.Path(); + cPath.moveTo(aX, aY); + cPath.lineTo(bX, bY); + cPath.lineTo(cX, cY); + cPath.lineTo(dX, dY); + cPath.lineTo(eX, eY); + cPath.close(); + canvas.drawPath(cPath); + canvas.detachBrush(); + + let startX2 = 380.0; + let startY2 = 50.0; + let font2 = new drawing.Font(); + font2.setSize(screenWidth / 20); + let text1 = getContext().resourceManager.getStringSync($r('app.string.PathWithPen')); + const textBlob2 = drawing.TextBlob.makeFromString(text1, font2, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(textBlob2, startX2, startY2); + // 路径描边 + let cPen = new drawing.Pen(); + cPen.setAntiAlias(true); + let cPenColor: common2D.Color = { + alpha: 0xFF, + red: 0x00, + green: 0x00, + blue: 0xFF + }; + cPen.setColor(cPenColor); + cPen.setStrokeWidth(10.0); + canvas.attachPen(cPen); + let cPath2 = new drawing.Path(); + const centerX = 450.0; + const centerY = 180.0; + const outerRadius = 80; // 外圆半径 + const innerRadius = 30; // 内圆半径 + const points = 5; // 五角星的角数 + for (let i = 0; i < points * 2; i++) { + const angle = (i * Math.PI) / points; // 计算角度 + const radius = i % 2 === 0 ? outerRadius : innerRadius; // 交替使用外圆半径和内圆半径 + const x = centerX + radius * Math.cos(angle + 60.0); + const y = centerY + radius * Math.sin(angle + 60.0); + if (i === 0) { + cPath2.moveTo(x, y); + } else { + cPath2.lineTo(x, y); + } + } + cPath2.close(); + canvas.drawPath(cPath2); + canvas.detachPen(); + + let startX3 = 80.0; + let startY3 = 330.0; + let font3 = new drawing.Font(); + font3.setSize(screenWidth / 20); + let text2 = getContext().resourceManager.getStringSync($r('app.string.CurveClose')); + const textBlob3 = drawing.TextBlob.makeFromString(text2, font3, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(textBlob3, startX3, startY3); + // 曲线闭合 + let cPath3 = new drawing.Path(); + cPath3.moveTo(100.0, 380.0); + cPath3.rCubicTo(200.0, 200.0, 200.0, 100.0, 200.0, 50.0); + cPath3.close(); + let cPen2 = new drawing.Pen(); + cPen2.setAntiAlias(true); + let cPen2_color: common2D.Color = { + alpha: 0xFF, + red: 0x00, + green: 0x00, + blue: 0xFF + }; + cPen2.setColor(cPen2_color); + cPen2.setStrokeWidth(8.0); + canvas.attachPen(cPen2); + let qBrush = new drawing.Brush(); + let qBrush_color: common2D.Color = { + alpha: 0xFF, + red: 0x00, + green: 0xFF, + blue: 0xFF + }; + qBrush.setColor(qBrush_color); + canvas.attachBrush(qBrush); + canvas.drawPath(cPath3); + canvas.detachPen(); + canvas.detachBrush(); + + let startX4 = 350.0; + let startY4 = 330.0; + let font4 = new drawing.Font(); + font4.setSize(screenWidth / 20); + let text3 = getContext().resourceManager.getStringSync($r('app.string.CurveNotClose')); + const textBlob4 = drawing.TextBlob.makeFromString(text3, font4, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(textBlob4, startX4, startY4); + // 曲线未闭合 + let cPen3 = new drawing.Pen(); + let cPen3_color: common2D.Color = { + alpha: 0xFF, + red: 0x00, + green: 0x00, + blue: 0xFF + }; + cPen3.setColor(cPen3_color); + cPen3.setStrokeWidth(8.0); + canvas.attachPen(cPen3); + let path2 = new drawing.Path(); + path2.moveTo(400.0, 350.0); + path2.cubicTo(400.0, 420.0, 550.0, 380.0, 450.0, 500.0); + canvas.drawPath(path2); + canvas.detachPen(); + + let startX5 = 320.0; + let startY5 = 550.0; + let font5 = new drawing.Font(); + font5.setSize(screenWidth / 20); + let text4 = getContext().resourceManager.getStringSync($r('app.string.ComplexPath')); + const textBlob5 = drawing.TextBlob.makeFromString(text4, font5, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(textBlob5, startX5, startY5); + + // 复杂路径 + // 小猫的脸 + let pen = new drawing.Pen(); + let penColor: common2D.Color = { + alpha: 0xFF, + red: 0xFF, + green: 0xCC, + blue: 0x00 + }; + pen.setColor(penColor); + pen.setStrokeWidth(10.0); + canvas.attachPen(pen); + let path = new drawing.Path(); + path.addCircle(380.0, 750.0, 100.0, drawing.PathDirection.CLOCKWISE); + canvas.drawPath(path); + canvas.detachPen(); + + // 小猫耳朵 + let earX1 = 280.0; + let earY1 = 560.0; + + let ePath = new drawing.Path(); + ePath.moveTo(earX1, earY1); + ePath.lineTo(earX1 + 10.0, earY1 + 150.0); + ePath.moveTo(earX1, earY1); + ePath.lineTo(earX1 + 80.0, earY1 + 90.0); + canvas.attachPen(pen); + canvas.drawPath(ePath); + + let earX2 = 480.0; + let earY2 = 550.0; + let ePath2 = new drawing.Path(); + ePath2.moveTo(earX2, earY2); + ePath2.lineTo(earX2 - 80.0, earY2 + 100.0); + ePath2.moveTo(earX2, earY2); + ePath2.lineTo(earX2 - 10.0, earY2 + 150.0); + canvas.drawPath(ePath2); + + // 小猫胡须 + let clen = 150.0; + let musLX1 = 330.0; + let musLY1 = 750.0; + let mLPath = new drawing.Path(); + mLPath.moveTo(musLX1, musLY1); + mLPath.lineTo(musLX1 - clen, musLY1 - 20.0); + canvas.drawPath(mLPath); + + let musLX2 = 330.0; + let musLY2 = 770.0; + let mLPath2 = new drawing.Path(); + mLPath2.moveTo(musLX2, musLY2); + mLPath2.lineTo(musLX2 - clen, musLY2); + canvas.drawPath(mLPath2); + + let musLX3 = 330.0; + let musLY3 = 790.0; + let mLPath3 = new drawing.Path(); + mLPath3.moveTo(musLX3, musLY3); + mLPath3.lineTo(musLX3 - clen, musLY3 + 20.0); + canvas.drawPath(mLPath3); + + let musRX1 = 420.0; + let musRY1 = 750.0; + let mRPath = new drawing.Path(); + mRPath.moveTo(musRX1, musRY1); + mRPath.lineTo(musRX1 + clen, musRY1 - 20.0); + canvas.drawPath(mRPath); + + let musRX2 = 420.0; + let musRY2 = 770.0; + let mRPath2 = new drawing.Path(); + mRPath2.moveTo(musRX2, musRY2); + mRPath2.lineTo(musRX2 + clen, musRY2); + canvas.drawPath(mRPath2); + + let musRX3 = 420.0; + let musRY3 = 790.0; + let mRPath3 = new drawing.Path(); + mRPath3.moveTo(musRX3, musRY3); + mRPath3.lineTo(musRX3 + clen, musRY3 + 20.0); + canvas.drawPath(mRPath3); + canvas.detachPen(); + + // 小猫眼睛 + let ePen = new drawing.Pen(); + let ePenColor: common2D.Color = { + alpha: 0xFF, + red: 0xFF, + green: 0xCC, + blue: 0x00 + }; + ePen.setColor(ePenColor); + ePen.setStrokeWidth(8.0); + canvas.attachPen(ePen); + let ovalPath = new drawing.Path(); + let rect: common2D.Rect = { + left: 320.0, + top: 700.0, + right: 360.0, + bottom: 720.0 + }; + ovalPath.addArc(rect, 180, 360); + ovalPath.addArc(rect, 0, 180); + canvas.attachPen(ePen); + canvas.drawPath(ovalPath); + let rect2: common2D.Rect = { + left: 400.0, + top: 700.0, + right: 440.0, + bottom: 720.0 + }; + ovalPath.addOval(rect2, 5, drawing.PathDirection.COUNTER_CLOCKWISE); + canvas.drawPath(ovalPath); + canvas.detachPen(); + } +} + +export class MyRenderNodePathBrush extends RenderNode { + async draw(context: DrawContext) { + // title + const canvas = context.canvas; + canvas.detachBrush(); + const startX = 200; + const startY = 60; + const w = screenWidth / 6; + const font = new drawing.Font(); + font.setSize(30); // 30 font size + let text5 = getContext().resourceManager.getStringSync($r('app.string.DrawPathWithBrush')); + const textBlob = + drawing.TextBlob.makeFromString(text5, font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(textBlob, startX, startY); + // set brush + const brush = new drawing.Brush(); + const color: common2D.Color = { + alpha: 0xFF, + red: 0x44, + green: 0x72, + blue: 0xC3 + }; + brush.setColor(color); + canvas.attachBrush(brush); + // rect + const pathRect = new drawing.Path(); + const rect: common2D.Rect = { + left: w + 10, + top: 100, + right: w * 2 - 10, + bottom: 160 + }; + pathRect.addRect(rect); + canvas.drawPath(pathRect); + // oval + const pathOval = new drawing.Path(); + const rectOval: common2D.Rect = { + left: w * 2 + 10, + top: 100, + right: w * 3 - 10, + bottom: 160 + }; + pathOval.addOval(rectOval, 0); + canvas.drawPath(pathOval); + // roundrect + const pathRoundRect = new drawing.Path(); + const rectRoundRect: common2D.Rect = { + left: w * 3 + 10, + top: 100, + right: w * 4 - 10, + bottom: 160 + }; + const roundRect = new drawing.RoundRect(rectRoundRect, 20, 20); + pathRoundRect.addRoundRect(roundRect); + canvas.drawPath(pathRoundRect); + // polygon + const pathPolygon = new drawing.Path(); + let points: common2D.Point[] = []; + const point1: common2D.Point = { x: w * 4 + 10, y: 160 }; + const point2: common2D.Point = { x: w * 5 - 10, y: 160 }; + const point3: common2D.Point = { x: w * 9 / 2, y: 100 }; + points.push(point1); + points.push(point2); + points.push(point3); + pathPolygon.addPolygon(points, true); + canvas.drawPath(pathPolygon); + // arc + const pathArc = new drawing.Path(); + const rectArc: common2D.Rect = { + left: w * 5 - 50, + top: 100, + right: w * 6 - 10, + bottom: 220 + }; + pathArc.addArc(rectArc, 0, -90); + canvas.drawPath(pathArc); + // detach + canvas.detachBrush(); + } +} + +export class MyRenderNodePathPen extends RenderNode { + async draw(context: DrawContext) { + // title + const canvas = context.canvas; + canvas.detachBrush(); + const startX = 200; + const startY = 230; + const w = screenWidth / 6; + const font = new drawing.Font(); + font.setSize(30); // 30 font size + let text6 = getContext().resourceManager.getStringSync($r('app.string.DrawPathWithPen')); + const textBlob = drawing.TextBlob.makeFromString(text6, + font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(textBlob, startX, startY); + // set pen + const pen = new drawing.Pen(); + const color: common2D.Color = { + alpha: 0xFF, + red: 0x44, + green: 0x72, + blue: 0xC3 + }; + pen.setColor(color); + pen.setAntiAlias(true); + pen.setStrokeWidth(3); + canvas.attachPen(pen); + // lineto + const pathLine = new drawing.Path(); + pathLine.moveTo(20, 290); + pathLine.lineTo(w - 20, 290); + canvas.drawPath(pathLine); + // rect + const pathRect = new drawing.Path(); + const rect: common2D.Rect = { + left: w + 10, + top: 260, + right: w * 2 - 10, + bottom: 320 + }; + pathRect.addRect(rect); + canvas.drawPath(pathRect); + // oval + const pathOval = new drawing.Path(); + const rectOval: common2D.Rect = { + left: w * 2 + 10, + top: 260, + right: w * 3 - 10, + bottom: 320 + }; + pathOval.addOval(rectOval, 0); + canvas.drawPath(pathOval); + // roundrect + const pathRoundRect = new drawing.Path(); + const rectRoundRect: common2D.Rect = { + left: w * 3 + 10, + top: 260, + right: w * 4 - 10, + bottom: 320 + }; + const roundRect = new drawing.RoundRect(rectRoundRect, 20, 20); + pathRoundRect.addRoundRect(roundRect); + canvas.drawPath(pathRoundRect); + // polygon + const pathPolygon = new drawing.Path(); + let points: common2D.Point[] = []; + const point1: common2D.Point = { x: w * 4 + 10, y: 320 }; + const point2: common2D.Point = { x: w * 5 - 10, y: 320 }; + const point3: common2D.Point = { x: w * 9 / 2, y: 260 }; + points.push(point1); + points.push(point2); + points.push(point3); + pathPolygon.addPolygon(points, true); + canvas.drawPath(pathPolygon); + // arc + const pathArc = new drawing.Path(); + const rectArc: common2D.Rect = { + left: w * 5 - 50, + top: 260, + right: w * 6 - 10, + bottom: 380 + }; + pathArc.addArc(rectArc, 0, -90); + canvas.drawPath(pathArc); + // detach + canvas.detachPen(); + } +} + +export class MyRenderNodePathShade extends RenderNode { + async draw(context: DrawContext) { + // title + const canvas = context.canvas; + canvas.detachBrush(); + const startX = 200; + const startY = 380; + const w = screenWidth / 6; + const font = new drawing.Font(); + font.setSize(30); // 30 font size + let text7 = getContext().resourceManager.getStringSync($r('app.string.DrawPathWithShader')); + const textBlob = drawing.TextBlob.makeFromString(text7, + font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(textBlob, startX, startY); + // rect + const brushRect = new drawing.Brush(); + const startPt1: common2D.Point = { x: w * 3 / 2, y: 410 }; + const endPt1: common2D.Point = { x: w * 3 / 2, y: 470 }; + const shaderEffectRect = + drawing.ShaderEffect.createLinearGradient(startPt1, endPt1, [0xFFF8FA12, 0xFFACC3DD], drawing.TileMode.CLAMP); + brushRect.setShaderEffect(shaderEffectRect); + canvas.attachBrush(brushRect); + const pathRect = new drawing.Path(); + const rect: common2D.Rect = { + left: w + 10, + top: 410, + right: w * 2 - 10, + bottom: 470 + }; + pathRect.addRect(rect); + canvas.drawPath(pathRect); + canvas.detachBrush(); + // oval + const brushOval = new drawing.Brush(); + const startPt2: common2D.Point = { x: w * 5 / 2, y: 410 }; + const endPt2: common2D.Point = { x: w * 5 / 2, y: 470 }; + const shaderEffectOval = + drawing.ShaderEffect.createLinearGradient(startPt2, endPt2, [0xFFF8FA12, 0xFFACC3DD], drawing.TileMode.CLAMP); + brushOval.setShaderEffect(shaderEffectOval); + canvas.attachBrush(brushOval); + const pathOval = new drawing.Path(); + const rectOval: common2D.Rect = { + left: w * 2 + 10, + top: 410, + right: w * 3 - 10, + bottom: 470 + }; + pathOval.addOval(rectOval, 0); + canvas.drawPath(pathOval); + canvas.detachBrush(); + // roundrect + const brushRoundRect = new drawing.Brush(); + const startPt3: common2D.Point = { x: w * 7 / 2, y: 410 }; + const endPt3: common2D.Point = { x: w * 7 / 2, y: 470 }; + const shaderEffectRoundRect = + drawing.ShaderEffect.createLinearGradient(startPt3, endPt3, [0xFFF8FA12, 0xFFACC3DD], drawing.TileMode.CLAMP); + brushRoundRect.setShaderEffect(shaderEffectRoundRect); + canvas.attachBrush(brushRoundRect); + const pathRoundRect = new drawing.Path(); + const rectRoundRect: common2D.Rect = { + left: w * 3 + 10, + top: 410, + right: w * 4 - 10, + bottom: 470 + }; + const roundRect = new drawing.RoundRect(rectRoundRect, 20, 20); + pathRoundRect.addRoundRect(roundRect); + canvas.drawPath(pathRoundRect); + canvas.detachBrush(); + // polygon + const brushPolygon = new drawing.Brush(); + const startPt4: common2D.Point = { x: w * 9 / 2, y: 410 }; + const endPt4: common2D.Point = { x: w * 9 / 2, y: 470 }; + const shaderEffectPolygon = + drawing.ShaderEffect.createLinearGradient(startPt4, endPt4, [0xFFF8FA12, 0xFFACC3DD], drawing.TileMode.CLAMP); + brushPolygon.setShaderEffect(shaderEffectPolygon); + canvas.attachBrush(brushPolygon); + const pathPolygon = new drawing.Path(); + let points: common2D.Point[] = []; + const point1: common2D.Point = { x: w * 4 + 10, y: 470 }; + const point2: common2D.Point = { x: w * 5 - 10, y: 470 }; + const point3: common2D.Point = { x: w * 9 / 2, y: 410 }; + points.push(point1); + points.push(point2); + points.push(point3); + pathPolygon.addPolygon(points, true); + canvas.drawPath(pathPolygon); + canvas.detachBrush(); + // arc + const brushArc = new drawing.Brush(); + const startPt5: common2D.Point = { x: w * 11 / 2, y: 410 }; + const endPt5: common2D.Point = { x: w * 6 - 10, y: 470 }; + const shaderEffectArc = + drawing.ShaderEffect.createLinearGradient(startPt5, endPt5, [0xFFF8FA12, 0xFFACC3DD], drawing.TileMode.CLAMP); + brushArc.setShaderEffect(shaderEffectArc); + canvas.attachBrush(brushArc); + const pathArc = new drawing.Path(); + const rectArc: common2D.Rect = { + left: w * 5 - 50, + top: 410, + right: w * 6 - 10, + bottom: 530 + }; + pathArc.addArc(rectArc, 0, -90); + canvas.drawPath(pathArc); + canvas.detachBrush(); + } +} diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/common/ShapeRenderNode.ets b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/common/ShapeRenderNode.ets new file mode 100644 index 0000000000000000000000000000000000000000..7ec06a7719badd36f3e1f08780c1129346fe5717 --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/common/ShapeRenderNode.ets @@ -0,0 +1,384 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { RenderNode, DrawContext } from '@ohos.arkui.node'; +import drawing from '@ohos.graphics.drawing'; +import common2D from '@ohos.graphics.common2D'; +import display from '@ohos.display'; + +let screen = display.getDefaultDisplaySync(); +let screenWidth: number = screen.width; + +export class ShapeRenderNodeCanvasPen extends RenderNode { + async draw(context: DrawContext) { + const w = screenWidth / 4; + const canvas = context.canvas; + canvas.detachBrush(); + const startX = 200; + const startY = 60; + const fontTitle = new drawing.Font(); + fontTitle.setSize(30); // 30 font size + let text = getContext().resourceManager.getStringSync($r('app.string.GeometryWithPen')); + const textBlob = + drawing.TextBlob.makeFromString(text, fontTitle, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(textBlob, startX, startY); + // font + const font = new drawing.Font(); + font.setSize(22); // 22 font size + // pen + const pen = new drawing.Pen(); + const color: common2D.Color = { + alpha: 0xFF, + red: 0x44, + green: 0x72, + blue: 0xC3 + }; + pen.setColor(color); + pen.setAntiAlias(true); + pen.setStrokeWidth(3); + // rect + const startX1 = 20; + const startY1 = 120; + const stringRect = + drawing.TextBlob.makeFromString('DrawRect', font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(stringRect, startX1, startY1); + canvas.attachPen(pen); + const rect: common2D.Rect = { + left: 20, + top: 150, + right: w - 20, + bottom: 250 + }; + canvas.drawRect(rect); + canvas.detachPen(); + // roundrect + const startX2 = w; + const startY2 = 120; + const stringRoundRect = + drawing.TextBlob.makeFromString('DrawRoundRect', font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(stringRoundRect, startX2, startY2); + canvas.attachPen(pen); + const rectRoundRect: common2D.Rect = { + left: w + 20, + top: 150, + right: w * 2 - 20, + bottom: 250 + }; + const roundRect = new drawing.RoundRect(rectRoundRect, 20, 20); + canvas.drawRoundRect(roundRect); + canvas.detachPen(); + // line + const startX3 = w * 2 + 30; + const startY3 = 120; + const stringLine = + drawing.TextBlob.makeFromString('DrawLine', font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(stringLine, startX3, startY3); + canvas.attachPen(pen); + canvas.drawLine(w * 2 + 20, 200, w * 3 - 20, 200); + canvas.detachPen(); + // arc + const startX4 = w * 3 + 10; + const startY4 = 120; + const stringArc = + drawing.TextBlob.makeFromString('DrawArc', font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(stringArc, startX4, startY4); + canvas.attachPen(pen); + const rectArc: common2D.Rect = { + left: w * 3 - 50, + top: 150, + right: w * 4 - 20, + bottom: 350 + }; + canvas.drawArc(rectArc, 0, -90); + canvas.detachPen(); + // path + const startX5 = 20; + const startY5 = 320; + const stringPath = + drawing.TextBlob.makeFromString('DrawPath', font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(stringPath, startX5, startY5); + canvas.attachPen(pen); + const path = new drawing.Path(); + const rectFirst: common2D.Rect = { + left: 10, + top: 350, + right: w / 2, + bottom: 450 + }; + path.addArc(rectFirst, 0, -130); + const rectSecond: common2D.Rect = { + left: w / 2, + top: 350, + right: w - 20, + bottom: 450 + }; + path.addArc(rectSecond, 50, 130); + canvas.drawPath(path); + canvas.detachPen(); + // circle + const startX6 = w + 20; + const startY6 = 320; + const stringCircle = + drawing.TextBlob.makeFromString('DrawCircle', font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(stringCircle, startX6, startY6); + canvas.attachPen(pen); + canvas.drawCircle(w * 3 / 2, 400, 50); + canvas.detachPen(); + // oval + const startX7 = w * 2 + 25; + const startY7 = 320; + const stringOval = + drawing.TextBlob.makeFromString('DrawOval', font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(stringOval, startX7, startY7); + canvas.attachPen(pen); + const rectOval: common2D.Rect = { + left: w * 2 + 20, + top: 350, + right: w * 3 - 20, + bottom: 430 + }; + canvas.drawOval(rectOval); + canvas.detachPen(); + + // Region + const textBlob1 = drawing.TextBlob.makeFromString('DrawRegion', font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(textBlob1, 20, 520); + canvas.detachBrush(); + canvas.attachPen(pen); + let centerX = 70; + let centerY = 600; + let sideLength = 60; + let angleIncrement = 2 * Math.PI / 5; + let regionPath = new drawing.Path(); + for (let i = 0; i < 5; ++i) { + let angle = angleIncrement * i; + let x = centerX + sideLength * Math.cos(angle - 60); + let y = centerY - sideLength * Math.sin(angle - 60); + if (i === 0) { + regionPath.moveTo(x, y); + } else { + regionPath.lineTo(x, y); + } + } + ; + regionPath.close(); + let region = new drawing.Region(); + let clip = new drawing.Region(); + clip.setRect(10, 400, 690, 650); + region.setPath(regionPath, clip); + canvas.drawRegion(region); + canvas.detachPen(); + + // Point + let startX9 = 20.0+w; + let startY9 = 520.0; + const stringPoint = drawing.TextBlob.makeFromString('DrawPoint', font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(stringPoint, startX9, startY9); + let pPen = new drawing.Pen(); + pPen.setStrokeWidth(8.0); + canvas.attachPen(pPen); + canvas.drawPoint(80.0 + w, 580.0); + canvas.detachPen(); + } +} + +export class ShapeRenderNodeCanvasBrush extends RenderNode { + async draw(context: DrawContext) { + const w = screenWidth / 4; + const dis = screenWidth / 2; + const canvas = context.canvas; + canvas.detachBrush(); + const startX = 200; + const startY = 60 + dis; + const fontTitle = new drawing.Font(); + fontTitle.setSize(30); // 30 font size + let text1 = getContext().resourceManager.getStringSync($r('app.string.GeometryWithBrush')); + const textBlob = + drawing.TextBlob.makeFromString(text1, fontTitle, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(textBlob, startX, startY); + // font + const font = new drawing.Font(); + font.setSize(22); // 22 font size + // set brush + const brush = new drawing.Brush(); + const color: common2D.Color = { + alpha: 0xFF, + red: 0x44, + green: 0x72, + blue: 0xC3 + }; + brush.setColor(color); + // rect + const startX1 = 20; + const startY1 = 120; + const stringRect = + drawing.TextBlob.makeFromString('DrawRect', font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(stringRect, startX1, startY1 + dis); + canvas.attachBrush(brush); + const rect: common2D.Rect = { + left: 20, + top: 150 + dis, + right: w - 20, + bottom: 250 + dis + }; + canvas.drawRect(rect); + canvas.detachBrush(); + // roundrect + const startX2 = w; + const startY2 = 120; + const stringRoundRect = + drawing.TextBlob.makeFromString('DrawRoundRect', font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(stringRoundRect, startX2, startY2 + dis); + canvas.attachBrush(brush); + const rectRoundRect: common2D.Rect = { + left: w + 20, + top: 150 + dis, + right: w * 2 - 20, + bottom: 250 + dis + }; + const roundRect = new drawing.RoundRect(rectRoundRect, 20, 20); + canvas.drawRoundRect(roundRect); + canvas.detachBrush(); + // line + const startX3 = w * 2 + 30; + const startY3 = 120; + const stringLine = + drawing.TextBlob.makeFromString('DrawLine', font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(stringLine, startX3, startY3 + dis); + canvas.attachBrush(brush); + canvas.drawLine(w * 2 + 20, 200 + dis, w * 3 - 20, 200 + dis); + canvas.detachBrush(); + // arc + const startX4 = w * 3 + 10; + const startY4 = 120; + const stringArc = + drawing.TextBlob.makeFromString('DrawArc', font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(stringArc, startX4, startY4+dis); + canvas.attachBrush(brush); + const rectArc: common2D.Rect = { + left: w * 3 - 50, + top: 150 + dis, + right: w * 4 - 20, + bottom: 350 + dis + }; + canvas.drawArc(rectArc, 0, -90); + canvas.detachBrush(); + // path + const startX5 = 20; + const startY5 = 320; + const stringPath = + drawing.TextBlob.makeFromString('DrawPath', font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(stringPath, startX5, startY5 + dis); + canvas.attachBrush(brush); + const path = new drawing.Path(); + const rectFirst: common2D.Rect = { + left: 10, + top: 350 + dis, + right: w / 2, + bottom: 450 + dis + }; + path.addArc(rectFirst, 0, -130); + const rectSecond: common2D.Rect = { + left: w / 2, + top: 350 + dis, + right: w - 20, + bottom: 450 + dis + }; + path.addArc(rectSecond, 50, 130); + canvas.drawPath(path); + canvas.detachBrush(); + // circle + const startX6 = w + 20; + const startY6 = 320; + const stringCircle = + drawing.TextBlob.makeFromString('DrawCircle', font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(stringCircle, startX6, startY6 + dis); + canvas.attachBrush(brush); + canvas.drawCircle(w * 3 / 2, 400 + dis, 50); + canvas.detachBrush(); + // oval + const startX7 = w * 2 + 25; + const startY7 = 320; + const stringOval = + drawing.TextBlob.makeFromString('DrawOval', font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(stringOval, startX7, startY7 + dis); + canvas.attachBrush(brush); + const rectOval: common2D.Rect = { + left: w * 2 + 20, + top: 350 + dis, + right: w * 3 - 20, + bottom: 430 + dis + }; + canvas.drawOval(rectOval); + canvas.detachBrush(); + // background + const brushLayer = new drawing.Brush; + brushLayer.setColor({ alpha:0xFF,red:0xFF,green:0xFF,blue:0xFF }); + const rectLayer: common2D.Rect = { + left: 0, + top: 0, + right: 0, + bottom: 0 + }; + canvas.saveLayer(rectLayer, brushLayer); + const startX8 = w * 3 - 10; + const startY8 = 320; + font.setSize(20); + const stringBackground = + drawing.TextBlob.makeFromString('DrawBackground', font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(stringBackground, startX8, startY8 + dis); + const rectBackground: common2D.Rect = { + left: w * 3 + 20, + top: 350 + dis, + right: w * 4 - 20, + bottom: 450 + dis + }; + canvas.clipRect(rectBackground, drawing.ClipOp.INTERSECT); + canvas.attachBrush(brush); + canvas.drawBackground(brush); + canvas.detachBrush(); + canvas.restore(); + + // Region + const textBlob1 = drawing.TextBlob.makeFromString('DrawRegion', font, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(textBlob1, 20, 680 + dis); + canvas.detachBrush(); + const brushBitmap = new drawing.Brush(); + brushBitmap.setColor({alpha: 0xFF, red: 0x44, green: 0x72, blue: 0xC3}); + canvas.attachBrush(brushBitmap); + let centerX = 70; + let centerY = 780 + dis; + let sideLength = 60; + let angleIncrement = 2 * Math.PI / 5; + let regionPath = new drawing.Path(); + for (let i = 0; i < 5; ++i) { + let angle = angleIncrement * i; + let x = centerX + sideLength * Math.cos(angle - 60); + let y = centerY - sideLength * Math.sin(angle - 60); + if (i === 0) { + regionPath.moveTo(x, y); + } else { + regionPath.lineTo(x, y); + } + }; + regionPath.close(); + let region = new drawing.Region(); + let clip = new drawing.Region(); + clip.setRect(10, 600 + dis, 690, 850 + dis); + region.setPath(regionPath, clip); + canvas.drawRegion(region); + canvas.detachBrush(); + } +} \ No newline at end of file diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/common/TextBlobRenderNode.ets b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/common/TextBlobRenderNode.ets new file mode 100644 index 0000000000000000000000000000000000000000..2ce6cb66c75ec85b0297f719653a0ce4b383a747 --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/common/TextBlobRenderNode.ets @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { RenderNode, DrawContext } from '@ohos.arkui.node'; +import drawing from '@ohos.graphics.drawing'; +import common2D from '@ohos.graphics.common2D'; +import image from '@ohos.multimedia.image'; +import display from '@ohos.display'; + +let screen = display.getDefaultDisplaySync(); +let screenWidth: number = screen.width; + +export class TextBlobRenderNode extends RenderNode { + private pMap: image.PixelMap | undefined = undefined; + + async draw(context: DrawContext) { + const canvas = context.canvas; + let startX = 20.0; + let startY = 600.0; + let fontTitle = new drawing.Font(); + fontTitle.setSize(screenWidth / 25); + let text = getContext().resourceManager.getStringSync($r('app.string.DrawTextBlob')); + const stringImage = drawing.TextBlob.makeFromString(text, + fontTitle, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(stringImage, screenWidth / 3, 550.0); + let text1 = getContext().resourceManager.getStringSync($r('app.string.DrawBaseTextBlob')); + const stringBasicText = drawing.TextBlob.makeFromString(text1, + fontTitle, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(stringBasicText, startX, startY); + // 基本字块绘制 + const font1 = new drawing.Font(); + font1.setSize(100); + const textBlob1 = drawing.TextBlob.makeFromString('Hello world', font1, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(textBlob1, 100, 720); + + let text2 = getContext().resourceManager.getStringSync($r('app.string.FontWithPen')); + const stringTextPen = drawing.TextBlob.makeFromString(text2, + fontTitle, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(stringTextPen, startX, startY + 280); + // 文字描边 + let pen1 = new drawing.Pen(); + pen1.setAntiAlias(true); + pen1.setStrokeWidth(3.0); + pen1.setColor(0xFF, 0xFF, 0x00, 0x00); + const font2 = new drawing.Font(); + font2.setSize(100); + canvas.attachPen(pen1); + const textBlob2 = drawing.TextBlob.makeFromString('Hello world', font2, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(textBlob2, 100, 1000); + canvas.detachPen(); + + let text3 = getContext().resourceManager.getStringSync($r('app.string.TextGradient')); + const stringTextShader = drawing.TextBlob.makeFromString(text3, + fontTitle, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(stringTextShader, startX, startY + 520); + + // 文字渐变 + let startPt: common2D.Point = { x: 100, y: 100 + 1000 }; + let endPt: common2D.Point = { x: 900, y: 900 + 1000 }; + let colors = [0xFFFFFF00, 0xFFFF0000, 0xFF0000FF]; + let shaderEffect = drawing.ShaderEffect.createLinearGradient(startPt, endPt, colors, drawing.TileMode.CLAMP); + let brush2 = new drawing.Brush(); + brush2.setShaderEffect(shaderEffect); + canvas.attachBrush(brush2); + const font3 = new drawing.Font(); + font3.setSize(200); + const textBlob3 = drawing.TextBlob.makeFromString('Hello world', font3, drawing.TextEncoding.TEXT_ENCODING_UTF8); + canvas.drawTextBlob(textBlob3, 100, 1300); + canvas.detachBrush(); + } +} \ No newline at end of file diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/entryability/EntryAbility.ets b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..0f2f8b94aa24b0a50e272270e4e18b6df93ac5fd --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +} diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1e212947256c5533c7b06285a597c94f840a6e3 --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/pages/Index.ets b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..4273d55eb204bca050c064c9db586be1ecad2eb9 --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/ets/pages/Index.ets @@ -0,0 +1,238 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { NodeController, FrameNode, RenderNode } from "@ohos.arkui.node"; +import { UIContext } from '@ohos.arkui.UIContext'; +import drawing from "@ohos.graphics.drawing"; +import display from '@ohos.display'; +import image from '@ohos.multimedia.image'; +import hilog from '@ohos.hilog'; +import { resourceManager } from '@kit.LocalizationKit'; +import { BusinessError } from '@kit.BasicServicesKit'; +import {CanvasRenderNodeCropping, CanvasRenderNodeMatrix, CanvasRenderNodeCombination} from "../common/CanvasRenderNode"; +import {ShapeRenderNodeCanvasPen, ShapeRenderNodeCanvasBrush} from "../common/ShapeRenderNode"; +import {PathRenderNode} from "../common/PathRenderNode"; +import { MyRenderNodePathBrush, MyRenderNodePathShader, MyRenderNodePathPen } from '../common/MyRenderNodePath'; +import {ImageRenderNode, ImageRectRenderNode} from '../common/ImageRenderNode'; +import {TextBlobRenderNode} from '../common/TextBlobRenderNode'; +import {ComplexRenderNode} from '../common/ComplexRenderNode'; + +const TAG = 'tsdrawingsample'; +let screen = display.getDefaultDisplaySync(); +let screenWidth: number = screen.width; +let screenHeight: number = screen.height; + +let canvas_: drawing.Canvas; +let pixelMap_: image.PixelMap | undefined; + +function func(node: RenderNode) { + node.frame = { + x: 0, + y: 0, + width: screenWidth, + height: screenHeight + } +} + +const canvasCroppingNode = new CanvasRenderNodeCropping(); +func(canvasCroppingNode); + +const canvasMatrixNode = new CanvasRenderNodeMatrix(); +func(canvasMatrixNode); + +const canvasCombinationNode = new CanvasRenderNodeCombination(); +func(canvasCombinationNode); + +const shapePenNode = new ShapeRenderNodeCanvasPen(); +func(shapePenNode); + +const shapeBrushNode = new ShapeRenderNodeCanvasBrush(); +func(shapeBrushNode); + +const pathNode = new PathRenderNode(); +func(pathNode); + +const pathBrushNode = new MyRenderNodePathBrush(); +func(pathBrushNode); + +const pathPenNode = new MyRenderNodePathPen(); +func(pathPenNode); + +const pathShaderNode = new MyRenderNodePathShader(); +func(pathShaderNode); + +const imagePixmapNode = new ImageRenderNode(); +func(imagePixmapNode); + +const imageRectNode = new ImageRectRenderNode(); +func(imageRectNode); + +const textblobNode = new TextBlobRenderNode(); +func(textblobNode); + +const complexNode = new ComplexRenderNode(); +func(complexNode); + + +class MyNodeController extends NodeController { + private rootNode: FrameNode | null = null; + + makeNode(uiContext: UIContext): FrameNode { + this.rootNode = new FrameNode(uiContext); + if (this.rootNode === null) { + return this.rootNode; + } + const renderNode = this.rootNode.getRenderNode(); + if (renderNode !== null) { + renderNode.frame = { + x: 0, + y: 0, + width: 10, + height: 500 + }; + renderNode.pivot = { x: 50, y: 50 }; + } + return this.rootNode; + } + + addNode(node: RenderNode): void { + if (this.rootNode === null) { + return; + } + const renderNode = this.rootNode.getRenderNode(); + if (renderNode !== null) { + renderNode.appendChild(node); + } + } + + clearNodes(): void { + if (this.rootNode === null) { + return; + } + const renderNode = this.rootNode.getRenderNode(); + if (renderNode !== null) { + renderNode.clearChildren(); + } + } +} + + +@Entry +@Component +struct RenderTest { + private myNodeController: MyNodeController = new MyNodeController(); + + aboutToAppear() { + const imageContext: Context = getContext(this); + const resourceMgr: resourceManager.ResourceManager = imageContext.resourceManager; + resourceMgr.getRawFileContent('startIcon.png').then((fileData: Uint8Array) => { + hilog.info(0x0001, TAG, 'Succeeded in getting RawFileContent'); + const buffer = fileData.buffer.slice(0); + const imageSource: image.ImageSource = image.createImageSource(buffer); + imageSource.createPixelMap().then((pMap: image.PixelMap) => { + imagePixmapNode.setPixelMap(pMap); + imageRectNode.setPixelMap(pMap); + }).catch((err: BusinessError) => { + hilog.error(0x0001, TAG, 'Failed to creating PixelMap'); + }); + }).catch((err: BusinessError) => { + hilog.error(0x0001, TAG, 'Failed to get RawFileContent'); + }); + } + + build() { + Column() { + Row() { + NodeContainer(this.myNodeController) + .height('100%') + } + .width('100%') + .height('80%') + GridRow({ + columns: 1, + gutter: {x: 5, y: 10}, + }) { + GridCol({ span: { xs: 1, sm: 2, md: 3, lg: 4 }, offset: 0, order: 0 }){ + Row() { + Button("Canvas") + .margin({ top: 10, right: 10 }) + .onClick(() => { + this.myNodeController.clearNodes(); + this.myNodeController.addNode(canvasCroppingNode); + this.myNodeController.addNode(canvasMatrixNode); + this.myNodeController.addNode(canvasCombinationNode); + }) + Button("Geometric shape") + .margin({ top: 10, right: 10 }) + .onClick(() => { + this.myNodeController.clearNodes(); + this.myNodeController.addNode(shapePenNode); + this.myNodeController.addNode(shapeBrushNode); + }) + Button("Path") + .margin({ top: 10, right: 10 }) + .onClick(() => { + this.myNodeController.clearNodes(); + this.myNodeController.addNode(pathNode); + this.myNodeController.addNode(pathBrushNode); + this.myNodeController.addNode(pathPenNode); + this.myNodeController.addNode(pathShaderNode); + }) + } + } + GridCol({ span: { xs: 1, sm: 2, md: 3, lg: 4 }, offset: 0, order: 0 }){ + Row(){ + Button("Image and Font") + .margin({ right: 8 }) + .onClick(() => { + this.myNodeController.clearNodes(); + this.myNodeController.addNode(imageRectNode); + this.myNodeController.addNode(imagePixmapNode); + this.myNodeController.addNode(textblobNode); + }) + Button("Complex") + .margin({ right: 8 }) + .onClick(() => { + this.myNodeController.clearNodes(); + this.myNodeController.addNode(complexNode); + }) + } + } + } + .width('100%') + .shadow(ShadowStyle.OUTER_DEFAULT_SM) + .layoutWeight(1) + } + } + + public async CreateBitmapCanvas() { + const color: ArrayBuffer = new ArrayBuffer(screenWidth * screenHeight * 4); + let opts: image.InitializationOptions = + { editable: true, pixelFormat: 3, size: { height: screenHeight, width: screenWidth } }; + pixelMap_ = await image.createPixelMap(color, opts); + if (pixelMap_ === null || pixelMap_ === undefined) { + hilog.info(0x0001, TAG, 'create pixelMap error'); + return; + } + + // 创建离屏canvas,与pixelMap绑定 + canvas_ = new drawing.Canvas(pixelMap_); + if (canvas_ === null || canvas_ === undefined) { + hilog.info(0x0001, TAG, 'create canvas_ error'); + return; + } + hilog.info(0x0001, TAG, 'create canvas_ success'); + } +} diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/module.json5 b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4144486d1af4c03b0d767cce1cda86fc0d697f91 --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/module.json5 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/base/element/color.json b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/base/element/string.json b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..5a86a0e0b39dd7b539ee3533fb608f9063736575 --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/base/element/string.json @@ -0,0 +1,120 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "TsDrawingSample" + }, + { + "name": "crop", + "value": "tailor" + }, + { + "name": "scaling", + "value": "scaling" + }, + { + "name": "translation_and_rotation", + "value": "translation and rotation" + }, + { + "name": "tailor_and_translation", + "value": "tailor and translation" + }, + { + "name": "ShaderEffect", + "value": "ShaderEffect" + }, + { + "name": "ImageFilter", + "value": "ImageFilter" + }, + { + "name": "PathEffect", + "value": "PathEffect" + }, + { + "name": "DrawImage", + "value": "DrawImage" + }, + { + "name": "DrawPixelMapMesh", + "value": "DrawPixelMapMesh" + }, + { + "name": "DrawPicture", + "value": "DrawPicture" + }, + { + "name": "DrawImageRectWithSrc", + "value": "DrawImageRectWithSrc" + }, + { + "name": "DrawImageRect", + "value": "DrawImageRect" + }, + { + "name": "DrawPathWithBrush", + "value": "DrawPathWithBrush" + }, + { + "name": "DrawPathWithPen", + "value": "DrawPathWithPen" + }, + { + "name": "DrawPathWithShader", + "value": "DrawPathWithShader" + }, + { + "name": "PathWithBrush", + "value": "PathWithBrush" + }, + { + "name": "PathWithPen", + "value": "PathWithPen" + }, + { + "name": "CurveClose", + "value": "CurveClose" + }, + { + "name": "CurveNotClose", + "value": "CurveNotClose" + }, + { + "name": "ComplexPath", + "value": "ComplexPath" + }, + { + "name": "GeometryWithPen", + "value": "GeometryWithPen" + }, + { + "name": "GeometryWithBrush", + "value": "GeometryWithBrush" + }, + { + "name": "DrawTextBlob", + "value": "DrawTextBlob" + }, + { + "name": "DrawBaseTextBlob", + "value": "DrawBaseTextBlob" + }, + { + "name": "FontWithPen", + "value": "FontWithPen" + }, + { + "name": "TextGradient", + "value": "TextGradient" + } + ] +} \ No newline at end of file diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/base/media/app_icon.png b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..cd45accb1dfd2fd0da16c732c72faa6e46b26521 Binary files /dev/null and b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/base/media/app_icon.png differ diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/base/media/background.png b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/base/media/background.png differ diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/base/media/foreground.png b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/base/media/foreground.png differ diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/base/media/layered_image.json b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/base/media/startIcon.png b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/base/media/startIcon.png differ diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/base/profile/backup_config.json b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/base/profile/main_pages.json b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/en_US/element/string.json b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/en_US/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..5a86a0e0b39dd7b539ee3533fb608f9063736575 --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/en_US/element/string.json @@ -0,0 +1,120 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "TsDrawingSample" + }, + { + "name": "crop", + "value": "tailor" + }, + { + "name": "scaling", + "value": "scaling" + }, + { + "name": "translation_and_rotation", + "value": "translation and rotation" + }, + { + "name": "tailor_and_translation", + "value": "tailor and translation" + }, + { + "name": "ShaderEffect", + "value": "ShaderEffect" + }, + { + "name": "ImageFilter", + "value": "ImageFilter" + }, + { + "name": "PathEffect", + "value": "PathEffect" + }, + { + "name": "DrawImage", + "value": "DrawImage" + }, + { + "name": "DrawPixelMapMesh", + "value": "DrawPixelMapMesh" + }, + { + "name": "DrawPicture", + "value": "DrawPicture" + }, + { + "name": "DrawImageRectWithSrc", + "value": "DrawImageRectWithSrc" + }, + { + "name": "DrawImageRect", + "value": "DrawImageRect" + }, + { + "name": "DrawPathWithBrush", + "value": "DrawPathWithBrush" + }, + { + "name": "DrawPathWithPen", + "value": "DrawPathWithPen" + }, + { + "name": "DrawPathWithShader", + "value": "DrawPathWithShader" + }, + { + "name": "PathWithBrush", + "value": "PathWithBrush" + }, + { + "name": "PathWithPen", + "value": "PathWithPen" + }, + { + "name": "CurveClose", + "value": "CurveClose" + }, + { + "name": "CurveNotClose", + "value": "CurveNotClose" + }, + { + "name": "ComplexPath", + "value": "ComplexPath" + }, + { + "name": "GeometryWithPen", + "value": "GeometryWithPen" + }, + { + "name": "GeometryWithBrush", + "value": "GeometryWithBrush" + }, + { + "name": "DrawTextBlob", + "value": "DrawTextBlob" + }, + { + "name": "DrawBaseTextBlob", + "value": "DrawBaseTextBlob" + }, + { + "name": "FontWithPen", + "value": "FontWithPen" + }, + { + "name": "TextGradient", + "value": "TextGradient" + } + ] +} \ No newline at end of file diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/rawfile/startIcon.png b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/rawfile/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..366f76459ffd4494ec40d0ddd5c59385b9c5da11 Binary files /dev/null and b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/rawfile/startIcon.png differ diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/zh_CN/element/string.json b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/zh_CN/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..fd0ca0876cb803bf084b2aafdc15677e66ecaae3 --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/main/resources/zh_CN/element/string.json @@ -0,0 +1,120 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "模块描述" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "TsDrawingSample" + }, + { + "name": "crop", + "value": "裁剪" + }, + { + "name": "scaling", + "value": "矩阵变换-缩放" + }, + { + "name": "translation_and_rotation", + "value": "矩阵变换-平移和旋转" + }, + { + "name": "tailor_and_translation", + "value": "裁剪+平移" + }, + { + "name": "ShaderEffect", + "value": "ShaderEffect" + }, + { + "name": "ImageFilter", + "value": "ImageFilter" + }, + { + "name": "PathEffect", + "value": "PathEffect" + }, + { + "name": "DrawImage", + "value": "DrawImage" + }, + { + "name": "DrawPixelMapMesh", + "value": "DrawPixelMapMesh" + }, + { + "name": "DrawPicture", + "value": "图片绘制" + }, + { + "name": "DrawImageRectWithSrc", + "value": "DrawImageRectWithSrc" + }, + { + "name": "DrawImageRect", + "value": "DrawImageRect" + }, + { + "name": "DrawPathWithBrush", + "value": "Path填充绘制矢量图元" + }, + { + "name": "DrawPathWithPen", + "value": "Path描边绘制矢量图元" + }, + { + "name": "DrawPathWithShader", + "value": "Path渐变绘制矢量图元" + }, + { + "name": "PathWithBrush", + "value": "路径填充" + }, + { + "name": "PathWithPen", + "value": "路径描边" + }, + { + "name": "CurveClose", + "value": "曲线闭合" + }, + { + "name": "CurveNotClose", + "value": "曲线未闭合" + }, + { + "name": "ComplexPath", + "value": "复杂路径" + }, + { + "name": "GeometryWithPen", + "value": "几何形状绘制-描边" + }, + { + "name": "GeometryWithBrush", + "value": "几何形状绘制-填充" + }, + { + "name": "DrawTextBlob", + "value": "字块绘制" + }, + { + "name": "DrawBaseTextBlob", + "value": "基本字块绘制" + }, + { + "name": "FontWithPen", + "value": "文字描边" + }, + { + "name": "TextGradient", + "value": "文字渐变" + } + ] +} \ No newline at end of file diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/mock/mock-config.json5 b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/mock/mock-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..b9a78e201535765168a92d3543c690273ecdc019 --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/mock/mock-config.json5 @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ +} \ No newline at end of file diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/ohosTest/ets/test/Ability.test.ets b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..c3496a42e7765f954c2e2448ec26590ed0691fbe --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry'; +import { describe, it, expect } from '@ohos/hypium'; +import { Driver, ON } from '@ohos.UiTest'; +import hilog from '@ohos.hilog' + +const TAG = '[Sample_DrawingAPI]'; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用 + */ + it("StartAbility_001", 0, async (done: Function) => { + hilog.info(0x0001, TAG, 'StartAbility_001 begin'); + let driver = Driver.create(); + await driver.delayMs(1000); + done(); + hilog.info(0x0001, TAG, 'StartAbility_001 end'); + }) + + /** + * 点击按钮,绘制路径 + */ + it('DrawPath_001', 2, async () => { + hilog.info(0x0001, TAG, 'DrawPath_001 begin'); + let driver = Driver.create(); + let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); + try { + await abilityDelegator.startAbility({ + bundleName: 'com.samples.tsdrawingsample', + abilityName: 'EntryAbility' + }); + } catch (exception) { + expect().assertFail(); + } + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('Path')); + let drawPathBtn = await driver.findComponent(ON.text('Path')); + // 点击'Path'按钮 + await drawPathBtn.click(); + await driver.delayMs(1000); + hilog.info(0x0001, TAG, 'DrawPath_001 end'); + }) + + /** + * 点击按钮,绘制画布 + */ + it('DrawCanvas_001', 2, async () => { + hilog.info(0x0001, TAG, 'DrawCanvas_001 begin'); + let driver = Driver.create(); + let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); + try { + await abilityDelegator.startAbility({ + bundleName: 'com.samples.tsdrawingsample', + abilityName: 'EntryAbility' + }); + } catch (exception) { + expect().assertFail(); + } + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('Canvas')); + let drawPathBtn = await driver.findComponent(ON.text('Canvas')); + // 点击'Canvas'按钮 + await drawPathBtn.click(); + await driver.delayMs(1000); + hilog.info(0x0001, TAG, 'DrawCanvas_001 end'); + }) + + /** + * 点击按钮,绘制文字和图片 + */ + it('ImageAndFont_001', 2, async () => { + hilog.info(0x0001, TAG, 'ImageAndFont_001 begin'); + let driver = Driver.create(); + let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); + try { + await abilityDelegator.startAbility({ + bundleName: 'com.samples.tsdrawingsample', + abilityName: 'EntryAbility' + }); + } catch (exception) { + expect().assertFail(); + } + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('Image and Font')); + let drawPathBtn = await driver.findComponent(ON.text('Image and Font')); + // 点击'Image and Font'按钮 + await drawPathBtn.click(); + await driver.delayMs(1000); + hilog.info(0x0001, TAG, 'ImageAndFont_001 end'); + }) + }) +} \ No newline at end of file diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/ohosTest/ets/test/List.test.ets b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..f40533d1374f4046f8af1e7df6aa90157cb361d8 --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/ohosTest/module.json5 b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c3fd9dda3040d888d9d8b0b62bcb5d3b6fbeb614 --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "entry_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/test/List.test.ets b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..f1186b1f53c3a70930921c5dbd1417332bec56c9 --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import localUnitTest from './LocalUnit.test'; + +export default function testsuite() { + localUnitTest(); +} \ No newline at end of file diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/test/LocalUnit.test.ets b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/test/LocalUnit.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..7fc57c77dbf76d8df08a2b802a55b948e3fcf968 --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/entry/src/test/LocalUnit.test.ets @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function localUnitTest() { + describe('localUnitTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }); + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }); + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }); + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }); + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }); + }); +} \ No newline at end of file diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/hvigor/hvigor-config.json5 b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..43beb743cbd25c3507b1cf8a744bf8197b3bf2fb --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.0", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/hvigorfile.ts b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a5e543f190732c159beb574dfc9fa37bc94e156 --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/oh-package.json5 b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..93f097993a458e967d6d5239ea0580e79b5d6998 --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.0", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/ohosTest.md b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..7102e28eb2f1d7dfd33b21fd226c14c091eea141 --- /dev/null +++ b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/ohosTest.md @@ -0,0 +1,10 @@ +# NdkTsDrawing 测试用例归档 + +## 用例表 + +|测试功能|预置条件|输入|预期输出|是否自动|测试结果| +|--------------------------------|--------------------------------|--------------------------------|--------------------------------|--------------------------------|--------------------------------| +| 拉起应用 | 设备正常运行 | |成功拉起应用|是| Pass | +| 绘制图形 | 位于首页 | 点击** Path** | 页面绘制路径效果 | 是 | Pass | +| 绘制图形 | 位于首页 | 点击**Canvas** | 页面绘制画布效果 | 是 | Pass | +| 绘制图形 | 位于首页 | 点击**Image and Font** | 页面显示图片和文字效果 | 是 | Pass | \ No newline at end of file diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/screenshots/device/Canvas.png b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/screenshots/device/Canvas.png new file mode 100644 index 0000000000000000000000000000000000000000..57644d740c1dfca30d47ac8b4b649b5fddf2af9d Binary files /dev/null and b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/screenshots/device/Canvas.png differ diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/screenshots/device/Complex.png b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/screenshots/device/Complex.png new file mode 100644 index 0000000000000000000000000000000000000000..c0dd825d2d4357b6be245a24c6c8457db9f3bbea Binary files /dev/null and b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/screenshots/device/Complex.png differ diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/screenshots/device/GeometricShape.png b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/screenshots/device/GeometricShape.png new file mode 100644 index 0000000000000000000000000000000000000000..5e12bbd63135dc6d71f05affacc155aab2ef92ef Binary files /dev/null and b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/screenshots/device/GeometricShape.png differ diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/screenshots/device/ImageAndFont.png b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/screenshots/device/ImageAndFont.png new file mode 100644 index 0000000000000000000000000000000000000000..a9927d84c0c184375ded14d17c5b79cf19bc5a05 Binary files /dev/null and b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/screenshots/device/ImageAndFont.png differ diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/screenshots/device/Index.png b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/screenshots/device/Index.png new file mode 100644 index 0000000000000000000000000000000000000000..3bcf8099c7ab6091f1d5ef7f4a02b31135e13980 Binary files /dev/null and b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/screenshots/device/Index.png differ diff --git a/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/screenshots/device/Path.png b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/screenshots/device/Path.png new file mode 100644 index 0000000000000000000000000000000000000000..11203a2dd108ab3de4352318811d9c88b71bfcb5 Binary files /dev/null and b/code/BasicFeature/Graphics/Graphics2d/TsDrawingSample/screenshots/device/Path.png differ