Jump to content

Mixing canvas operations with Phaser


Hsaka
 Share

Recommended Posts

Hi, I'm trying to draw some primitive polygons and such using Canvas. I know there is a Graphics object which can be used to do the same thing, but there will be lots of polygons and the primitives will be modified every frame, and doing so with Graphics objects leads to lots of slowdown.

 

When mixing canvas operations and Phaser sprites, the canvas primitives are shifted around. Is there any way to render the primitives properly?

 

I modified the 'playing with points' example to show this, it simply adds a sprite:

<script type="text/javascript">    var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', {preload: preload, create: create,update:update,render:render});    var p1;    var p2;    var p3;    var p4;    var d2 = 0;    var d3 = 0;    var d4 = 0;        function preload() {      game.load.image('mushroom', 'assets/sprites/mushroom2.png');    }    function create() {        p1 = new Phaser.Point(game.world.centerX, game.world.centerY);        p2 = new Phaser.Point(p1.x - 50, p1.y - 50);        p3 = new Phaser.Point(p2.x - 50, p2.y - 50);        p4 = new Phaser.Point(p3.x - 50, p3.y - 50);                var test = game.add.sprite(200, 200, 'mushroom');    }    function update() {        p2.rotate(p1.x, p1.y, game.math.wrapAngle(d2), true, 150);        p3.rotate(p2.x, p2.y, game.math.wrapAngle(d3), true, 50);        p4.rotate(p3.x, p3.y, game.math.wrapAngle(d4), true, 100);        d2 += 1;        d3 += 4;        d4 += 6;    }    function render() {        game.context.strokeStyle = 'rgb(0,255,255)';        game.context.beginPath();        game.context.moveTo(p1.x, p1.y);        game.context.lineTo(p2.x, p2.y);        game.context.lineTo(p3.x, p3.y);        game.context.lineTo(p4.x, p4.y);        game.context.stroke();        game.context.closePath();        game.context.fillStyle = 'rgb(255,255,0)';        game.context.fillRect(p1.x, p1.y, 4, 4);        game.context.fillStyle = 'rgb(255,0,0)';        game.context.fillRect(p2.x, p2.y, 4, 4);        game.context.fillStyle = 'rgb(0,255,0)';        game.context.fillRect(p3.x, p3.y, 4, 4);        game.context.fillStyle = 'rgb(255,0,255)';        game.context.fillRect(p4.x, p4.y, 4, 4);    }</script>
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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