Angular – 调整大小时画布不重新绘制形状

huangapple go评论80阅读模式
英文:

Angular - Canvas not redrawing shapes on resizing it

问题

Here is the translated code part:

我有一个Angular应用程序,其中我使用画布,根据用户的选择可以调整大小。现在,当我在表单中调整画布大小时,画布的大小正确更改,但上面的所有内容都消失了,我明白画布会清除自己。但然后我调用一个叫做"redraw"的方法,试图再次在画布上绘制这些形状,但它不起作用。

当我更改画布中元素的坐标时,我也调用"redraw"方法,所有形状再次出现。有人可以帮助我解决这个问题吗?

这是显示问题的视频。

[![点击这里查看问题描述][1]][1]

ngOnInit(): void {
    console.log('ngOnInit');
    const canvas: HTMLCanvasElement = this.myCanvas.nativeElement;
    this.context = canvas.getContext('2d');
    this.form = this.formBuilder.group({
        labelName: ['', Validators.required],
        labelOrientation: ['VERTICAL', Validators.required],
        labelWidth: [, Validators.required],
        labelHeight: [, Validators.required],
        barcodeLeft: [, Validators.required],
        barcodeTop: [, Validators.required],
        barcodeWidth: [,],
        barcodeHeight: [,],
        gtinLeft: [, Validators.required],
        gtinTop: [, Validators.required],
        gtinWidth: [,],
        gtinHeight: [,],
    });

    this.form.setValue(this.verticalOrientationData);
    this.objSerialLabelDesignModel = this.form.value;
    if (this.context) {
        console.log('ngOnInit check this.context', this.context);
        this.redrawLabel(this.objSerialLabelDesignModel);
    }
    this.onChanges();
}

ngAfterViewInit() {
    console.log('ngAfterViewInit');
    this.startDrawing(this.shapesToDrawArray);
}

onChanges(): void {
    console.log('onChange!!!!!!');
    // 订阅表单值的更改以在其更改时重绘标签
    this.form.valueChanges.subscribe((newVal) => {
        this.objSerialLabelDesignModel = newVal;
        this.redrawLabel(this.objSerialLabelDesignModel);
    });
}

redrawLabel(result: ISerialLabelDesign) {
    console.log('inside redrawLabel');
    this.clearDrawing();
    console.log(this.shapesToDrawArray.length);
    this.shapesToDrawArray.length = 0;
    console.log('result::::::::', result); // 给出整个 form.value 对象
    this.defaultLabelWidth = result.labelWidth * 3.7795; // 将毫米转换为像素
    this.defaultLabelHeight = result.labelHeight * 3.7795;
    this.storeDrawing(result);
    this.startDrawing(this.shapesToDrawArray);
}

startDrawing(shapesToDraw: Shape[]) {
    console.log('inside startDrawing::');
    for (var i = 0; i < shapesToDraw.length; i++) {
        const shape = shapesToDraw[i];
        if (shape.type === 'barcode') {
            this.drawRectangle(this.context, shape.x, shape.y, shape.w, shape.h);
        } else if (shape.type === 'text') {
            this.drawText(this.context, shape);
        }
    }
}

storeDrawing(result: ISerialLabelDesign) {
    this.saveShapes('barcode', undefined, result.barcodeLeft, result.barcodeTop, result.barcodeWidth, result.barcodeHeight);
    this.saveShapes('text', 'GTIN:', result.gtinLeft, result.gtinTop, 0, 0, result.labelOrientation);
    this.saveShapes('text', 'UID:', result.uidLeft, result.uidTop, 0, 0, result.labelOrientation);
    this.saveShapes('text', 'EXP:', result.expLeft, result.expTop, 0, 0, result.labelOrientation);
    this.saveShapes('text', 'LOT:', result.lotLeft, result.lotTop, 0, 0, result.labelOrientation);
    this.saveShapes('text', 'C PRICE:', result.costpriceLeft, result.costpriceTop, 0, 0, result.labelOrientation);
    this.saveShapes('text', 'S PRICE:', result.sellingpriceLeft, result.sellingpriceTop, 0, 0, result.labelOrientation);
    this.saveShapes('text', '12345678901234', result.gtinWidth, result.gtinHeight, 0, 0, result.labelOrientation);
    this.saveShapes('text', '12345678901234567890', result.uidWidth, result.uidHeight, 0, 0, result.labelOrientation);
    this.saveShapes('text', '2022-01-01', result.expWidth, result.expHeight, 0, 0, result.labelOrientation);
    this.saveShapes('text', '313', result.lotWidth, result.lotHeight, 0, 0, result.labelOrientation);
    this.saveShapes('text', '250000', result.costpriceWidth, result.costpriceHeight, 0, 0, result.labelOrientation);
    this.saveShapes('text', '550000', result.sellingpriceWidth, result.sellingpriceHeight, 0, 0, result.labelOrientation);
}

saveShapes(type: string, text: string | undefined, x: number, y: number, w: number, h: number, orientation?: string) {
    this.createdShapeObject = {
        type: type,
        text: text,
        x: x * 3.7795,
        y: y * 3.7795,
        w: w * 3.7795,
        h: h * 3.7795,
        orientation: orientation
    };
    this.shapesToDrawArray.push(this.createdShapeObject);
    // 根据文本位置显示坐标
    let xCoord = x, yCoord = y;
    if (text && orientation === 'VERTICAL') {
        xCoord += h;
    } else {
        yCoord += h;
    }
    console.log(`(${xCoord}, ${yCoord})`);
}

private drawRectangle(context: CanvasRenderingContext2D, x: number, y: number, w: number, h: number) {
    console.log('inside drawRectangle');
    context.strokeRect(x, y, w, h);
}

private drawText(context: CanvasRenderingContext2D, shape: Shape) {
    console.log('inside drawText', shape)
    context.fillStyle = 'black';
    context.font = '8px Arial';
    console.log('shape.orientation', shape.orientation);
    if (shape.orientation === 'HORIZONTAL') {
        console.log('inside HORIZONTAL shape.orientation', shape.orientation);
        this.drawTextHorizontal(context, shape.text, shape.x, shape.y, shape.w, shape.h);
    } else {
        console.log('inside VERTICAL shape.orientation', shape.orientation);
        this.drawTextVertical(context, shape.text, shape.x, shape.y, shape.w, shape.h);
    }
}

Please note that the translation might not be perfect, and it's essential to ensure that the code logic is correctly preserved when implementing it.

英文:

I have an Angular application where I use canvas that I can resize based on the user selection. Now, when I resize my canvas size in the form, the canvas correctly changes size but everything on it disappears, I understand that the canvas clears itself. But then I call a method called redraw which tries to draw those shapes in the canvas again, but it does not.

When I change a coordinates for a element in the canvas, here also I call that redraw method and all shapes again reappears. Can someone please help me resole this issue.

Here is video showing the issue.

Angular – 调整大小时画布不重新绘制形状

ngOnInit(): void {
console.log(&#39;ngOnInit&#39;);
const canvas: HTMLCanvasElement = this.myCanvas.nativeElement;
this.context = canvas.getContext(&#39;2d&#39;);
this.form = this.formBuilder.group({
labelName: [&#39;&#39;, Validators.required],
labelOrientation: [&#39;VERTICAL&#39;, Validators.required],
labelWidth: [, Validators.required],
labelHeight: [, Validators.required],
barcodeLeft: [, Validators.required],
barcodeTop: [, Validators.required],
barcodeWidth: [,],
barcodeHeight: [,],
gtinLeft: [, Validators.required],
gtinTop: [, Validators.required],
gtinWidth: [,],
gtinHeight: [,],
});
this.form.setValue(this.verticalOrientationData);
this.objSerialLabelDesignModel = this.form.value;
if (this.context) {
console.log(&#39;ngOnInit check this.context&#39;, this.context);
this.redrawLabel(this.objSerialLabelDesignModel);
}
this.onChanges();
}
ngAfterViewInit() {
console.log(&#39;ngAfterViewInit&#39;);
this.startDrawing(this.shapesToDrawArray);
}
onChanges(): void {
console.log(&#39;onChange!!!!!!&#39;);
// Subscribe to changes in the form value to redraw the label when it changes
this.form.valueChanges.subscribe((newVal) =&gt; {
this.objSerialLabelDesignModel = newVal;
this.redrawLabel(this.objSerialLabelDesignModel);
});
}
redrawLabel(result: ISerialLabelDesign) {
console.log(&#39;inside redrawLabel&#39;);    
this.clearDrawing();
console.log(this.shapesToDrawArray.length);
this.shapesToDrawArray.length = 0;
console.log(&#39;result::::::::&#39;, result); // giving the entire form.value object
this.defaultLabelWidth = result.labelWidth * 3.7795; // convert from mm to pixel
this.defaultLabelHeight = result.labelHeight * 3.7795;            
this.storeDrawing(result);          
this.startDrawing(this.shapesToDrawArray);
}
startDrawing(shapesToDraw: Shape[]) {
console.log(&#39;inside startDrawing::&#39;);
for (var i = 0; i &lt; shapesToDraw.length; i++) {
const shape = shapesToDraw[i];
if (shape.type === &#39;barcode&#39;) {
this.drawRectangle(this.context, shape.x, shape.y, shape.w, shape.h);
} else if (shape.type === &#39;text&#39;) {
this.drawText(this.context, shape);
}
}
}
storeDrawing(result: ISerialLabelDesign) {
this.saveShapes(&#39;barcode&#39;, undefined, result.barcodeLeft, result.barcodeTop, result.barcodeWidth, result.barcodeHeight,);
this.saveShapes(&#39;text&#39;, &#39;GTIN:&#39;, result.gtinLeft, result.gtinTop, 0, 0, result.labelOrientation);
this.saveShapes(&#39;text&#39;, &#39;UID:&#39;, result.uidLeft, result.uidTop, 0, 0, result.labelOrientation);
this.saveShapes(&#39;text&#39;, &#39;EXP:&#39;, result.expLeft, result.expTop, 0, 0, result.labelOrientation);
this.saveShapes(&#39;text&#39;, &#39;LOT:&#39;, result.lotLeft, result.lotTop, 0, 0, result.labelOrientation);
this.saveShapes(&#39;text&#39;, &#39;C PRICE:&#39;, result.costpriceLeft, result.costpriceTop, 0, 0, result.labelOrientation);
this.saveShapes(&#39;text&#39;, &#39;S PRICE:&#39;, result.sellingpriceLeft, result.sellingpriceTop, 0, 0, result.labelOrientation);
this.saveShapes(&#39;text&#39;, &#39;12345678901234&#39;, result.gtinWidth, result.gtinHeight, 0, 0, result.labelOrientation);
this.saveShapes(&#39;text&#39;, &#39;12345678901234567890&#39;, result.uidWidth, result.uidHeight, 0, 0, result.labelOrientation);
this.saveShapes(&#39;text&#39;, &#39;2022-01-01&#39;, result.expWidth, result.expHeight, 0, 0, result.labelOrientation);
this.saveShapes(&#39;text&#39;, &#39;313&#39;, result.lotWidth, result.lotHeight, 0, 0, result.labelOrientation);
this.saveShapes(&#39;text&#39;, &#39;250000&#39;, result.costpriceWidth, result.costpriceHeight, 0, 0, result.labelOrientation);
this.saveShapes(&#39;text&#39;, &#39;550000&#39;, result.sellingpriceWidth, result.sellingpriceHeight, 0, 0, result.labelOrientation);
}
saveShapes(type: string, text: string | undefined, x: number, y: number, w: number, h: number, orientation?: string) {
//console.log(&#39;length before push:::&#39;,this.shapesToDrawArray.length);
this.createdShapeObject = {
type: type,
text: text,
x: x * 3.7795,
y: y * 3.7795,
w: w * 3.7795,
h: h * 3.7795,
orientation: orientation
};
this.shapesToDrawArray.push(this.createdShapeObject);
// Show coordinates based on the text position
let xCoord = x, yCoord = y;
if (text &amp;&amp; orientation === &#39;VERTICAL&#39;) {
xCoord += h;
} else {
yCoord += h;
}
console.log(`(${xCoord}, ${yCoord})`);
}
private drawRectangle(context: CanvasRenderingContext2D, x: number, y: number, w: number, h: number) {
console.log(&#39;inside drawRectangle&#39;);
context.strokeRect(x, y, w, h);
}
private drawText(context: CanvasRenderingContext2D, shape: Shape) {
console.log(&#39;inside drawText&#39;, shape)
context.fillStyle = &#39;black&#39;;
context.font = &#39;8px Arial&#39;;
console.log(&#39;shape.orientation&#39;, shape.orientation);
if (shape.orientation === &#39;HORIZONTAL&#39;) {
console.log(&#39;inside HORIZONTAL shape.orientation&#39;, shape.orientation);
this.drawTextHorizontal(context, shape.text, shape.x, shape.y, shape.w, shape.h);
} else {
console.log(&#39;inside VERTICAL shape.orientation&#39;, shape.orientation);
this.drawTextVertical(context, shape.text, shape.x, shape.y, shape.w, shape.h);
}
}

答案1

得分: 1

以下是翻译好的部分:

"Ok for those who come across this issue, the solution is to put the reDraw method in a setTimeout. The reason being the browser renders everything so fast that it does not get the time to draw the shapes once the canvas is cleared.

setTimeout(() => { this.startDrawing(this.shapesToDrawArray); }, 500);"

英文:

Ok for those who come across this issue, the solution is to put the reDraw method in a setTimeout. The reason being the browser renders everything so fast that it does not get the time to draw the shapes once the canvas is cleared.

setTimeout(() =&gt; {         
this.startDrawing(this.shapesToDrawArray);
}, 500);

huangapple
  • 本文由 发表于 2023年5月15日 14:57:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76251553.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定