简体   繁体   English

如何使用 PixiJS 渲染 SVG?

[英]How to render SVG with PixiJS?

I'm trying to make a game using SVG images for scalability and for procedurally making physical objects from them (see matter.js for how).我正在尝试使用 SVG 图像制作游戏以实现可扩展性并从它们中程序化地制作物理对象(请参阅matter.js了解如何)。

The problem I'm having is if I load 2 different SVG textures and then render them, the second has the first layered underneath it.我遇到的问题是,如果我加载 2 个不同的 SVG 纹理然后渲染它们,第二个将第一个分层放在它下面。

This doesn't happen with raster images and doesn't happen with the canvas options, only with WebGL.这不会发生在光栅图像上,也不会发生在画布选项上,只会发生在 WebGL 上。

Is there a way to stop this or am I doing the SVGs wrong?有没有办法阻止这种情况,还是我做错了 SVG?

 var renderer = PIXI.autoDetectRenderer( window.innerWidth, window.innerHeight, { backgroundColor : 0xffffff, resolution:2 } ); // add viewport and fix resolution doubling document.body.appendChild(renderer.view); renderer.view.style.width = "100%"; renderer.view.style.height = "100%"; var stage = new PIXI.Container(); //load gear svg var texture = PIXI.Texture.fromImage('https://upload.wikimedia.org/wikipedia/commons/thumb/0/0b/Gear_icon_svg.svg/2000px-Gear_icon_svg.svg.png'); var gear = new PIXI.Sprite(texture); //position and scale gear.scale = {x:0.1,y:0.1}; gear.position = {x:window.innerWidth / 2,y:window.innerHeight / 2}; gear.anchor = {x:0.5,y:0.5}; //load heart svg var texture2 = PIXI.Texture.fromImage('https://upload.wikimedia.org/wikipedia/commons/thumb/4/42/Love_Heart_SVG.svg/2000px-Love_Heart_SVG.svg.png'); var heart = new PIXI.Sprite(texture2); //position and scale heart.scale = {x:0.1,y:0.1}; heart.position = {x:window.innerWidth/4,y:window.innerHeight / 2}; heart.anchor = {x:0.5,y:0.5}; //add to stage stage.addChild(gear); stage.addChild(heart); // start animating animate(); function animate() { gear.rotation += 0.05; // render the container renderer.render(stage); requestAnimationFrame(animate); }
 <script src="https://github.com/pixijs/pixi.js/releases/download/v4.8.2/pixi.min.js"></script>

So I think you're mixing concepts a bit.所以我认为你有点混淆概念。

SVG is one thing and WebGL is another. SVG 是一回事,而 WebGL 是另一回事。 SVG's are rendered by the browser and you can scale them up or down without losing quality/resolution or whatever you want to call it. SVG 由浏览器呈现,您可以放大或缩小它们,而不会降低质量/分辨率或您想要的任何称呼。

This characteristic however is not possible in WebGL because WebGL rasterises images.然而,这个特性在 WebGL 中是不可能的,因为 WebGL 光栅化图像。 A bit like taking a screenshot and putting it in a layer on Photoshop.有点像截取屏幕截图并将其放在 Photoshop 上的图层中。 You can manipulate that image, but u can't scale it without starting to see the pixels.您可以操纵该图像,但您无法在不开始看到像素的情况下对其进行缩放。

So short answer is, you can't use SVG's in a WebGL hoping to make your graphics "scale".简短的回答是,您不能在 WebGL 中使用 SVG 来希望使您的图形“缩放”。

In regards to your example above, the result is the expected.关于你上面的例子,结果是预期的。

You are loading 2 png textures and overlaying them.您正在加载 2 个 png 纹理并覆盖它们。

Well, this example seems to work pretty well!好吧,这个例子似乎工作得很好!

var beeSvg = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/bee.svg";
beeTexture = new PIXI.Texture.fromImage(beeSvg, undefined, undefined, 1.0);
var bee = new PIXI.Sprite(beeTexture)

See more at: https://codepen.io/osublake/pen/ORJjGj查看更多: https : //codepen.io/osublake/pen/ORJjGj

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

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