HarmonyOS 鸿蒙Next设置为横屏后 Canvas的PanGesture onAction绘画动作失效
HarmonyOS 鸿蒙Next设置为横屏后 Canvas的PanGesture onAction绘画动作失效
<markdown _ngcontent-hye-c237="" class="markdownPreContainer">
private settings: RenderingContextSettings = new RenderingContextSettings(true);
private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings);
onPageShow() {
let orientation = window.Orientation.LANDSCAPE;
try {
globalThis.windowClass.setPreferredOrientation(orientation, (err:ESObject) => {});
} catch (exception) {
console.error('设置失败: ’ + JSON.stringify(exception));
}
}
Canvas(this.context)
.width(‘100%’)
.height(‘100%’)
.backgroundColor(’#f12222’)
.onReady(() => {
this.context.lineWidth = 3;
this.context.strokeStyle = “#000”;
this.context.strokeRect(50, 50, 200, 150);
})
.gesture(PanGesture(<span class="hljs-keyword"><span class="hljs-keyword">new</span></span> PanGestureOptions({ direction: PanDirection.All, distance: <span class="hljs-number"><span class="hljs-number">1</span></span> }))
.onActionStart((event: GestureEvent) => {
<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.isDown = <span class="hljs-literal"><span class="hljs-literal">true</span></span>;
<span class="hljs-keyword"><span class="hljs-keyword">const</span></span> finger = event.fingerList[<span class="hljs-number"><span class="hljs-number">0</span></span>]
<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.lastX = finger.localX;
<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.lastY = finger.localY;
<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.context.beginPath();
<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.paths.push({ x: finger.localX, y: finger.localY } as point );
})
.onActionUpdate((event: GestureEvent) => {
<span class="hljs-keyword"><span class="hljs-keyword">if</span></span> (<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.isDown) {
<span class="hljs-keyword"><span class="hljs-keyword">const</span></span> finger = event.fingerList[<span class="hljs-number"><span class="hljs-number">0</span></span>]
<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.draw(<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.lastX, <span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.lastY, finger.localX, finger.localY);
<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.lastX = finger.localX;
<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.lastY = finger.localY;
<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.paths.push({ x: finger.localX, y: finger.localY } as point);
}
})
.onActionEnd(() => {
<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.isDown = <span class="hljs-literal"><span class="hljs-literal">false</span></span>;
<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.undoStack.push([...this.paths]);
<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.paths = [];
<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.context.closePath();
}))
draw(startX: number, startY: number, endX: number, endY: number) {
this.context.moveTo(startX, startY);
this.context.lineTo(endX, endY);
this.context.stroke();
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 4px; right: 8px; font-size: 14px;">复制</button>
为什么会出现这种情况 在竖屏时还是好好的
</markdown>关于HarmonyOS 鸿蒙Next设置为横屏后 Canvas的PanGesture onAction绘画动作失效的问题,您也可以访问:https://www.itying.com/category-93-b0.html 联系官网客服。
更多关于HarmonyOS 鸿蒙Next设置为横屏后 Canvas的PanGesture onAction绘画动作失效的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
更多关于HarmonyOS 鸿蒙Next设置为横屏后 Canvas的PanGesture onAction绘画动作失效的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
有要学HarmonyOS AI的同学吗,联系我:https://www.itying.com/goods-1206.html