为什么 SVG 呈现时倾斜了(Processing)?

huangapple go评论118阅读模式
英文:

Why are svg rendered tilted (Processing)?

问题

以下是您提供的内容的翻译:

我的目标是使用Processing 3在Windows 10上重新制作经典的Windows XP上的纸牌游戏FreeCell。
为此,我从这里下载了一组最接近旧纸牌外观的svg文件。
在我的代码中,我有一个名为Deck的类,其中包含一个卡片的ArrayList,在构造时,它通过为它们提供所需的svg文件路径以及x和y坐标来初始化卡片。

  1. class Deck
  2. {
  3. String[] seeds = {"HEART", "DIAMOND", "CLUB", "SPADE"};
  4. String[] values = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"};
  5. ArrayList<Card> cards = new ArrayList<Card>();
  6. Deck()
  7. {
  8. //创建卡片并将其存储到数组中。
  9. for(int seed = 0; seed < seeds.length; seed++)
  10. {
  11. for(int value = 0; value < values.length; value++)
  12. {
  13. String cardName = seeds[seed] + "-" + values[value] + ".svg";
  14. cards.add(new Card(cardName));
  15. }
  16. }
  17. //洗牌。
  18. //this.shuffle();
  19. //为卡片分配x和y坐标。
  20. for(int c = 0; c < cards.size(); c++)
  21. {
  22. int x = cardSpace + (cardWidth+cardSpace)*(c%8);
  23. int y = 32 + cardHeight + edge*2 + cardSpace + 25*(c/8);
  24. cards.get(c).setxy(x, y);
  25. }
  26. }
  27. void shuffle()
  28. {
  29. for(int c = cards.size()-1; c > 0; c--)
  30. {
  31. int k = (int)Math.floor(Math.random() * (c + 1));
  32. Card card = cards.get(c);
  33. cards.set(c, cards.get(k));
  34. cards.set(k, card);
  35. }
  36. }
  37. void render()
  38. {
  39. for(Card card : cards)
  40. {
  41. card.render();
  42. }
  43. }
  44. }

卡片类:

  1. class Card
  2. {
  3. PShape svg;
  4. int x;
  5. int y;
  6. Card(String svgName)
  7. {
  8. this.svg = loadShape(svgName);
  9. }
  10. void setxy(int x, int y)
  11. {
  12. this.x = x;
  13. this.y = y;
  14. }
  15. boolean isMouseOver()
  16. {
  17. return mouseX > x && mouseX < (x+cardWidth) && mouseY > y && mouseY < (y+cardHeight);
  18. }
  19. void render()
  20. {
  21. shape(svg, x, y, cardWidth, cardHeight);
  22. }
  23. }

Deck类的render()方法在每个draw()循环中调用,帧率设置为10。
无论如何,代码工作得很好,唯一的问题在于两个svg的渲染方式,如您在这里所见:
为什么 SVG 呈现时倾斜了(Processing)?
由于某些原因,只有黑桃5红桃J的内部绘图被完全倾斜。
然而,当使用Inkscape或任何浏览器打开时,它们的显示方向是正确的,因此我感到困惑。
我尝试使用这两个svg的原始版本,但它们仍然被倾斜渲染。我尝试比较这两个svg的XML,但我无法理解可能导致问题的原因。
可能是Processing的问题,但我也不知道为什么或如何修复它。
对于任何帮助,我将不胜感激,提前感谢。
编辑
根据评论的要求,添加了主sketch文件。
但请注意,它被剥去了上部的灰色条和矩形。

  1. //全局对象。
  2. Deck deck;
  3. //全局对象。
  4. //全局变量。
  5. int edge = 2;
  6. int cardSpace = 15;
  7. int cardWidth = 100;
  8. int cardHeight = 140;
  9. //全局变量。
  10. void settings()
  11. {
  12. int w = cardWidth*8 + cardSpace*9;
  13. int h = cardHeight*5 + 32;
  14. size(w, h);
  15. }
  16. void setup()
  17. {
  18. frameRate(30);
  19. deck = new Deck(); //由于loadImage(),在此处初始化。
  20. }
  21. void draw()
  22. {
  23. background(60, 145, 50);
  24. deck.render();
  25. }
英文:

My aim is to remake the classic Freecell card game for Windows Xp on Windows 10 with Processing 3.<br>
To do so I downloaded from here a set of svg files that most resembled the old cards' look.<br>
In my code I have a class called Deck that contains an ArrayList of cards and, when constructed, it initializes the cards giving them the path for the svg file they need and after also x and y coordinates.<br>

  1. class Deck
  2. {
  3. String[] seeds = {&quot;HEART&quot;, &quot;DIAMOND&quot;, &quot;CLUB&quot;, &quot;SPADE&quot;};
  4. String[] values = {&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;, &quot;6&quot;, &quot;7&quot;, &quot;8&quot;, &quot;9&quot;, &quot;10&quot;, &quot;11&quot;, &quot;12&quot;, &quot;13&quot;};
  5. ArrayList&lt;Card&gt; cards = new ArrayList&lt;Card&gt;();
  6. Deck()
  7. {
  8. //Create the cards and store them into the array.
  9. for(int seed = 0; seed &lt; seeds.length; seed++)
  10. {
  11. for(int value = 0; value &lt; values.length; value++)
  12. {
  13. String cardName = seeds[seed] + &quot;-&quot; + values[value] + &quot;.svg&quot;;
  14. cards.add(new Card(cardName));
  15. }
  16. }
  17. //Shuffle the deck.
  18. //this.shuffle();
  19. //Give the cards x and y coordinates.
  20. for(int c = 0; c &lt; cards.size(); c++)
  21. {
  22. int x = cardSpace + (cardWidth+cardSpace)*(c%8);
  23. int y = 32 + cardHeight + edge*2 + cardSpace + 25*(c/8);
  24. cards.get(c).setxy(x, y);
  25. }
  26. }
  27. void shuffle()
  28. {
  29. for(int c = cards.size()-1; c &gt; 0; c--)
  30. {
  31. int k = (int)Math.floor(Math.random() * (c + 1));
  32. Card card = cards.get(c);
  33. cards.set(c, cards.get(k));
  34. cards.set(k, card);
  35. }
  36. }
  37. void render()
  38. {
  39. for(Card card : cards)
  40. {
  41. card.render();
  42. }
  43. }
  44. }

Card class:

  1. class Card
  2. {
  3. PShape svg;
  4. int x;
  5. int y;
  6. Card(String svgName)
  7. {
  8. this.svg = loadShape(svgName);
  9. }
  10. void setxy(int x, int y)
  11. {
  12. this.x = x;
  13. this.y = y;
  14. }
  15. boolean isMouseOver()
  16. {
  17. return mouseX &gt; x &amp;&amp; mouseX &lt; (x+cardWidth) &amp;&amp; mouseY &gt; y &amp;&amp; mouseY &lt; (y+cardHeight);
  18. }
  19. void render()
  20. {
  21. shape(svg, x, y, cardWidth, cardHeight);
  22. }
  23. }

The render() method of the class Deck is called once every draw() loop, with frameRate set to 10.<br>
Anyway, the code works fine, the only problem is in how two svg are rendered, as you can see here:
为什么 SVG 呈现时倾斜了(Processing)?
For some reasons, only the 5 of spade and the inner drawing of the jack of heart are completely tilted.<br>
However when opened with Inkscape or any browser they look correctly orientated, thus my confusion.<br>
I tried to use the original versions of those two svg, yet they are still rendered tilted. I tried to compare the xml of those two svg, but I couldn't understand what could be causing the problem.<br>
It may also be processing, but then I would have no idea why or how to fix it.<br>
Any help will be appreciated, thanks in advance.<br>

EDIT<br>
Adding main sketch file as requested in the comments.<br>
However note that it's stripped off of the upper gray bar and the rectangles.

  1. //Global objects.
  2. Deck deck;
  3. //Global objects.
  4. //Global variables.
  5. int edge = 2;
  6. int cardSpace = 15;
  7. int cardWidth = 100;
  8. int cardHeight = 140;
  9. //Global variables.
  10. void settings()
  11. {
  12. int w = cardWidth*8 + cardSpace*9;
  13. int h = cardHeight*5 + 32;
  14. size(w, h);
  15. }
  16. void setup()
  17. {
  18. frameRate(30);
  19. deck = new Deck(); //Initialized here because of loadImage().
  20. }
  21. void draw()
  22. {
  23. background(60, 145, 50);
  24. deck.render();
  25. }

答案1

得分: 0

已测试,并且这似乎修复了它。

在 SPADE-5.svg 中,删除第 84/85 行上的

  1. transform="rotate(180,1744.645,-315.5025)"

在 HEART-11-JACK.svg 中,删除第 123/124 行上的

  1. transform="rotate(0.33810995,363.27095,269.89449)"

(确保不要删除 &gt;

英文:

I tested it, and this seems to fix it.

In SPADE-5.svg, on line 84/85, remove

  1. transform=&quot;rotate(180,1744.645,-315.5025)&quot;

HEART-11-JACK.svg, on line 123/124, remove

  1. transform=&quot;rotate(0.33810995,363.27095,269.89449)&quot;

(make sure not to delete the &gt;)

huangapple
  • 本文由 发表于 2020年8月31日 08:40:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63663440.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定