简体   繁体   English

如何使用Raphael.js缩放文本

[英]How to scale text using Raphael.js

I came to know about Raphael.js recently and I was trying to do some hands on. 我最近开始了解Raphael.js,并且正在尝试动手。 I would like to scale a text but it is not working. 我想缩放文本,但是无法正常工作。 After searching a lot in Google I have not find any idea. 在Google中搜索了很多之后,我仍然找不到任何想法。

Code is: 代码是:

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 is working by txt.scale(-1,1) but txt.scale(2,1) is not scaling the text. flip可以通过txt.scale(-1,1)使用,但是txt.scale(2,1)不能缩放文本。

Is there a way to scale a text? 有没有办法缩放文本?

Note: Font size of the text needs to remain same ie 30px in my case. 注意:文本的字体大小需要保持相同,即我的情况下为30px。

Is there a way to scale a text? 有没有办法缩放文本?

DEMO 演示

I checked and it works like a charm 我检查了一下,它就像一个魅力

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);
});

If we scale the text only then font size shall be changed. 如果仅缩放文本,则字体大小应更改。 We need to convert the text into an image and the we need to scale it. 我们需要将文本转换为图像,然后进行缩放。 To do that I have used a hidden canvas. 为此,我使用了隐藏的画布。

Following code works for me. 以下代码对我有用。

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM