旋转画布上的文本方向

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

Rotate Text orientation on Canvas

问题

I have a canvas in my Angular application where the user can manipulate the coordinates of some fixed shapes (8 texts and 1 rectangle). I want to allow the user to select the orientation of the text, i.e., horizontal or vertical. How can I achieve this behavior? Appreciate any help.

redrawLabel(result: ISerialLabelDesign) {
    console.log('inside redrawLabel');
    this.clearDrawing();
    this.shapesToDrawArray.length = 0;
    console.log(result); // giving the entire form.value object
    this.defaultLabelWidth = 300; // result.labelWidth * 3.7795;  // convert from mm to pixel
    this.defaultLabelHeight = 300; // result.labelHeight * 3.7795;
    this.renderDrawing(result);
    this.startDrawing(this.shapesToDrawArray);
}

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

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, text: string, x: number, y: number, w: number, h: number) {
    context.fillStyle = 'black';
    context.font = '8px Arial';
    context.fillText(text, x, y);
}

旋转画布上的文本方向

英文:

I have a canvas in my Angular application where user can play around with the coordinates of some fixed shapes(8 texts and 1 rectangle). I want to be able to be able to allow user to also select the orientation of the text i.e. show horizontal or vertical. How can I achieve this behaviour. Appreciate any help.

redrawLabel(result: ISerialLabelDesign) {
console.log(&#39;inside redrawLabel&#39;);
this.clearDrawing();
this.shapesToDrawArray.length = 0;
console.log(result); // giving the entire form.value object
this.defaultLabelWidth = 300//result.labelWidth * 3.7795;  // convert from mm to pixel
this.defaultLabelHeight = 300//result.labelHeight * 3.7795;
this.renderDrawing(result);
this.startDrawing(this.shapesToDrawArray);
}
startDrawing(shapesToDraw: Shape[]) {
console.log(&#39;inside startDrawing::&#39;);
for (var i = 0; i &lt; shapesToDraw.length; i++) {
if (shapesToDraw[i].type === &#39;barcode&#39;) {
this.drawRectangle(this.context, shapesToDraw[i].x, shapesToDraw[i].y, shapesToDraw[i].w, shapesToDraw[i].h);
} else if (shapesToDraw[i].type === &#39;text&#39;) {
this.drawText(this.context, shapesToDraw[i].text, shapesToDraw[i].x, shapesToDraw[i].y, shapesToDraw[i].w, shapesToDraw[i].h);
}
}
}
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, text: string, x: number, y: number, w: number, h: number) {
context.fillStyle = &#39;black&#39;;
context.font = &#39;8px Arial&#39;;
context.fillText(text, x, y);
}

旋转画布上的文本方向

答案1

得分: 2

你可以使用 rotate() 方法来旋转文本,示例如下:

private drawText(context: CanvasRenderingContext2D, text: string, x: number, y: number, w: number, h: number) {
    context.fillStyle = 'black';
    context.font = '8px Arial';
    context.rotate(Math.PI / 2); // 文本旋转
    context.textBaseline = 'middle';
    context.textAlign = 'center';
    context.fillText(text, x, y);
}

请注意,这段代码用于在Canvas上绘制文本,并使用rotate()方法将文本旋转了90度。

英文:

You can rotate your text with rotate() as followed:

private drawText(context: CanvasRenderingContext2D, text: string, x: number, y: number, w: number, h: number) {
context.fillStyle = &#39;black&#39;;
context.font = &#39;8px Arial&#39;;
context.rotate(Math.PI / 2); // Rotation of the text
context.textBaseline = &#39;middle&#39;;
context.textAlign = &#39;center&#39;;
context.fillText(text, x, y);
}

答案2

得分: 0

旋转HTML5 Canvas可以使用rotate()变换方法。旋转变换需要一个以弧度为单位的角度。要定义旋转点,我们需要首先将画布上下文进行平移,以使上下文的左上角位于所需的旋转点上。
使用CSS:
Transform: rotate(90deg)

英文:

To rotate the HTML5 Canvas, we can use the rotate() transform method. The rotate transformation requires an angle in radians. To define the rotation point, we need to first translate the canvas context such that the top left corner of the context lies on the desired rotation point.
use css :
Transform : rotate(90deg)

huangapple
  • 本文由 发表于 2023年5月10日 13:49:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76215254.html
匿名

发表评论

匿名网友

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

确定