在HarmonyOS中,无法通过.clip属性实现所需效果。当.clip属性设置为true时,子组件超出当前组件范围的区域将不会响应绑定的手势事件;当设置为undefined时,系统将不再对超出部分进行裁剪,但这并不意味着可以控制内容超出组件本身的绘制范围。可以通过以下方案实现让Canvas绘制内容能超出父组件限制的功能,步骤如下:
当组件Canvas绑定了.responseRegion(Rect),所有落在Rect区域范围的触摸事件和手势可被组件Canvas对应的回调响应。
@Entry @Component struct DrawBankCom { paintSize: number = 5; // 当前画笔大小 private settings: RenderingContextSettings = new RenderingContextSettings(true); canvasContext: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings); tempPath: Path2D = new Path2D(); @State @Watch('onChange') pathArray: Array<Path2D> = []; // 所有画图路径信息 isUpdate: boolean = true; removeArray: Array<Path2D> = []; // 回退的路径集合 @State text: string = ''; @State eventType: string = ''; build() { Column() { if (this.isUpdate) { Row() { Column() { Canvas(this.canvasContext) // Canvas(子组件)宽高设置为300*300 .width(300) .height(300) .onReady(() => { this.canvasContext.lineWidth = this.paintSize; this.canvasContext.stroke(this.tempPath); for (let index = 0; index < this.pathArray.length; index++) { this.canvasContext.stroke(this.pathArray[index]); } }) .onTouch((event?: TouchEvent) => { if (event) { if (event.type === TouchType.Down) { this.eventType = 'Down'; this.canvasContext.lineWidth = this.paintSize; this.canvasContext.beginPath(); this.tempPath = new Path2D(); this.pathArray.push(this.tempPath); this.tempPath.moveTo(event.touches[0].x, event.touches[0].y); this.canvasContext.moveTo(event.touches[0].x, event.touches[0].y); } if (event.type === TouchType.Up) { this.eventType = 'Up'; } if (event.type === TouchType.Move) { this.eventType = 'Move'; this.tempPath.lineTo(event.touches[0].x, event.touches[0].y); this.canvasContext.stroke(this.tempPath); this.canvasContext.lineTo(event.touches[0].x, event.touches[0].y); this.canvasContext.stroke(); } this.text = 'TouchType:' + this.eventType + '\n touch point and touch element:\nx: ' + event.touches[0].x + '\n' + 'y: ' + event.touches[0].y + '\nwidth:' + event.target.area.width + '\nheight:' + event.target.area.height + '\pathArray size:' + this.pathArray.length; } }) .renderFit(RenderFit.TOP_RIGHT) // 设置触摸热区,蓝色区域可响应“down”操作,红色不可响应“down”操作,只能响应“move”操作 .responseRegion({ x: 75, y: 0, width: 150, height: 150 }); } // Column(父组件)宽高设置为150*150 .width(150) .height(150) .backgroundColor('#ffbdf3e9'); }; } Column() { Row() { Button('回退').onClick(() => { this.pathArray.pop(); }); }; Text(this.text); }; } .width(300) .height(300) .clip(false) .borderRadius(10) .backgroundColor('#ff0000'); } onChange() { this.canvasContext.reset(); this.canvasContext.lineWidth = this.paintSize; for (let index = 0; index < this.pathArray.length; index++) { this.canvasContext.stroke(this.pathArray[index]); } } }
具体表现为从蓝色区域开始画线,可以延伸到红色区域,但是不可从红色区域开始画线。