openharmony 鸿蒙 js-components-canvas-image

2025-06-12 浏览 (1)

Image

NOTE

This component is supported since API version 4. Updates will be marked with a superscript to indicate their earliest API version.

The Image object is an image on the canvas.

Attributes

NameTypeDefault ValueMandatoryDescription
srcstring-YesImage resource path.
width<length>0pxNoImage width.
height<length>0pxNoImage height.
onloadFunction-NoFunction called when an image is successfully loaded. This function has no parameter.
onerrorFunction-NoFunction called when an image fails to be loaded. This function has no parameter.

Example

<!-- xxx.hml -->
<div>
  <canvas ref="canvas" style="width: 500px; height: 500px; "></canvas>
</div>
// xxx.js
export default {
    onShow() {
        const el = this.$refs.canvas;
        var ctx = el.getContext('2d');
        var img = new Image();
        // It is recommended that the image be stored in the common directory.
        img.src = 'common/images/example.jpg';
        img.onload = function () {
            console.log('Image load success');
            ctx.drawImage(img, 0, 0, 360, 250);
        };
        img.onerror = function () {
            console.log('Image load fail');
        };
    }
}

1-9

你可能感兴趣的鸿蒙文章

harmony 鸿蒙JavaScript-compatible Web-like Development Paradigm (ArkUI.Full)

harmony 鸿蒙Data Type Attributes

harmony 鸿蒙button

harmony 鸿蒙chart

harmony 鸿蒙divider

harmony 鸿蒙image-animator

harmony 鸿蒙image

harmony 鸿蒙input

harmony 鸿蒙label

harmony 鸿蒙marquee

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