Skip to content

Commit

Permalink
feat(scribble-tool): Fix the size error after changing img
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerwin-L committed Sep 18, 2022
1 parent c7513b0 commit de426ae
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions packages/lb-annotation/src/core/toolOperation/ScribbleTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class ScribbleTool extends BasicToolOperation {
this.penSize = size;
}

public createCacheCanvas(imgNode?: HTMLImageElement) {
public initCacheCanvas(imgNode?: HTMLImageElement) {
if (this.cacheCanvas || !imgNode) {
return;
}
Expand All @@ -60,21 +60,37 @@ class ScribbleTool extends BasicToolOperation {
this.cacheContext = ctx;
}

public updateCacheCanvasSize(imgNode: HTMLImageElement) {
if (this.cacheCanvas) {
this.cacheCanvas.width = imgNode.width;
this.cacheCanvas.height = imgNode.height;
}
}

public updateUrl2CacheContext(url: string) {
ImgConversionUtils.createImgDom(url).then((img) => {
if (!this.cacheContext) {
this.createCacheCanvas(img);
this.initCacheCanvas(img);
}
if (this.cacheContext) {
this.cacheContext.save();
this.cacheContext.clearRect(0, 0, img.width, img.height);
this.clearResult();
this.cacheContext.drawImage(img, 0, 0, img.width, img.height);
this.cacheContext.restore();
this.render();
}
});
}

public setImgNode(imgNode: HTMLImageElement, basicImgInfo?: Partial<{ valid: boolean; rotate: number }>): void {
super.setImgNode(imgNode, basicImgInfo);
if (this.cacheCanvas) {
this.updateCacheCanvasSize(imgNode);
} else {
this.initCacheCanvas(imgNode);
}
}

public setResult(data: IScribbleData[]) {
// Only has one layer
const { url } = data?.[0] ?? {};
Expand Down Expand Up @@ -112,7 +128,7 @@ class ScribbleTool extends BasicToolOperation {
}

// Init Image
this.createCacheCanvas(this.imgNode);
this.initCacheCanvas(this.imgNode);
this.mouseEvents('onMouseDown').call(this, e);
};

Expand Down Expand Up @@ -245,11 +261,15 @@ class ScribbleTool extends BasicToolOperation {
return [[], this.basicImgInfo, { imgBase64 }];
}

public clearResult() {
public clearCacheCanvas() {
this.cacheContext?.clearRect(0, 0, this.cacheContext.canvas.width, this.cacheContext.canvas.height);
this.render();
}

public clearResult() {
this.clearCacheCanvas();
}

public renderPoint(radius: number) {
DrawUtils.drawCircleWithFill(this.canvas, this.coord, radius, { color: this.color });
}
Expand Down

0 comments on commit de426ae

Please sign in to comment.