Jump to content

Draw lines on mousedown


Gregory
 Share

Recommended Posts

Hi,

 

I would like to draw lines when the user is dragging his mouse. I have a working solution bellow but it is really bad, especially because of this line, it's adding a billion sprites to the game so it becomes super slow really quickly :D

this.sprite = this.game.add.sprite(0, 0, this.bmd);

Code:

  'use strict';  function Play() {     this.isDragging = false;    this.lastPoint = null;  }  Play.prototype = {    create: function() {      // create a new bitmap data object      this.bmd = this.game.add.bitmapData(this.game.width, this.game.height);      // draw to the canvas context like normal      this.bmd.ctx.strokeStyle = 'rgb( 77, 77, 77)';      this.bmd.ctx.lineWidth   = 10;      this.bmd.ctx.lineCap     = 'round';      this.bmd.ctx.fillStyle = '#ff0000';      this.sprite = this.game.add.sprite(0, 0, this.bmd);    },    update: function() {      if(this.game.input.mousePointer.isUp) {        this.isDragging = false;        this.lastPoint = null;      }      if (this.game.input.mousePointer.isDown) {        console.log('down');        this.isDragging = true;        this.bmd.ctx.beginPath();                        var newPoint = new Phaser.Point(this.game.input.x, this.game.input.y);        if(this.lastPoint) {          this.bmd.ctx.moveTo(this.lastPoint.x, this.lastPoint.y);          this.bmd.ctx.lineTo(newPoint.x, newPoint.y);        }        this.lastPoint = newPoint;        this.bmd.ctx.stroke();        // That's the line I need to change        this.sprite = this.game.add.sprite(0, 0, this.bmd);      }    }  };    module.exports = Play;

Ideally i would like to redraw my sprite while the mouse is moving but I could find how to do so in the documentation.

Does anyone has an idea? 

 
 
Thanks!
Link to comment
Share on other sites

Look at using a graphics object instead of a billion sprites...

 

http://docs.phaser.io/Phaser.Graphics.html

 

You can draw lines from point to point, so at regular intervals draw a new line from the last point to the cursor...

 

 

Edit: Just actually read your code...

Lemme think again...

 

Still think you could be better off with a graphics object instead of the bitmap data/sprite combination...

Link to comment
Share on other sites

I'm not sure what advantages using Graphics may have over a BitmapData backed Sprite (I haven't looked much at the Graphics type) but if you find you need to go back to your original approach to get the effect you're after, I can offer a couple of pointers:

 

The line you marked as needing to change should simply go away; you already have a Sprite that's using the BitmapData object to render, so you don't need to create additional ones. Instead, you need to mark the BitmapData instance as dirty so it knows to re-render itself: this.bmd.dirty = true.

 

If you run into performance issues as a result of marking the bmd dirty on every update, you might also consider checking that the pointer has actually moved away from lastPoint before drawing and dirty-marking.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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