目录

PointPredictor (Point Prediction Function)

This module provides the point prediction capability. By calling the point prediction API, you can optimize the drawing and writing performance in your app, improving the user experience of writing with a stylus.

Since: 5.0.0(12)

Modules to Import

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

The module provides the following methods.

Method Description
getPredictionPoint Obtains the prediction point information.

getPredictionPoint

getPredictionPoint(event: TouchEvent): TouchPoint

Obtains the prediction point information.

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

System capability: SystemCapability.Stylus.Handwrite

Since: 5.0.0(12)

Parameters:

Parameter Type Mandatory Description
event TouchEvent Yes Information about the current touch event.

Returns:

Type Description
TouchPoint Prediction point information.

Example:

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

@Entry
@Component
struct PointPredictorDemo {
  @State actualXCoordinate: number = 0;
  @State actualYCoordinate: number = 0;
  @State predictorXCoordinate: Dimension = 0;
  @State predictorYCoordinate: Dimension = 0;
  pointPredictor: PointPredictor = new PointPredictor();

  aboutToAppear() {
    console.info('getPredictionPoint aboutToAppear');
  }

  aboutToDisappear() {
    console.info('getPredictionPoint aboutToDisappear');
  }

  build() {
    Stack({ alignContent: Alignment.TopEnd }) {
      this.canvas(); // Canvas.
    }.height('100%').width('100%')
  }

  // Canvas.
  @Builder
  canvas() {
    Column() {
      Text ('Actual point coordinates: X: '+ this.actualXCoordinate +' Y:' + this.actualYCoordinate) .textAlign(TextAlign.Start)
      Text ('Prediction point coordinates: X: '+ this.predictorXCoordinate +' Y:' + this.predictorYCoordinate)
        .textAlign(TextAlign.Start)
    }.position({ x: 0, y: 0 })
    .alignItems(HorizontalAlign.Start)

    Stack()
      .width('100%')
      .height('100%')
      .onTouch((event: TouchEvent) => {
        switch (event.type) {
          case TouchType.Down: // Create a drawing path when the screen is touched.
            break;
          case TouchType.Move: // Use the prediction algorithm to perform prediction and obtain the prediction point.
            let point = this.pointPredictor?.getPredictionPoint(event);
            this.actualXCoordinate = event.touches[0]?.x;
            this.actualYCoordinate = event.touches[0]?.y;
            this.predictorXCoordinate = point?.x;
            this.predictorYCoordinate = point?.y;
            console.info ('pointPredictor actual point coordinates x:' + event.touches[0]?.x +' y:' + event.touches[0]?.y);
            console.info ('pointPredictor prediction point coordinates x:' + point?.x +' y:' + point?.y);
            break;
          case TouchType.Up:
            break;
        }
      })
  }
}