英文:
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('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);
}
答案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 = 'black';
context.font = '8px Arial';
context.rotate(Math.PI / 2); // Rotation of the text
context.textBaseline = 'middle';
context.textAlign = 'center';
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论