使用CanvasRenderingContext2D.stroke绘制path时,重合的半透明笔迹会叠加。如何实现颜色不叠加效果?颜色叠加代码如下:
@Entry @Component struct Page { private settings: RenderingContextSettings = new RenderingContextSettings(true); private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings); build() { Column() { Canvas(this.context) .onReady(() => { this.context.lineWidth = 40; this.context.strokeStyle = '#5164fff0'; this.context.moveTo(80, 20); this.context.lineTo(80, 220); this.context.stroke(); this.context.beginPath(); this.context.moveTo(20, 80); this.context.lineTo(220, 80); this.context.stroke(); this.context.beginPath(); this.context.moveTo(160, 20); this.context.lineTo(160, 220); this.context.stroke(); this.context.beginPath(); this.context.moveTo(20, 160); this.context.lineTo(220, 160); this.context.stroke(); }); } .height('100%') .width('100%'); } }
问题效果预览:

在绘制过程中如果让多个路径分别绘制,会让绘制出的图案颜色多次覆盖。如果是绘制用的颜色为半透明,颜色会进行叠加直到不透明。如果想让绘制的颜色不叠加有以下两种方式。
@Entry @Component struct Page { private settings: RenderingContextSettings = new RenderingContextSettings(true); private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings); build() { Column() { Canvas(this.context) .onReady(() => { this.context.lineWidth = 40; this.context.strokeStyle = '#5164fff0'; this.context.moveTo(80, 20); this.context.lineTo(80, 220); this.context.moveTo(20, 80); this.context.lineTo(220, 80); this.context.moveTo(160, 20); this.context.lineTo(160, 220); this.context.moveTo(20, 160); this.context.lineTo(220, 160); this.context.stroke(); }); } .height('100%') .width('100%'); } }
@Entry @Component struct Page2 { private settings: RenderingContextSettings = new RenderingContextSettings(true); private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings); build() { Column() { Canvas(this.context) .onReady(() => { // 第一段,左边的垂直向下路径 this.context.lineWidth = 40; this.context.strokeStyle = '#5164fff0'; this.context.moveTo(80, 20); this.context.lineTo(80, 220); this.context.stroke(); // 第二段,上面的水平向右路径 this.context.beginPath(); this.context.moveTo(20, 80); this.context.lineTo(220, 80); this.context.globalCompositeOperation = 'destination-out'; this.context.strokeStyle = '#ff000000'; this.context.stroke(); // 去除路径上原有的颜色 this.context.globalCompositeOperation = 'source-over'; this.context.strokeStyle = '#5164fff0'; this.context.stroke(); // 绘制路径 // 第三段,左边的垂直向下路径 this.context.beginPath(); this.context.moveTo(160, 20); this.context.lineTo(160, 220); this.context.globalCompositeOperation = 'destination-out'; this.context.strokeStyle = '#ff000000'; this.context.stroke(); // 去除路径上原有的颜色 this.context.globalCompositeOperation = 'source-over'; this.context.strokeStyle = '#5164fff0'; this.context.stroke(); // 绘制路径 // 第四段,下面的水平向右路径 this.context.beginPath(); this.context.moveTo(20, 160); this.context.lineTo(220, 160); this.context.globalCompositeOperation = 'destination-out'; this.context.strokeStyle = '#ff000000'; this.context.stroke(); // 去除路径上原有的颜色 this.context.globalCompositeOperation = 'source-over'; this.context.strokeStyle = '#5164fff0'; this.context.stroke(); // 绘制路径 }); } .height('100%') .width('100%'); } }