拉斐尔 svg 导入尺寸

发布于 2024-11-30 18:10:01 字数 506 浏览 1 评论 0原文

我已经从 Illustrator 图像创建了 SVG 文件。我正在使用这个: https://github.com/wout/raphael-svg-import 导入并显示矢量图像。

我以文本形式读取 svg 文件,并将其提交给导入功能:

var mygetrequest = new XMLHttpRequest();
var svgdata = mygetrequest.responseText; 
paper.importSVG(svgdata); //where paper is my Raphael canvas

一切都运行良好。

但我似乎找不到任何有关如何更改图像设置和视图的信息或提示? 例如,我非常希望它填满它的容器,而不管它的大小。

有什么想法吗?一路上为我提供帮助的文档?任何事情,真的...

I've created and SVG-file from an Illustrator-image. I'm using this: https://github.com/wout/raphael-svg-import to import and display the vector-image.

I read the svg-file in as text, and submit it to the import-function:

var mygetrequest = new XMLHttpRequest();
var svgdata = mygetrequest.responseText; 
paper.importSVG(svgdata); //where paper is my Raphael canvas

All working very nicely.

But I can't seem to find any info or hints on how to change the settings and view of the image?
For example I would very much like it to fill out its container, disregarding the size of this.

Any ideas? Documentation to help me along the way? Anything, really...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

打小就很酷 2024-12-07 18:10:01

我可以为您提供两个想法:

1)也许 SVG 导入会考虑原始大小并为您进行缩放。然而,在 illustrator 中编辑时,您可能周围有一个更大的文档,这会让 svgImport 感到困惑?

2) 否则,您可以将论文作为一组导入。并缩放集合。

var set = paper.set();
paper.importSVG(svgdata, set);
set.scale( 2, 2, centerx, centery );

(如果没有示例,我不确定 centerx 和 centery 应该是什么)。

[编辑(批准后)]
看来 Raphael 使您能够获取集合的边界框:

var bbox = set.getBBox();
// You'll now have bbox.x, bbox.y, bbox.width and bbox.height at your disposal. 
// For example:
paper.rect( bbox.x, bbox.y, bbox.width, bbox.height ).attr('border', 'red');

所以我想说您可以:

  1. 获取导入的 SVG 集合的边界框
  2. 使用 set.translate 将边界框的中心与纸张的中心对齐(例如,使用 ( paperwidth / 2 ) - ( bbox.x + bbox.width / 2 ) 进行 x 翻译)。
  3. 根据与纸张尺寸相比的边界框尺寸进行缩放。您可能希望在保持纵横比的同时进行缩放,因此您必须使用最接近 1 的那个(
    paper.width/bbox.width 和 paper.height/bbox.height )。
  4. (注意:现在使用纸张中心作为缩放中心 - 因为你翻译了你的集合,这实际上也是 svg 集合的中心)

我是从头顶做的,所以它可能不是 100% 准确,但如果你知道你可能能够消除这些故障。

There's two ideas I can offer you:

1) maybe the SVG import takes into account the original size and does scaling for you. However when editing in illustrator you might have a much bigger document around it, which confuses the svgImport?

2) Otherwise, you could import the paper as a set. And scale the set.

var set = paper.set();
paper.importSVG(svgdata, set);
set.scale( 2, 2, centerx, centery );

(I'm not sure without an example to play with, what centerx and centery should be).

[EDIT (after approval)]
It seems Raphael enables you to get a set's bounding box:

var bbox = set.getBBox();
// You'll now have bbox.x, bbox.y, bbox.width and bbox.height at your disposal. 
// For example:
paper.rect( bbox.x, bbox.y, bbox.width, bbox.height ).attr('border', 'red');

So I'd say you could:

  1. Get bounding box of imported SVG set
  2. Use set.translate to align the bounding box's center with the center of the paper (use something like ( paperwidth / 2 ) - ( bbox.x + bbox.width / 2 ) for example for the x translation).
  3. Scale based on the bounding box size compared to the paper size. You probably want to scale while maintaining aspect ratio so you'll have to use the one that's closest to 1 (of
    paper.width/bbox.width and paper.height/bbox.height).
  4. (Note: use center of paper now as center of scaling - since you translated your set this is actually the center of the svg set as well)

I did this from the top of my head so it might not be 100% accurate, but if you get the idea you'll probably be able to get rid of the glitches.

七禾 2024-12-07 18:10:01

我有类似的问题。我所做的是在 Inkscape (SVG 编辑器程序)中打开 SVG,更改大小直到可以......但是可能有更好的方法!

I had a similar problem. What I did was open the SVG in Inkscape (SVG editor program), change the size until it was OK... but there is probably a better way!

风情万种。 2024-12-07 18:10:01

我遇到了与原始海报完全相同的问题。我也在做着和他一样的事情。即使在阅读 Jochem 的解决方案/想法后,我也遇到了几天无法解决的问题。

我的具体问题是,在 Internet Explorer(所有版本)中,图像不断呈现超出范围,特别是将其自身与包含的 div 的右下角对齐。

  • 尝试使用变换将图像居中并“适合”其容器是行不通的,也不容易理解拉斐尔对变换的实现。
  • 我意识到 raphael-svg-import 库不完整,无法使用 svg 组,并且文档不存在。

在花了这么多时间在这个问题上之后,我的高级开发人员得出结论,这个插件有缺陷并且无法使用。

到目前为止,我还没有找到满足我的特定要求的替代解决方案(由于公司政策,我们不能使用 Flash)。对于那些没有这种限制的人,我推荐 Google 的 SVGWeb

虽然这不是OP问题的答案,我的发现可能会为将来可能遇到此问题的人节省一些时间。

I had exactly the same problem as the original poster. I was doing the same thing that he did. I came across problems that I was not able to solve for days even after I read the solution/ideas from Jochem.

My specific problem was that in Internet Explorer (all versions), the image kept rendering out of bounds, specifically aligning itself to the bottom right hand corner of the containing div.

  • Trying to center and 'fit' the image to it's container using transformations did not work nor was it easy to understand the Raphael's implementation of the transformations.
  • I've realized that raphael-svg-import library was incomplete, unable to work with svg groups and documentation are non-existent.

After spending so much time on this issue, my senior developer concluded that this plugin is flawed and is unusable.

So far, I have not been able to find a alternative solution that meets my specific requirements (we cannot use Flash because of company policies). For those without that constrain, I recommend Google's SVGWeb

Although this is not a answer to the OP's question, my findings may save some time for someone who might come across this issue in the future.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文