开天辟地 HarmonyOS(鸿蒙) - canvas: 通过 CanvasRenderingContext2D 绘制图形(文本,图像,变换,透明,阴影)

源码 https://github.com/webabcd/HarmonyDemo
作者 webabcd

开天辟地 HarmonyOS(鸿蒙) - canvas: 通过 CanvasRenderingContext2D 绘制图形(文本,图像,变换,透明,阴影)

示例如下:

pages\canvas\CanvasRenderingContext2DDemo2.ets

/** CanvasRenderingContext2D - 按照 W3C 标准封装了本地绘图接口(效率比 DrawingRenderingContext 低)** 本例用于演示如何通过 CanvasRenderingContext2D 绘制文本,图像,变换,透明,阴影*/import { MyLog, TitleBar } from '../TitleBar';
import { LengthMetricsUnit } from '@kit.ArkUI';@Entry
@Component
struct CanvasRenderingContext2DDemo2 {build() {Column() {TitleBar()Tabs() {TabContent() { MySample1() }.tabBar('文本').align(Alignment.Top)TabContent() { MySample2() }.tabBar('图像').align(Alignment.Top)TabContent() { MySample3() }.tabBar('变换').align(Alignment.Top)TabContent() { MySample4() }.tabBar('透明').align(Alignment.Top)TabContent() { MySample5() }.tabBar('阴影').align(Alignment.Top)}.scrollable(true).barMode(BarMode.Scrollable).layoutWeight(1)}}
}// 文本
@Component
struct MySample1 {private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(new RenderingContextSettings(true))text = "hello webabcd"@State message: string = ""build() {Column({ space: 10 }) {Text(this.message)/** CanvasRenderingContext2D - 按照 W3C 标准封装了本地绘图接口*   fillStyle - 填充颜色*   strokeStyle - 画笔颜色*   lineWidth - 画笔宽度**   font - 字体*     格式为 'font-style font-weight font-size font-family’*   fillText() - 用 fillStyle 填充文本*     参数按顺序分别为: 文本内容, 文本左下角的 x, y, 最大宽度(可选参数,不指定则不限制宽度)*   strokeText() - 用 strokeStyle 描边文本*     参数按顺序分别为: 文本内容, 文本左下角的 x, y, 最大宽度(可选参数,不指定则不限制宽度)*   measureText() - 测量指定的文本内容在当前上下文中绘制后的尺寸*   textBaseline - 文本的基线对齐方式*     alphabetic(默认值), top, hanging, middle, ideographic, bottom*   textAlign - 文本的对齐方式*     left, right, center, start(默认值), end*/Canvas(this.context).width('100%').height('100%').backgroundColor(Color.Yellow).onReady(() => {this.context.fillStyle = Color.Redthis.context.strokeStyle = Color.Greenthis.context.lineWidth = 2this.context.font = '24vp'this.context.fillText(this.text, 0, 50, 2000)this.context.font = 'italic 400 24vp HarmonyOS Sans'this.context.fillText(this.text, 0, 100)this.message = `measureText width: ${this.context.measureText(this.text).width}` // 指定的文本内容在当前上下文中绘制后的宽this.message = `measureText height: ${this.context.measureText(this.text).height}` // 指定的文本内容在当前上下文中绘制后的高this.context.font = '24vp'this.context.strokeText("ABC", 0, 150)this.context.moveTo(0, 200)this.context.lineTo(this.context.width, 200)this.context.stroke()this.context.font = '12vp'this.context.textBaseline = 'top'this.context.fillText('top', 0, 200)this.context.textBaseline = 'bottom'this.context.fillText('bottom', 40, 200)this.context.textBaseline = 'middle'this.context.fillText('middle', 100, 200)this.context.textBaseline = 'alphabetic'this.context.fillText('alphabetic', 160, 200)this.context.textBaseline = 'hanging'this.context.fillText('hanging', 240, 200)this.context.textBaseline = 'ideographic'this.context.fillText('ideographic', 300, 200)this.context.moveTo(this.context.width / 2, 250)this.context.lineTo(this.context.width / 2, 550)this.context.stroke()this.context.font = '12vp'this.context.textAlign = 'start'this.context.fillText('textAlign=start', this.context.width / 2, 300)this.context.textAlign = 'end'this.context.fillText('textAlign=end', this.context.width / 2, 350)this.context.textAlign = 'left'this.context.fillText('textAlign=left', this.context.width / 2, 400)this.context.textAlign = 'center'this.context.fillText('textAlign=center', this.context.width / 2, 450)this.context.textAlign = 'right'this.context.fillText('textAlign=right', this.context.width / 2, 500)})}}
}// 图像
@Component
struct MySample2 {private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(new RenderingContextSettings(true))/** ImageBitmap - 图像*   src - 图像地址*   unit - 一个 LengthMetricsUnit 枚举*     DEFAULT - 单位为 vp*     PX - 单位为 px*/private imageBitmap = new ImageBitmap("resources/base/media/son.jpg", LengthMetricsUnit.DEFAULT)build() {Column({ space: 10 }) {/** CanvasRenderingContext2D - 按照 W3C 标准封装了本地绘图接口*   imageSmoothingEnabled - 绘制图片时是否进行图像平滑度调整(默认值为 true)*   imageSmoothingQuality - 图像平滑度(当 imageSmoothingEnabled 为 true 时有效)*     low(默认值), medium, high**   drawImage() - 绘制图像*   getPixelMap(), setPixelMap() - 获取和设置 PixelMap 对象*   getImageData(), putImageData() - 获取和设置 ImageData 对象*   createImageData() - 创建 ImageData 对象** PixelMap - 无压缩位图,支持各种格式的图片转为 PixelMap,也支持 PixelMap 转为各种格式的图片,支持图片的剪裁、缩放、旋转等操作* ImageData - 无压缩位图,用于存储或设置 canvas 上的图像*/Canvas(this.context).width('100%').height('100%').backgroundColor(Color.Yellow).onReady(() => {this.context.imageSmoothingEnabled = truethis.context.imageSmoothingQuality = 'low'// drawImage() - 绘制图像,参数按顺序分别是//   图像, 绘制区域左上角的 x, y//   图像, 绘制区域左上角的 x, y, width, height//   图像, 对源图像做剪裁的左上角的 x, y, width, height, 将剪裁后的图像放到的绘制区域的左上角的 x, y, width, height,this.context.drawImage(this.imageBitmap, 0, 0)this.context.drawImage(this.imageBitmap, 80, 0, 100, 100)this.context.drawImage(this.imageBitmap, this.imageBitmap.width / 2 , this.imageBitmap.height / 2, this.imageBitmap.width / 2 , this.imageBitmap.height / 2, 200, 0, 100, 100)this.context.drawImage(this.imageBitmap, 0, 120, 100, 100)// getImageData() - 获取指定区域的 ImageData 对象,参数按顺序分别是//   需要获取的区域的左上角的 x, y, width, height// putImageData - 将 ImageData 对象绘制到指定的区域,参数按顺序分别是//   ImageData 对象, 绘制区域左上角的 x, y//   ImageData 对象, 绘制区域左上角的 x, y, 对源图像做剪裁的左上角的 x, y, width, heightlet data = this.context.getImageData(0, 120, 100, 100)this.context.putImageData(data, 0, 240)this.context.putImageData(data, 120, 240, 50, 50, 50, 50)// createImageData() - 创建 ImageData 对象//   参数按顺序分别是图像的宽和高//   每 4 个字节描述一个像素点,这 4 个字节分别指的是 r, g, b, alet myData = this.context.createImageData(100, 100)for (let i = 0; i < myData.data.length; i += 4) {myData.data[i + 0] = 0myData.data[i + 1] = 0myData.data[i + 2] = 255myData.data[i + 3] = 255}this.context.putImageData(myData, 0, 360)})}}
}// 变换
@Component
struct MySample3 {private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(new RenderingContextSettings(true))build() {Column({ space: 10 }) {/** CanvasRenderingContext2D - 按照 W3C 标准封装了本地绘图接口*   translate() - 位移*   scale() - 缩放*   rotate() - 旋转(单位:弧度)*   transform() - 在原有 transform 的基础上叠加一个 transform*     参数按顺序分别为: scaleX, skewY, skewX, scaleY, translateX, translateY*   resetTransform() - 重置 transform*   setTransform() - 清除原有 transform 并设置一个新的 transform*     参数按顺序分别为: scaleX, skewY, skewX, scaleY, translateX, translateY*     也可以设置一个 Matrix2D 对象*   getTransform() - 获取当前上下文的 Matrix2D 对象** Matrix2D - 矩阵*   identity() - 获取单位矩阵*   invert() - 获取当前矩阵的逆矩阵*   translate() - 位移*   scale() - 缩放*   rotate() - 旋转(参数按顺序分别为: 旋转的弧度, 旋转中心点的 x, y)*/Canvas(this.context).width('100%').height('100%').backgroundColor(Color.Yellow).onReady(() => {this.context.lineWidth = 2this.context.strokeStyle = Color.Redthis.context.strokeRect(0, 0, 50, 50)this.context.strokeStyle = Color.Greenthis.context.translate(0, 60)this.context.scale(2, 2)this.context.rotate(5 * Math.PI / 180)this.context.strokeRect(0, 0, 50, 50)this.context.strokeStyle = Color.Bluethis.context.transform(2 ,0, 0, 2, 0, 60)this.context.strokeRect(0, 0, 50, 50)this.context.strokeStyle = Color.Orangethis.context.setTransform(2 ,0, 0, 2, 200, 0)this.context.strokeRect(0, 0, 50, 50)this.context.fillStyle = Color.Pinklet matrix: Matrix2D = new Matrix2D().identity()matrix = matrix.identity()matrix.rotate(5 * Math.PI / 180, 25, 25)matrix.translate(0, 400)matrix.scale(2, 2)this.context.setTransform(matrix)this.context.fillRect(0, 0, 50, 50)})}}
}// 透明
@Component
struct MySample4 {private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(new RenderingContextSettings(true))build() {Column({ space: 10 }) {/** CanvasRenderingContext2D - 按照 W3C 标准封装了本地绘图接口*   globalAlpha - 不透明度*/Canvas(this.context).width('100%').height('100%').backgroundColor(Color.Yellow).onReady(() => {this.context.fillStyle = Color.Redthis.context.fillRect(0, 0, 100, 100)this.context.globalAlpha = 0.3this.context.fillRect(0, 100, 100, 100)})}}
}// 阴影
@Component
struct MySample5 {private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(new RenderingContextSettings(true))build() {Column({ space: 10 }) {/** CanvasRenderingContext2D - 按照 W3C 标准封装了本地绘图接口*   shadowBlur - 阴影的模糊级别,值越大越模糊*   shadowColor - 阴影的颜色*   shadowOffsetX - 阴影的 x 轴方向的偏移距离*   shadowOffsetY - 阴影的 y 轴方向的偏移距离*/Canvas(this.context).width('100%').height('100%').backgroundColor(Color.Yellow).onReady(() => {this.context.shadowBlur = 30this.context.shadowColor = "#ff0000"this.context.shadowOffsetX = 20this.context.shadowOffsetY = 20this.context.fillStyle = Color.Greenthis.context.fillRect(50, 50, 100, 100)})}}
}

源码 https://github.com/webabcd/HarmonyDemo
作者 webabcd

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/902517.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

harmony OS NEXT-TypeScript

TS基础内容 1.1 TypeScript简介TypeScript是由微软开发,是基于JavaScript的一个扩展语言 TypeScript包含了JavaScript的所有内容,即:TypeScript是JavaScript的超能 TypeScript增加了:静态类型检查、接口、泛型等很多现代开发特性,因此适合更大型的项目开发 TypeScript需要…

Unreal最佳实践——TSharedRef和Ptr的使用和坑

A:管理比较麻烦的时候用,像slate这种用得就多 A:这种东西能不用还是别用,性能肯定是低的(低不了多少),还有一个是要注意循环引用 A:写一个ptr记得在destruction里面加一个,或者endplay加一下,就能避免泄漏问题 养成习惯就行了

用户说 | 零基础用通义灵码 AI 程序员开发个人笔记网站

通义灵码是一款基于通义大模型的智能编码辅助工具,支持自然语言生成代码、单元测试生成、代码注释生成等功能,兼容多种主流IDE和编程语言。对于零基础用户,只需通过自然语言描述需求,通义灵码即可自动生成代码,帮助快速开发个人笔记网站,极大简化开发流程,提升效率。作者…

harmony OS NEXT-通过用户首选项实现数据持久化

鸿蒙通过用户首选项实现数据持久化 1.1 场景介绍 用户首选项为应用提供Key-Value键值型的数据处理能力,支持应用持久化轻量级数据,并对其修改和查询。当用户希望有一个全局唯一存储的地方,可以采用用户首选项来进行存储。Preferences会将该数据缓存在内存中,当用户读取的时…

harmony OS NEXT-启动页开发

鸿蒙启动页开发 1.1 更改应用名称和图标 1.更改应用图标找到moudle.json5文件,找到应用启动的EntryAbility下面的icon,将原来的图标改成自己设置的即可2.更改应用名称3.效果展示2.1 广告页面开发3.1 详细介绍 3.1.1 启动页面 import { PrivacyDialog } from ../views/componen…

harmony OS NEXT–状态管理器–@State详解

鸿蒙Harmony--状态管理器--@State详解 1.1 定义 @State装饰的变量,或者称为状态变量,一旦变量拥有了状态属性,就可以触发其直接绑定UI组件的刷新。当状态改变时,UI会发生对应的渲染变化 ,@State装饰的变量,与声明式范式中的其他被装饰变量一样,是私有的,只能从组件内部访问。在…

harmony OS NEXTUI开发

UI开发 1. 布局概述 1.1 开发流程1.先确定开发流程 -> 2.分析页面元素构成 ->3.选用合适的布局容器组件1.3 布局元素组成:盒模型2.1 布局分类 2.1 线性布局线性布局是开发中最常用、最基础的布局,通过线性容器Row和Column构建2.1.1 线性布局子元素排列方向:主轴布局子…

harmony OS NEXT组件结构

组件结构 1. 认识基本的组件结构ArkTS通过装饰器@Component 和@Entry 装饰 struct 关键字声明的数据结构,构成一个自定义组件 自定义组件中提供了一个build函数,开发者需要在函数内以链式调用的方式进行基本的UI描述,UI描述的方法请参考UI描述规范srtuct-自定义组件基于stru…

如何使用 vxe-table 来实现高亮行与高亮列,单元格选择高亮移动功能

如何使用 vxe-table 来实现高亮行与高亮列,单元格选择高亮移动功能 查看官网:https://vxetable.cn gitbub:https://github.com/x-extends/vxe-table gitee:https://gitee.com/x-extends/vxe-table 效果代码 通过 rowConfig.isCurrent 启用高亮行功能,columnConfig.isCurre…

Browser-use探索-webui

UI化的Browser-use 拉项目下来:git clone https://github.com/browser-use/web-ui.git # 拉取项目依次安装依赖:# pip install browser-use# playwright install# pip install -r requirements.txt 重新搞个配置文件.env:OPENAI_ENDPOINT=https://api.openai.com/v1 OPENAI_…

Web 页面实现图片放大镜效果

Web 页面实现图片放大镜效果Web 页面实现图片放大镜效果 下面是一个完整的示例代码: <!DOCTYPE html> <html><head><meta charset="UTF-8" /><title>图片放大镜效果</title><style>.container {position: relative;width…

3.A+B 问题三

用while(hasNext())来判断是非还有输入 后面用if判断结束循环的条件 break 结束循环; continue 跳过本次循环 进入下一次循环