Jump to content

Loading SVG file


kemz
 Share

Recommended Posts

  • 1 year later...

Hi. A little late in the response, but you can load svg images in Phaser using the bitmapData object.

bitmapData is a versatile feature that enables you to render stuff in HTML5 canvas, so technically, anything that you can draw on HTML5 canvas, you should be able to do so in Phaser too. Here's what I did:
var mySprite = game.make.bitmapData(300,400) //create a bitmap data object, but not actually adding it yet to the stage

You can then use ctx (context in HTML5 canvas) to draw your svg. For example,

mySprite.ctx.beginPath();

 

So how do you get your SVG into your code? I drew an image in Inkscape, using the latest version (0.91) and there is a Save As...>HTML 5 Canvas option.

Just copy and paste the generated portion to your code, and add your var name to prefix it.
ctx.save();
    ctx.transform(1.000000, 0.000000, 0.000000, 1.000000, -188.124050, -127.935690);
    

From Inkscape generateed HTML5 canvas
// #path2985
    ctx.beginPath(); //
    ctx.lineJoin = 'miter';
    ctx.strokeStyle = 'rgb(40, 33, 155)';
    ctx.lineCap = 'butt';
    ctx.miterLimit = 4;
    ctx.lineWidth = 10.400000;
    ctx.fillStyle = 'rgb(255, 230, 128)';
    ctx.moveTo(217.142860, 598.076470);
    ctx.lineTo(297.142860, 495.219330);
    ctx.lineTo(297.142860, 452.362180);
    ctx.lineTo(254.285710, 429.505040);
    ctx.bezierCurveTo(170.677070, 536.339150, 190.894420, 413.272840, 211.428570, 289.505040);
    ctx.lineTo(268.571430, 209.505040);
    ctx.bezierCurveTo(325.763380, 160.357620, 378.200010, 74.752689, 457.142860, 192.362180);
    ctx.bezierCurveTo(466.318390, 224.115920, 481.143040, 254.457370, 431.428570, 300.933610);
    ctx.lineTo(474.285710, 340.933610);
    ctx.lineTo(425.714290, 355.219330);
    ctx.lineTo(422.857140, 418.076470);
    ctx.bezierCurveTo(384.479520, 425.449090, 354.572490, 411.645250, 331.428570, 380.933610);
    ctx.bezierCurveTo(348.567220, 403.797060, 338.183020, 453.659070, 382.857140, 435.219320);
    ctx.lineTo(431.428570, 440.933610);
    ctx.lineTo(428.571430, 449.505040);
    ctx.lineTo(408.571430, 466.647900);
    ctx.lineTo(374.285710, 483.790760);
    ctx.lineTo(420.000000, 520.933610);
    ctx.bezierCurveTo(420.000000, 520.933610, 517.142860, 535.219330, 522.857140, 560.933610);
    ctx.bezierCurveTo(528.571430, 586.647900, 551.428570, 598.076470, 551.428570, 598.076470);
    ctx.fill();
    ctx.stroke();
    ctx.restore();

 

Simply add mySprite to each of the lines..

mySprite.ctx.beginPath(); //
    mySprite.ctx.lineJoin = 'miter';
   mySprite. ctx.strokeStyle = 'rgb(40, 33, 155)';
    mySprite.ctx.lineCap = 'butt';
    mySprite.ctx.miterLimit = 4;
    mySprite.ctx.lineWidth = 10.400000;
    mySprite.ctx.fillStyle = 'rgb(255, 230, 128)';
    mySprite.ctx.moveTo(217.142860, 598.076470);
    mySprite.ctx.lineTo(297.142860, 495.219330);
   mySprite. ctx.lineTo(297.142860, 452.362180);
    mySprite.ctx.lineTo(254.285710, 429.505040);

etc..

 

Then, you can add a sprite, then either add the bitmapData mySPrite as a child, or use the cache key as the image to load for the sprite.

 

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...