openharmony 鸿蒙 arkts-apis-graphics-drawing-ShaderEffect

2026-08-25 浏览 (1)

Class (ShaderEffect)

Implements the shader effect. After a shader effect is set for a pen or brush, the shader effect instead of the color attribute is used for drawing. In this case, the alpha value set for the pen or brush still takes effect.

NOTE

  • The initial APIs of this module are supported since API version 11. Newly added APIs will be marked with a superscript to indicate their earliest API version.

  • The initial APIs of this class are supported since API version 12.

  • This module uses the physical pixel unit, px.

  • This module operates under a single-threaded model. The caller needs to manage thread safety and context state transitions.

Modules to Import

import { drawing } from '@kit.ArkGraphics2D';

createComposeShader20+

static createComposeShader(dstShaderEffect: ShaderEffect, srcShaderEffect: ShaderEffect, blendMode: BlendMode): ShaderEffect

Creates a shader by blending two existing shaders in a certain way.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
dstShaderEffectShaderEffectYesShader that serves as the destination color in blend mode.
srcShaderEffectShaderEffectYesShader that serves as the source color in blend mode.
blendModeBlendModeYesBlend mode.

Returns

TypeDescription
ShaderEffectShaderEffect object created.

Error codes

For details about the following error code, see Drawing and Display Error Codes.

IDError Message
25900001Parameter error.Possible causes: Incorrect parameter range.

Example

import { drawing } from '@kit.ArkGraphics2D';

let dstShader = drawing.ShaderEffect.createColorShader(0xFF0000FF);
let srcShader = drawing.ShaderEffect.createColorShader(0xFFFF0000);
let shader = drawing.ShaderEffect.createComposeShader(dstShader, srcShader, drawing.BlendMode.SRC);

createImageShader20+

static createImageShader(pixelmap: image.PixelMap, tileX: TileMode, tileY: TileMode, samplingOptions: SamplingOptions, matrix?: Matrix|null): ShaderEffect

Creates a shader based on an image. You are advised not to use the function for the canvas of the capture type because it affects the performance.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
pixelmapimage.PixelMapYesImage object to be sampled.
tileXTileModeYesTile mode in the horizontal direction.
tileYTileModeYesTile mode in the vertical direction.
samplingOptionsSamplingOptionsYesImage sampling options.
matrixMatrix |nullNo(Optional) Matrix transformation applied to an image. If this parameter is left empty, no transformation is applied.

Returns

TypeDescription
ShaderEffectShaderEffect object created.

Error codes

For details about the following error code, see Drawing and Display Error Codes.

IDError Message
25900001Parameter error.Possible causes: Incorrect parameter range.

Example

import { RenderNode } from '@kit.ArkUI';
import { image } from '@kit.ImageKit';
import { drawing } from '@kit.ArkGraphics2D';

class DrawingRenderNode extends RenderNode {
  draw(context: DrawContext) {
    const width = 1000;
    const height = 1000;
    const bufferSize = width * height * 4;
    const color: ArrayBuffer = new ArrayBuffer(bufferSize);

    const colorData = new Uint8Array(color);
    for (let i = 0; i < colorData.length; i += 4) {
      colorData[i] = 255;
      colorData[i+1] = 156;
      colorData[i+2] = 0;
      colorData[i+3] = 255;
    }

    let opts: image.InitializationOptions = {
      editable: true,
      pixelFormat: 3,
      size: { height, width }
    }

    let pixelMap: image.PixelMap = image.createPixelMapSync(color, opts);
    let matrix = new drawing.Matrix();
    let options = new drawing.SamplingOptions(drawing.FilterMode.FILTER_MODE_NEAREST);
    if (pixelMap != null) {
      let imageShader =
        drawing.ShaderEffect.createImageShader(pixelMap, drawing.TileMode.REPEAT, drawing.TileMode.MIRROR, options,
          matrix);
    }
  }
}

createColorShader12+

static createColorShader(color: number): ShaderEffect

Creates a ShaderEffect object with a single color.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
colornumberYesColor in the ARGB format. The value is a 32-bit unsigned integer.

Returns

TypeDescription
ShaderEffectShaderEffect object with a single color.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types.

Example

import { drawing } from '@kit.ArkGraphics2D';

let shaderEffect = drawing.ShaderEffect.createColorShader(0xFFFF0000);

createLinearGradient12+

static createLinearGradient(startPt: common2D.Point, endPt: common2D.Point, colors: Array<number>, mode: TileMode, pos?: Array<number>|null, matrix?: Matrix|null): ShaderEffect

Creates a ShaderEffect object that generates a linear gradient between two points.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
startPtcommon2D.PointYesStart point.
endPtcommon2D.PointYesEnd point.
colorsArray<number>YesArray of colors to distribute between the two points. The values in the array are 32-bit (ARGB) unsigned integers.
modeTileModeYesTile mode of the shader effect.
posArray<number> |nullNoRelative position of each color in the color array. The array length must be the same as that of colors. The first element in the array must be 0.0, the last element must be 1.0, and the middle elements must be between 0.0 and 1.0 and increase by index. The default value is null, indicating that colors are evenly distributed between the two points.
matrixMatrix |nullNoMatrix object used to perform matrix transformation on the shader effect. The default value is null, indicating the identity matrix.

LinearGradient

The preceding figure shows the display effect when the colors array is set to red, green, and blue and the pos array is set to 0.0, 0.75, and 1.0. The triangle subscript is the relative position of a color between the start point and end point. Gradient colors are used between them.

Returns

TypeDescription
ShaderEffectShaderEffect object created.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types; 3. Parameter verification failed.

Example

import { common2D,drawing } from '@kit.ArkGraphics2D';

let startPt: common2D.Point = { x: 100, y: 100 };
let endPt: common2D.Point = { x: 300, y: 300 };
let shaderEffect = drawing.ShaderEffect.createLinearGradient(startPt, endPt, [0xFF00FF00, 0xFFFF0000], drawing.TileMode.REPEAT);

createRadialGradient12+

static createRadialGradient(centerPt: common2D.Point, radius: number, colors: Array<number>, mode: TileMode, pos?: Array<number>|null, matrix?: Matrix|null): ShaderEffect

Creates a ShaderEffect object that generates a radial gradient based on the center and radius of a circle. A radial gradient refers to the color transition that spreads out gradually from the center of a circle.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
centerPtcommon2D.PointYesCenter of the circle.
radiusnumberYesRadius of the gradient. A negative number is invalid. The value is a floating point number.
colorsArray<number>YesArray of colors to distribute between the center and ending shape of the circle. The values in the array are 32-bit (ARGB) unsigned integers.
modeTileModeYesTile mode of the shader effect.
posArray<number> |nullNoRelative position of each color in the color array. The array length must be the same as that of colors. The first element in the array must be 0.0, the last element must be 1.0, and the middle elements must be between 0.0 and 1.0 and increase by index. The default value is null, indicating that colors are evenly distributed between the center and ending shape of the circle.
matrixMatrix |nullNoMatrix object used to perform matrix transformation on the shader effect. The default value is null, indicating the identity matrix.

RadialGradient

The preceding figure shows the display effect when the colors array is set to red, green, and blue and the pos array is set to 0.0, 0.75, and 1.0. The triangle subscript is the relative position of the color between the center and ending shape of the circle. Gradient colors are used between them.

Returns

TypeDescription
ShaderEffectShaderEffect object created.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types; 3. Parameter verification failed.

Example

import { common2D,drawing } from '@kit.ArkGraphics2D';

let centerPt: common2D.Point = { x: 100, y: 100 };
let shaderEffect = drawing.ShaderEffect.createRadialGradient(centerPt, 100, [0xFF00FF00, 0xFFFF0000], drawing.TileMode.REPEAT);

createSweepGradient12+

static createSweepGradient(centerPt: common2D.Point, colors: Array<number>, mode: TileMode, startAngle: number, endAngle: number, pos?: Array<number>|null, matrix?: Matrix|null): ShaderEffect

Creates a ShaderEffect object that generates a color sweep gradient around a given center point, either in a clockwise or counterclockwise direction.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
centerPtcommon2D.PointYesCenter of the circle.
colorsArray<number>YesArray of colors to distribute between the start angle and end angle. The values in the array are 32-bit (ARGB) unsigned integers.
modeTileModeYesTile mode of the shader effect.
startAnglenumberYesStart angle of the sweep gradient, in degrees. The value 0 indicates the positive direction of the X axis. A positive number indicates an offset towards the positive direction, and a negative number indicates an offset towards the negative direction. The value is a floating point number.
endAnglenumberYesEnd angle of the sweep gradient, in degrees. The value 0 indicates the positive direction of the X axis. A positive number indicates an offset towards the positive direction, and a negative number indicates an offset towards the negative direction. A value less than the start angle is invalid. The value is a floating point number.
posArray<number> |nullNoRelative position of each color in the color array. The array length must be the same as that of colors. The first element in the array must be 0.0, the last element must be 1.0, and the middle elements must be between 0.0 and 1.0 and increase by index. The default value is null, indicating that the colors are evenly distributed between the start angle and end angle.
matrixMatrix |nullNoMatrix object used to perform matrix transformation on the shader effect. The default value is null, indicating the identity matrix.

SweepGradient

The preceding figure shows the display effect when the colors array is set to red, green, and blue, the pos array is set to 0.0, 0.75, and 1.0, startAngle is set to 0 degrees, and endAngle is set to 180 degrees. In the figure, 0.0 corresponds to the position of 0 degrees, 0.75 corresponds to the position of 135 degrees, and 1.0 corresponds to the position of 180 degrees. Gradient colors are used between them.

Returns

TypeDescription
ShaderEffectShaderEffect object created.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types; 3. Parameter verification failed.

Example

import { common2D,drawing } from '@kit.ArkGraphics2D';

let centerPt: common2D.Point = { x: 100, y: 100 };
let shaderEffect = drawing.ShaderEffect.createSweepGradient(centerPt, [0xFF00FF00, 0xFFFF0000], drawing.TileMode.REPEAT, 100, 200);

createConicalGradient12+

static createConicalGradient(startPt: common2D.Point, startRadius: number, endPt: common2D.Point, endRadius: number, colors: Array<number>, mode: TileMode, pos?: Array<number>|null, matrix?: Matrix|null): ShaderEffect

Creates a ShaderEffect object that generates a conical gradient between two given circles.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
startPtcommon2D.PointYesCenter of the start circle of the gradient.
startRadiusnumberYesRadius of the start circle of the gradient. A negative number is invalid. The value is a floating point number.
endPtcommon2D.PointYesCenter of the end circle of the gradient.
endRadiusnumberYesRadius of the end circle of the gradient. A negative value is invalid. The value is a floating point number.
colorsArray<number>YesArray of colors to distribute between the start circle and end circle. The values in the array are 32-bit (ARGB) unsigned integers.
modeTileModeYesTile mode of the shader effect.
posArray<number> |nullNoRelative position of each color in the color array. The array length must be the same as that of colors. The first element in the array must be 0.0, the last element must be 1.0, and the middle elements must be between 0.0 and 1.0 and increase by index. The default value is null, indicating that colors are evenly distributed between the two circles.
matrixMatrix |nullNoMatrix object used to perform matrix transformation on the shader effect. The default value is null, indicating the identity matrix.

ConicalGradient

The preceding figure shows the display effect when the colors array is set to red, green, and blue and the pos array is set to 0.0, 0.5, and 1.0. The left part shows the drawing result when the start circle is not in the end circle, and the right part shows the drawing result when the start circle is in the end circle.

Returns

TypeDescription
ShaderEffectShaderEffect object created.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types; 3. Parameter verification failed.

Example

import { common2D,drawing } from '@kit.ArkGraphics2D';

let startPt: common2D.Point = { x: 100, y: 100 };
let endPt: common2D.Point = {x: 200, y: 200};
let shaderEffect = drawing.ShaderEffect.createConicalGradient(startPt, 100, endPt, 50, [0xFF00FF00, 0xFFFF0000], drawing.TileMode.REPEAT);

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 capi-drawing-gpu-context-h

openharmony 鸿蒙 capi-drawing-nativepixelmap-

openharmony 鸿蒙 capi-nativevsync-oh-nativevsync-expectedraterange

openharmony 鸿蒙 capi-drawing-oh-drawing-brush

openharmony 鸿蒙 capi-drawing-oh-drawing-image-info

openharmony 鸿蒙 capi-drawing-text-linetypography-h

openharmony 鸿蒙 capi-oh-nativeimage-oh-nativeimage

openharmony 鸿蒙 capi-drawing-typeface-h

openharmony 鸿蒙 capi-drawing-brush-h

openharmony 鸿蒙 capi-nativewindow

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/SN6Gw6jG