gpt4 book ai didi

javascript - 保持文本在 Canvas 上垂直居中

转载 作者:太空狗 更新时间:2023-10-29 16:42:34 26 4
gpt4 key购买 nike

我遇到的问题是让用户输入的文本在 canvas 元素内垂直居中。我已经构建了一个测试环境来尝试解决这个问题,我在这篇文章中提供了这个问题以及一个 fiddle 。这是我的代码:

HTML:

Enter Your Text: <br/>
<textarea id="inputtextuser" onkeyup="inputtextgo()" name="Text">Enter Your</textarea> <br/>
TextBaseline:
<select id="inputtextbaseline" onchange="inputtextgo()";>
<option value="alphabetic">alphabetic</option>
<option value="top">top</option>
<option value="bottom">bottom</option>
<option value="middle">middle</option>
<option value="hanging">hanging</option>
</select> <br/>
<canvas id="canvas1" width="300px" height="200px" >Your browser does not support the HTML5 canvas tag.</canvas> <br/>
<div id ="textheightentered"></div>

CSS:

canvas {
border: solid 1px black;
}

JavaScript/jQuery:

//Declaring the Canvas
var canvas1 = document.getElementById('canvas1');
context1 = canvas1.getContext('2d');

//running the function to update the canvas
inputtextgo();

function inputtextgo() {
//Retrieving the text entered by the user
var inputtext = document.getElementById("inputtextuser").value;
//Calling the function to calculate the height on the text entered by the user
var textheight = fontheight(inputtext);
//retrieving the baseline selected by the user
var baseline = document.getElementById("inputtextbaseline").value;
//calculating the new y needed to center the text based on the height of the text
var y = 100 + textheight/2;

//Updating the canvas for the new text entered by the user
context1.clearRect(0, 0, 300, 200);
context1.font = "36px arial";
context1.textBaseline = baseline;
context1.fillText (inputtext, 0, y);

//Drawing a line across the middle of the canvas for reference
context1.strokeStyle="red";
context1.moveTo(5,100);
context1.lineTo(290,100);
context1.stroke();
$("#textheightentered").text("Text Height: " + textheight);
}

function fontheight(text){

var canvas=document.createElement("canvas");
canvas.width = 1000;
canvas.height = 200;
var ctx=canvas.getContext("2d");

function measureTextHeight(left, top, width, height,text) {

// Draw the text in the specified area
ctx.save();
ctx.clearRect(0,0, canvas.width, canvas.height);
ctx.baseline = "top";
ctx.fillText(text, 0, 35); // This seems like tall text... Doesn't it?
ctx.restore();

// Get the pixel data from the canvas
var data = ctx.getImageData(left, top, width, height).data,
first = false,
last = false,
r = height,
c = 0;

// Find the last line with a non-white pixel
while(!last && r) {
r--;
for(c = 0; c < width; c++) {
if(data[r * width * 4 + c * 4 + 3]) {
last = r;
break;
}
}
}

// Find the first line with a non-white pixel
while(r) {
r--;
for(c = 0; c < width; c++) {
if(data[r * width * 4 + c * 4 + 3]) {
first = r;
break;
}
}

// If we've got it then return the height
if(first != r) return last - first;
}

// We screwed something up... What do you expect from free code?
return 0;
}
ctx.font= "36px arial";
var height = measureTextHeight(0, 0, canvas.width, canvas.height,text);
//return the height of the string
return height;
} // end $(function(){});

JSFiddle:http://jsfiddle.net/grapien/R6DWN/3/

我遇到的问题是,如果用户输入小写字母“y”,例如文本高度会发生变化,并且文本不会保留在 Canvas 的中心。这是因为在 y 变量中执行的基线校正需要根据输入的文本(小写和大写字符的组合)而有所不同,因为基线以下的文本会扭曲在 y 变量中执行的校正。

这是我遇到问题的地方,因为我想让文本始终在 canvas 元素内居中。我唯一能想到的就是计算文本基线引用点上方和下方的非空白像素。然后使用从基线到文本末端的差异来偏移文本以使其在 Canvas 上居中。我不确定您是如何编写此脚本的,或者是否有可能。但这是我想出的最好的方法。

如果有人有任何想法或指导,我将不胜感激,因为开发一种解决此问题的方法让我很困惑。

最佳答案

我相信我已经使用我在问题中陈述的技术解决了这个问题。我创建了一个新函数来计算延伸到输入文本基线以下的字体量。使用与用于计算高度的方法类似的技术。我使用字母基线,然后计算包含高于该基线的内容的像素。请参阅下面的功能。

function fontheight2(text){

var canvas=document.createElement("canvas");
canvas.width = 1000;
canvas.height = 200;
var ctx=canvas.getContext("2d");

function measureTextHeight(left, top, width, height,text) {

// Draw the text in the specified area
ctx.save();
ctx.clearRect(0,0, canvas.width, canvas.height);
ctx.fillText(text, 0, 100); // This seems like tall text... Doesn't it?
ctx.restore();

// Get the pixel data from the canvas
var data = ctx.getImageData(left, top, width, height).data,
first = false,
last = false,
r = 100,
c = 0;

// Find the first line with a non-white pixel
while(r) {
r--;
for(c = 0; c < width; c++) {
if(data[r * width * 4 + c * 4 + 3]) {
first = r;
break;
}
}

// If we've got it then return the height
if(first != r) return 99 - first;
}

// We screwed something up... What do you expect from free code?
return 0;
}
ctx.baseline = "Alphabetic";
ctx.font= "36px arial";
var height = measureTextHeight(0, 0, canvas.width, canvas.height,text);
//return the height of the string
return height;
} // end $(function(){});

从那里我简单地使用以下对 y 的调整来将文本偏移到中心:

var textheight = fontheight(inputtext); //Calculate Text Height
var textheight2 = fontheight2(inputtext); //Calculate portion above the baseline
var difference = textheight - textheight2; // Calculate the portion below the baseline
var y = 100 - difference + textheight/2; // Adjust the y cordinate

这会偏移 y 以针对低于文本基线的文本部分进行调整。我在这里更新了 JSFiddle http://jsfiddle.net/grapien/R6DWN/6/ .

关于javascript - 保持文本在 Canvas 上垂直居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15648267/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com