基于Canvas实现选座功能鸿蒙示例代码

本文原创发布在华为开发者社区

介绍

本示例基于Canvas组件,构建了一个有关电影院购票选座场景,实现了选座/退座功能。

基于Canvas实现选座功能源码链接

效果预览

使用说明

1.应用启动时展示一个10×10的正方形,每个小正方形相当于一个座位;

2.点击任何一个正方形,该正方形变成黄色,表示该座位被选中,座位号记录下来,选中座位数量增加;

3.点击选中按钮,弹出对话框,显示选中的座位,提示你是否确认支付,如果确认,选中的座位就会 变成红色,如果取消,对话框消失。之后还能正常选择;

4.如果点击清空,所有选中的和确认的座位都变成灰色,恢复到初始状态。

实现思路

  1. 使用canvas组件画图,开始进入页面时,画一个10×10的正方形,同时使用seats记录座位是否被选中,默认值0表示没选中。
typescript 复制代码
   Column({ space: 20 }) {
      Canvas(this.context)
        .width('100%')
        .height('70%')
        .onReady(() => {
          this.context.fillStyle = '#ccc'
          for (let i = 0; i < 10; i++) {
            for (let j = 0; j < 10; j++) {
              this.context.fillRect(40 * i, 10 + 40 * j, 30, 30)
            }
          }
        })
typescript 复制代码
  aboutToAppear(): void {
    for (let i = 0; i < this.HEIGHT; i++) {
      this.seats.push([])
      for (let j = 0; j < this.WIDTH; j++) {
        this.seats[i].push(0)
      }
    }
  }
  1. 定义drawRect函数,用来画图,id为0画笔是灰色,id为1是黄色,id是2表示红色,同时给canvas画布绑定点击时间,算出点击的座位号,然后把该座位变成黄色,改变seats对应的座位号变成1,记录选中的座位号。
typescript 复制代码
   Canvas(this.context)
        .width('100%')
        .height('70%')
        .onReady(() => {
          this.context.fillStyle = '#ccc'
          for (let i = 0; i < 10; i++) {
            for (let j = 0; j < 10; j++) {
              this.context.fillRect(40 * i, 10 + 40 * j, 30, 30)
            }
          }
        })
        .onClick((e: ClickEvent) => {
          this.handleClick(e)
        })
typescript 复制代码
    handleClick(e: ClickEvent) {
    let x: number = Math.floor(e.displayX / 40)
    let y: number = Math.floor((e.displayY - 10) / 40) - 1
    if (x >= 0 && x < this.HEIGHT && y >= 0 && y < this.WIDTH) {
      if (this.seats[x][y] === 1) {
        promptAction.showToast({
          message: '该座位已经选中,请选择其他座位'
        })
        return
      } else if (this.seats[x][y] === 2) {
        promptAction.showToast({
          message: '该座位已经确认,请选择其他座位'
        })
        return
      } else {
        this.drawRect(x, y, 1)
      }
    }
  }
typescript 复制代码
   drawRect(x: number, y: number, id: number) {
    if (id === 0) {
      this.context.fillStyle = '#ccc'
    } else if (id === 1) {
      this.context.fillStyle = '#ff0'
      this.seats[x][y] = 1
      this.count++
      this.selectedSeats.push([x, y])
    } else {
      this.context.fillStyle = '#f00'
    }
    this.context.fillRect(40 * x, 10 + 40 * y, 30, 30)
  }
  1. 点击选中座位按钮,弹出对话框。
typescript 复制代码
  Button(this.count ? `已选中${this.count}个座位` : '没有选中座位')
        .onClick(() => {
          if (this.dialogController != null && this.count !== 0) {
            this.dialogController.open()
          } else if (this.count === 0) {
            promptAction.showToast({
              message: '你还没选择座位,请选择座位'
            })
          }
        })
  1. 点击选中座位按钮,弹出对话框。
typescript 复制代码
 confirm() {
    for (let i = 0; i < this.selectedSeats.length; i++) {
      this.drawRect(this.selectedSeats[i][0], this.selectedSeats[i][1], 2)
      this.seats[this.selectedSeats[i][0]][this.selectedSeats[i][1]] = 2
    }
    this.count = 0
    this.selectedSeats = []
  }
  1. 点击清空,所有的座位都变成灰色,同时记录也清空。
相关推荐
前端菜鸟日常11 小时前
鸿蒙版(ArkTs) 贪吃蛇,包含无敌模式 最高分 暂停和继续功能
华为·harmonyos
鸿蒙布道师16 小时前
鸿蒙NEXT开发数值工具类(TS)
android·ios·华为·harmonyos·arkts·鸿蒙系统·huawei
塞尔维亚大汉21 小时前
鸿蒙南向开发 ——轻量系统芯片移植指南(二)
物联网·嵌入式·harmonyos
李元豪21 小时前
华为面试,机器学习深度学习知识点:
机器学习·华为·面试
别说我什么都不会1 天前
OpenHarmony内核系统调用hats测试用例编写指南
物联网·嵌入式·harmonyos
90后的晨仔1 天前
鸿蒙ArkTS是如何实现并发的?
harmonyos
鸿蒙布道师1 天前
鸿蒙NEXT开发日期工具类(ArkTs)
android·ios·华为·harmonyos·arkts·鸿蒙系统·huawei
Fanmeang1 天前
DHCP Snooping各种场景实验案例
运维·网络·安全·华为·交换机·dhcp·dhcp snooping
HMSCore1 天前
在应用内购票、寄件时,如何一键填充所需信息?
harmonyos
嘿嘿-g1 天前
华为IP(4)
服务器·网络·华为