目录

HandwriteController

Main entrance for accessing functions of the handwriting suite, including the main methods of the handwriting capability.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Applications.StylusFeature

Start version: 5.0.0(12)

Module Import

import { HandwriteController } from '@kit.Penkit';

This module provides the methods described in the following table to load and save handwritten content.

Method

Description

load

Loads handwritten content.

save

Saves handwritten content.

onLoad

Registers the callback function when the saving is complete.

load

load(path: string): void

Loads a note file from a specified path. Call this API after the handwriting suite is initialized.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Stylus.Handwrite

Start version: 5.0.0(12)

Parameters:

Parameter

Type

Mandatory

Description

path

string

Yes

Path of the file to be loaded.

Error codes:

For details about the error codes, please refer to ArkTS API Error Codes.

Result Code

Error Message

1010400001

Loading failed.

Example:

For details about the code example, see onLoad.

save

save(path: string): Promise<void>

Saves notes to a specified path.

Call this API after the handwriting suite is loaded.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Stylus.Handwrite

Start version: 5.0.0(12)

Parameters:

Parameter

Type

Mandatory

Description

path

string

Yes

Path for saving the file.

Returns:

Type

Description

Promise<void>

Promise object without a return value.

Error codes:

For details about the error codes, please refer to ArkTS API Error Codes.

Result Code

Error Message

1010400002

Saving failed.

Example:

For details about the code example, see onLoad.

onLoad

onLoad(callback: AsyncCallback<string>): void

Registers a callback. This callback will be triggered when the loading is complete.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Stylus.Handwrite

Start version: 5.0.0(12)

Parameters:

Parameter

Type

Mandatory

Description

callback

AsyncCallback<string>

Yes

Callback function. If the loading is successful, err is undefined and string is the loading path; otherwise, an error object will be returned.

Error codes:

For details about the error codes, please refer to ArkTS API Error Codes.

Result Code

Error Message

1010400001

Initialization loading failed.

Example:
import { HandwriteComponent, HandwriteController} from '@kit.Penkit';

@Component
struct HandWritingComponent {
  controller: HandwriteController = new HandwriteController();
  // Obtain the path for storing the handwritten file based on the app storage rule. The path is for reference only.
  initPath : string = "aa";

  aboutToAppear(){
    // Set the callback for the completion of the saving action during loading.
    this.controller.onLoad(this.callback);
  }

  // Callback triggered once the handwritten file is loaded and rendered on the screen. You will be notified of the result and can then customize actions here that custom action can be performed here.
  callback = () => {
    // Custom action, for example, displaying the user operation guide after the file is loaded.
  }

  aboutToDisappear(){
    // Call the saving API when HandWriteDemo exits.
    const path : string = `savePath`; // Obtain the path for storing the handwritten file based on the app storage rule. The path is for reference only.
    this.controller?.save(path);
  }

  build() {
    Row() {
      Column() {
        HandwriteComponent({
          handwriteController: this.controller,
          onInit: () => {
            // Callback triggered once the canvas initialization is complete. In this case, you can call the API to load and display the note content.
            this.controller?.load(this.initPath);
          },
          onScale: (scale: number) => {
            // Callback triggered when the canvas is zoomed in or out. The scaling factor of the current handwriting control is returned. Custom actions can be performed here.
          }
        })
      }
      .width('100%')
    }
    .height('100%')
  }
}

The methods in HandwriteController need to be executed in the canvas control initialization callback or in customized methods in the preceding example.