Repository navigation
Description
Context
#1200 (for #1111) adds per-draw image tinting to canvas.DrawImage on Apple, Windows, Linux, Android, the web and mini programs. Its HarmonyOS part was taken out before merging: on HarmonyOS a tinted draw currently goes out as a plain image draw, so the image is drawn untinted, and the docs and roadmap say HarmonyOS tinting is not supported yet.
The HarmonyOS change changes the template cmd/nsgo/harmony/templates/entry/src/main/ets/pages/Index.ets, so the committed host has to be rebuilt with cmd/nsgo/harmony/host/build.sh, otherwise TestCommittedHostMatchesTemplates fails. That rebuild needs hvigorw and the HarmonyOS API 26 SDK, which the maintainers' build machines don't have right now (one has DevEco with API 22 only).
Why it matters
The same canvas code draws differently on HarmonyOS than on every other target, silently: a tinted sprite or icon shows its original colors.
Proposal
Restore the removed change (patch below) and rebuild the committed host:
internal/platform/harmonyos/canvas.go: addTint *uint32(json:"tint,omitempty") tocanvasOp;canvasJSONsets it fromuint32(o.Color)forDrawImageops witho.Tinted.Index.ets: addtint?: numbertoCanvasOp;CanvasViewkeeps a one-entry cache (tintSource,tintColor,tintImage) and atintedImage(pm, tint)that draws the PixelMap into anOffscreenCanvasRenderingContext2Din pixel units, multiplies each RGBA byte by the tint's channels over 255 throughgetImageData/putImageData, and returnsgetPixelMap(); the image op uses the tinted map unlesstintis undefined or0xffffffff.
Patch removed from #1200
diff --git a/cmd/nsgo/harmony/templates/entry/src/main/ets/pages/Index.ets b/cmd/nsgo/harmony/templates/entry/src/main/ets/pages/Index.ets
index 832856c1e..5ddebd2fa 100644
--- a/cmd/nsgo/harmony/templates/entry/src/main/ets/pages/Index.ets
+++ b/cmd/nsgo/harmony/templates/entry/src/main/ets/pages/Index.ets
@@ -8,7 +8,7 @@ import { camera, cameraPicker } from '@kit.CameraKit';
import { fileIo as fs, fileUri, picker } from '@kit.CoreFileKit';
import { accessibility } from '@kit.AccessibilityKit';
import { formProvider, formBindingData } from '@kit.FormKit';
-import { display, window, typeNode, BuilderNode, NodeController, FrameNode, UIContext, componentSnapshot } from '@kit.ArkUI';
+import { display, window, typeNode, BuilderNode, NodeController, FrameNode, UIContext, componentSnapshot, LengthMetricsUnit } from '@kit.ArkUI';
import { displaySync } from '@kit.ArkGraphics2D';
import { common, ConfigurationConstant, abilityAccessCtrl, bundleManager } from '@kit.AbilityKit';
import type { Permissions, PermissionRequestResult } from '@kit.AbilityKit';
@@ -159,6 +159,7 @@ interface TextRun {
interface CanvasOp {
k: number;
imgd?: string;
+ tint?: number;
x?: number;
y?: number;
x1?: number;
@@ -2175,6 +2176,9 @@ struct CanvasView {
// images are the pictures drawings draw, decoded once; a repaint runs
// when one arrives.
private images: Map<string, image.PixelMap> = new Map();
+ private tintSource: image.PixelMap | undefined;
+ private tintColor: number = 0;
+ private tintImage: image.PixelMap | undefined;
private loading: Set<string> = new Set();
private missing: Set<string> = new Set();
@@ -2197,6 +2201,34 @@ struct CanvasView {
ctx.closePath();
}
+ // tintedImage multiplies the source's RGBA pixels in a separate canvas.
+ private tintedImage(pm: image.PixelMap, tint: number): image.PixelMap {
+ if (this.tintSource === pm && this.tintColor === tint && this.tintImage !== undefined) {
+ return this.tintImage;
+ }
+ const size = pm.getImageInfoSync().size;
+ const ctx = new OffscreenCanvasRenderingContext2D(size.width, size.height,
+ new RenderingContextSettings(true), LengthMetricsUnit.PX);
+ ctx.drawImage(pm, 0, 0, size.width, size.height);
+ const pixels = ctx.getImageData(0, 0, size.width, size.height);
+ const r = tint >>> 24;
+ const g = (tint >>> 16) & 255;
+ const b = (tint >>> 8) & 255;
+ const a = tint & 255;
+ for (let i = 0; i < pixels.data.length; i += 4) {
+ pixels.data[i] = Math.round(pixels.data[i] * r / 255);
+ pixels.data[i + 1] = Math.round(pixels.data[i + 1] * g / 255);
+ pixels.data[i + 2] = Math.round(pixels.data[i + 2] * b / 255);
+ pixels.data[i + 3] = Math.round(pixels.data[i + 3] * a / 255);
+ }
+ ctx.putImageData(pixels, 0, 0);
+ const tinted = ctx.getPixelMap(0, 0, size.width, size.height);
+ this.tintSource = pm;
+ this.tintColor = tint;
+ this.tintImage = tinted;
+ return tinted;
+ }
+
// drawImage draws an image op: at its natural size when W and H are 0,
// else into that rectangle, from the part X1, Y1, X2, Y2 of it when the
// source rectangle is given. The op is in points and the pixels are not.
@@ -2450,7 +2482,8 @@ struct CanvasView {
this.loadImage(key, uri, data);
break;
}
- this.drawImage(ctx, pm, o);
+ const src = o.tint === undefined || o.tint === 0xffffffff ? pm : this.tintedImage(pm, o.tint);
+ this.drawImage(ctx, src, o);
break;
}
}
diff --git a/internal/platform/harmonyos/canvas.go b/internal/platform/harmonyos/canvas.go
index 483c718f1..4817b0cd8 100644
--- a/internal/platform/harmonyos/canvas.go
+++ b/internal/platform/harmonyos/canvas.go
@@ -43,7 +43,8 @@ type canvasOp struct {
D []float32 `json:"d,omitempty"`
Img string `json:"img,omitempty"`
// ImgData is the image's bytes, base64, when the drawing embeds them.
- ImgData string `json:"imgd,omitempty"`
+ ImgData string `json:"imgd,omitempty"`
+ Tint *uint32 `json:"tint,omitempty"`
}
// gradient is a canvas gradient as the host builds one: radial or linear,
@@ -90,6 +91,10 @@ func canvasJSON(ops []driver.DrawOp) string {
c.D = append([]float32(nil), (*o.Dash)...)
}
case driver.DrawImage:
+ if o.Tinted {
+ tint := uint32(o.Color)
+ c.Tint = &tint
+ }
if o.Image != nil {
c.Img = o.Image.URI
if len(o.Image.Data) > 0 {Acceptance
TestCommittedHostMatchesTemplatespasses with the rebuilt host committed.TestCanvasImageTintpasses on a HarmonyOS emulator or device.- The HarmonyOS exceptions in the canvas docs (en/zh),
docs/architecture/canvas.md,skills/nsgo/references/components.mdand the roadmap row are removed.
Activity
houko commentedon Nov 23, 2026
Landed in #2016, which applies this patch and the committed host rebuild: HarmonyOS tints canvas images from bytes or the app's resources.
Remaining, device-only: the shared tint pixel test's
encodedcase on a HarmonyOS emulator or device (roadmap row "Canvas image tint").