鸿蒙OpenHarmony开发实战:【MiniCanvas】

电子说

1.3w人已加入

描述

介绍

基于OpenHarmony的Cavas组件封装了一版极简操作的MiniCanvas,屏蔽了原有Canvas内部复杂的调用流程,支持一个API就可以实现相应的绘制能力,该库还在继续完善中,也欢迎PR。

使用说明

  1. 添加MiniCanvas依赖
    在项目entry目录执行如下命令安装MiniCanvas库:
    npm install git+https://gitee.com/ark-ui/MiniCanvas.git
    
  2. 引入MiniCanvas
    import { MiniCanvas, Paint, ICanvas } from '@ohos/mini_canvas'
    
  3. 使用MiniCanvas
    @Entry @Component struct TestMiniCanvas {
      build() {
        Column() {
          MiniCanvas({
            onDraw: (canvas) = > {
    
            }
          })
        }
        .size({width: "100%", height: "100%"})
      }
    }
    
  4. MiniCanvas绘制更多鸿蒙学习知识,可+mau123789,记住是v喔
    // import { MiniCanvas, Paint, ICanvas } from '@ohos/mini_canvas'
    // 源码方式引入
    import { MiniCanvas, Paint } from "./mini_canvas"
    
    @Entry @Component struct TestMiniCanvas {
      build() {
        Column() {
          MiniCanvas({
            // 在onDraw()方法内执行绘制
            onDraw: (canvas) = > {
              // 创建画笔
              let paint = new Paint()
    
              // 绘制直线
              paint.setColor("#FF0000");
              paint.setStrokeWidth(5);
              canvas.drawLine(10, 10, 280, 10, paint);
    
              // 绘制圆以及圆环
              canvas.drawCircle(50, 50, 25, paint);
              paint.setStroke(true);
              paint.setStrokeWidth(3);
              canvas.drawCircle(250, 50, 25, paint);
    
              // 绘制椭圆以及椭圆环
              paint.setStroke(false);
              canvas.drawOval(20, 100, 150, 50, paint)
              paint.setStroke(true);
              paint.setColor(Color.Pink.toString())
              canvas.drawOval(190, 100, 150, 50, paint)
    
              // 绘制矩形
              paint.setStroke(false)
              canvas.drawRect(20, 180, 150, 50, paint)
              paint.setStroke(true)
              paint.setStrokeWidth(5)
              canvas.drawRect(190, 180, 150, 50, paint)
    
              // 绘制圆角矩形
              paint.setStroke(false);
              canvas.drawRoundRect(20, 250, 150, 50, 10, paint)
              paint.setStroke(true);
              canvas.drawRoundRect(190, 250, 150, 50, 10, paint)
    
    
              // 绘制圆弧
              canvas.drawArc(80, 330, 40, 0, 135, paint);
              paint.setStroke(false);
              canvas.drawArc(250, 330, 40, 0, 135, paint);
    
              // 绘制图片
              let bitmap = new ImageBitmap("pages/test.jpg")
              canvas.drawImage(bitmap, 10, 10, 800, 600, 20, 400, 320, 160, paint)
            },
            // 设置画布的属性
            attribute: {
              id: "mini_canvas",
              width: "100%",
              height: "100%",
              background: "#ffffff",
              clickListener: (event) = > {
                console.log("onClicked: " + JSON.stringify(event));
              },
              touchListener: (event) = > {
                console.log("onTouched: " + JSON.stringify(event));
              }
            },
          })
        }
        .size({width: "100%", height: "100%"})
      }
    }
    

审核编辑 黄宇

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分