draw() {
this.context.clearRect(0, 0, this.canvasWidth, 400);
this.context.beginPath();
this.context.lineWidth = 2;
this.context.lineJoin = 'round';
this.context.moveTo(50, 250);
this.connectionPointList.forEach(element => {
this.context.lineTo(element.x, element.y);
this.context.arc(element.x, element.y, 5, 0, Math.PI * 2);
});
this.context.stroke();
} this.scroller.scrollTo({ xOffset: x - 300, yOffset: this.y }); import { display } from '@kit.ArkUI';
class ConnectionPoint {
x: number;
y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
}
@Entry
@Component
struct CanvasDrawOffset {
private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(new RenderingContextSettings(true));
private scroller: Scroller = new Scroller();
private connectionPointList: Array<ConnectionPoint> = [];
private count: number = 0;
private y: number = 350;
private screenWidth: number = 0;
@State canvasWidth: number = 0;
aboutToAppear(): void {
display.getAllDisplays((_err, data) => {
this.canvasWidth = this.screenWidth = data[0].width;
});
this.connectionPointList.push(new ConnectionPoint(150, 150));
this.connectionPointList.push(new ConnectionPoint(200, 350));
}
build() {
Column() {
Scroll(this.scroller) {
Canvas(this.context)
.width(this.canvasWidth)
.height('100%')
.backgroundColor('#f1f3f5')
.onReady(() => {
this.draw();
});
}
.scrollBar(BarState.Off)
.scrollable(ScrollDirection.Horizontal)
.height('70%')
.width('100%');
Button('添加').width('80%').margin({ top: 20 }).onClick(() => {
this.count++;
let x = 200 + 50 * this.count;
this.y = this.y === 150 ? 350 : 150;
this.canvasWidth = this.screenWidth + x;
this.connectionPointList.push(new ConnectionPoint(x, this.y));
this.draw();
this.scroller.scrollTo({ xOffset: x - 300, yOffset: this.y });
});
}
.width('100%')
.height('100%');
}
draw() {
this.context.clearRect(0, 0, this.canvasWidth, 400);
this.context.beginPath();
this.context.lineWidth = 2;
this.context.lineJoin = 'round';
this.context.moveTo(50, 250);
this.connectionPointList.forEach(element => {
this.context.lineTo(element.x, element.y);
this.context.arc(element.x, element.y, 5, 0, Math.PI * 2);
});
this.context.stroke();
}
} PanGesture({ fingers: 1 })
.onActionStart(event => {
if (this.isMoving) {
this.lastTranslateX = this.translateX;
this.lastTranslateY = this.translateY;
} else {
let line = new Line();
line.start(event.fingerList[0].localX - this.translateX,
event.fingerList[0].localY - this.translateY);
this.current = line;
this.list.add(line);
}
})
.onActionUpdate(event => {
if (this.isMoving) {
this.translateX = this.lastTranslateX + event.offsetX;
this.translateY = this.lastTranslateY + event.offsetY;
} else {
let line: Line = this.current as Line;
line.lineTo(event.fingerList[0].localX - this.translateX,
event.fingerList[0].localY - this.translateY);
}
this.paint();
})
.onActionEnd(() => {
this.current = undefined;
}) paint() {
this.ctx.reset();
this.ctx.translate(this.translateX, this.translateY);
this.list.forEach(item => {
if (item.isInCanvas(-this.translateX, -this.translateY, this.ctx.width, this.ctx.height)) {
item.paint(this.ctx);
}
});
} import { LinkedList } from '@kit.ArkTS';
@Entry
@Component
struct CanvasDrawTransLate {
private ctx: CanvasRenderingContext2D = new CanvasRenderingContext2D(new RenderingContextSettings(true));
private list: LinkedList<BaseItem> = new LinkedList();
private current?: BaseItem;
private translateX: number = 0;
private translateY: number = 0;
private lastTranslateX: number = 0;
private lastTranslateY: number = 0;
private isMoving: boolean = false;
paint() {
this.ctx.reset();
this.ctx.translate(this.translateX, this.translateY);
this.list.forEach(item => {
if (item.isInCanvas(-this.translateX, -this.translateY, this.ctx.width, this.ctx.height)) {
item.paint(this.ctx);
}
});
}
build() {
Column() {
Row() {
Button('绘画')
.onClick(() => {
this.isMoving = false;
});
Button('移动')
.margin({ left: 10 })
.onClick(() => {
this.isMoving = true;
});
};
Canvas(this.ctx)
.width('100%')
.height('100%')
.margin({ top: 10 })
.backgroundColor('#f1f3f5')
.gesture(
GestureGroup(GestureMode.Exclusive,
PanGesture({ fingers: 1 })
.onActionStart(event => {
if (this.isMoving) {
this.lastTranslateX = this.translateX;
this.lastTranslateY = this.translateY;
} else {
let line = new Line();
line.start(event.fingerList[0].localX - this.translateX,
event.fingerList[0].localY - this.translateY);
this.current = line;
this.list.add(line);
}
})
.onActionUpdate(event => {
if (this.isMoving) {
this.translateX = this.lastTranslateX + event.offsetX;
this.translateY = this.lastTranslateY + event.offsetY;
} else {
let line: Line = this.current as Line;
line.lineTo(event.fingerList[0].localX - this.translateX,
event.fingerList[0].localY - this.translateY);
}
this.paint();
})
.onActionEnd(() => {
this.current = undefined;
})
)
);
};
}
}
abstract class BaseItem {
protected left: number = 0;
protected top: number = 0;
protected right: number = 0;
protected bottom: number = 0;
isInCanvas(x: number, y: number, width: number, height: number) {
return !(this.right < x || this.left > x + width || this.top > y + height || this.bottom < y);
}
abstract paint(ctx: CanvasRenderingContext2D);
}
class Line extends BaseItem {
path: Path2D = new Path2D();
constructor() {
super();
}
start(x: number, y: number) {
this.path.moveTo(x, y);
this.left = x;
this.top = y;
}
lineTo(x: number, y: number) {
this.path.lineTo(x, y);
this.left = Math.min(x, this.left);
this.top = Math.min(y, this.top);
this.right = Math.max(x, this.right);
this.bottom = Math.max(y, this.bottom);
}
paint(ctx: CanvasRenderingContext2D): void {
ctx.strokeStyle = '#0097D4';
ctx.lineWidth = 10;
ctx.stroke(this.path);
}
} 实现效果如下:

两种实现方案对比如下:
方案 | 实现方式 | 优点 | 缺点 |
|---|---|---|---|
方案一 | 使用Scroller实现画布扩展和内容移动。 | 实现简单、易于理解,画布尺寸可以扩展。 | 无法自由移动,场景单一。 |
方案二 | 使用PanGesture实现画布平移和绘制。 | 可以自由移动,可以达到无限移动的效果。 | 实现较为复杂。 |