Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.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/5/spring-mvc/2.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
Javascript 如何使用Raphael.js缩放文本_Javascript_Html_Css_Raphael - Fatal编程技术网

Javascript 如何使用Raphael.js缩放文本

Javascript 如何使用Raphael.js缩放文本,javascript,html,css,raphael,Javascript,Html,Css,Raphael,我最近开始了解Raphael.js,我想亲自动手。 我想缩放文本,但它不起作用。 在谷歌搜索了很多之后,我没有发现任何想法 代码是: var paper = Raphael("paper1", 1000,1000); var txt = paper.text(20,50,"Hello World").attr({ "font-family":"Arial","font-size":"30px", "font-weight": "normal", fill: "#000000", stroke

我最近开始了解Raphael.js,我想亲自动手。 我想缩放文本,但它不起作用。 在谷歌搜索了很多之后,我没有发现任何想法

代码是:

var paper = Raphael("paper1", 1000,1000);

var txt = paper.text(20,50,"Hello World").attr({
"font-family":"Arial","font-size":"30px", "font-weight": "normal", 
fill: "#000000", stroke:"black", "stroke-width": "0px",
"text-anchor" : "start" , "font-style": "normal"});
flip正在按txt.scale(-1,1)工作 但是txt.scale(2,1)并没有缩放文本

有没有办法缩放文本

注意:在我的情况下,文本的字体大小需要保持不变,即30px

有办法缩放文本吗?

我检查了一下,效果很好

var r = Raphael('test');

var t = r.text(200, 100, "I am a text").attr({fill:'red', 'font-size':30});

$('#zoomin').click(function() {
    t.scale(2);
});

$('#zoomout').click(function() {
    t.scale(.5);
});

如果我们只缩放文本,那么字体大小就会改变。 我们需要将文本转换为图像,并对其进行缩放。 为此,我使用了隐藏的画布

下面的代码适合我

<canvas id='textCanvas' style="display:none"></canvas>

<script>
  var paper = Raphael("paper1", 1000,1000);
  var img =  paper.image(getTxtImg("Hello World","italic","bold","15px","arial",
                                       "#000000",130,35),20,28,300,80)

  img.scale(2,1)  //Double in width
  img.scale(.5,1)  //Half  in width

function getTxtImg(txt,style,weight,fontsize,fontfamily,color,w,h)
{
  var tCtx = document.getElementById('textCanvas').getContext('2d');
  tCtx.canvas.width = w;
  tCtx.canvas.height = h    ;
  var fontstr = "" + style + " " + weight + " " + fontsize + " " + fontfamily + " ";
  tCtx.font = fontstr
  tCtx.fillStyle  = color
  tCtx.fillText(txt, 0, h);
  return tCtx.canvas.toDataURL();
}
</script>

var paper=Raphael(“paper1”,10001000);
var img=paper.image(getTxtImg(“Hello World”、“italic”、“bold”、“15px”、“arial”),
"#000000",130,35),20,28,300,80)
图像比例(2,1)//宽度加倍
img.刻度(.5,1)//半宽
函数getTxtImg(txt、样式、重量、fontsize、fontfamily、颜色、w、h)
{
var tCtx=document.getElementById('textCanvas').getContext('2d');
tCtx.canvas.width=w;
tCtx.canvas.height=h;
var fontstr=“”+样式+“”+重量+“”+字体大小+“”+字体系列+“”;
tCtx.font=fontstr
tCtx.fillStyle=颜色
tCtx.fillText(txt,0,h);
返回tCtx.canvas.toDataURL();
}

非常感谢A.S.Roma的回复和演示。但实际上文本的字体大小在这里发生了变化。古拉布,我想你的问题可能没有说得很清楚——A.S.做了我应该做的事情,假设缩放意味着字体的外观大小发生了变化。你下面的自我回答并没有真正阐明你试图做什么。我的问题有一个注释部分“注意:文本的字体大小需要保持不变,即在我的情况下为30px”。实际上,我的问题是缩放文本,但字体大小需要保持不变,经过一些搜索后,我知道文本是不可能的。将文本转换为图像后,可以在不更改字体的情况下缩放文本。无论如何,我在这方面很新,所以我可能会犯一些错误,请不要介意。