Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html5用javascript检测绘图字符串长度_Javascript_Html_Canvas_Html5 Canvas_Drawstring - Fatal编程技术网

Html5用javascript检测绘图字符串长度

Html5用javascript检测绘图字符串长度,javascript,html,canvas,html5-canvas,drawstring,Javascript,Html,Canvas,Html5 Canvas,Drawstring,通过这样做,我可以随意地检测出绳子的宽度和高度 canvas.getContext("2d").font="12px Arial"; canvas.getContext("2d").fillStyle = "red"; canvas.getContext("2d").fillText("StackOverflow",x,y); var width = canvas.getContext('2d').measureText(ct_string).width; 但是,我需要先计算宽度,然后再将其

通过这样做,我可以随意地检测出绳子的宽度和高度

canvas.getContext("2d").font="12px Arial";
canvas.getContext("2d").fillStyle = "red";
canvas.getContext("2d").fillText("StackOverflow",x,y);

var width = canvas.getContext('2d').measureText(ct_string).width;
但是,我需要先计算宽度,然后再将其绘制到画布中,这样我就可以这样做

canvas.getContext("2d").fillText("StackOverflow",x - width,y);
我希望在这方面能得到一些帮助,也许我还没有完全理解measureText,我相信它只能在绳子被拉到画布上后才能使用,尽管我不确定

(我在没有服务器的情况下编程,无法测试正在运行的代码)。 创建第二个html5画布是不可能的


我需要更好地理解

我认为measure text从上下文中获取上下文字体属性并根据该属性计算宽度,而不是在html5画布上搜索包含字符串“StackOverflow”Sweet的对象,我找到了一个可以让我测试javascript的网站。对于任何需要帮助的人。。在这里Measure text接受字符串参数并应用字体样式,并计算绘制时将占用的空间。它不会在html5画布元素中搜索字符串。我希望文档会这样说:在画布上搜索文本而不是直接计算文本会太过繁琐和不精确。如果找到答案,请将其作为答案发布,而不是评论。我还不能发布我自己问题的答案。