Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

146
Views
PixiJS: Draw line with mousemove

Pixijs I'm trying to draw a single line using OnMouseMove(), my problem is that everytime I move the mouse It leaves a trail

my progress codepen

var sprite = new PIXI.Graphics();
let initPointer = null;

const onMouseMove = (e) => {
  if (initPointer == null) return;
  sprite.lineStyle(2, 0xff0000, 1);
  sprite.moveTo(initPointer.x, initPointer.y);

  const mousePosRef = getMousePos(e);
  sprite.lineTo(mousePosRef.x, mousePosRef.y);
  console.log(sprite);

  annoRef.addChild(sprite);
  sprite = new PIXI.Graphics();
};
const onMouseDown = (e) => {
  const mousePosRef = getMousePos(e);
  initPointer = mousePosRef;
  // sprite.moveTo(initPointer.x, initPointer.y);
};

enter image description here

what I need, any help

enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Changed your code a little bit: https://codepen.io/domis86/pen/LYOYrVx

main points:

  • see isMouseButtonDown variable usage
  • sprite.clear(); executed in onMouseMove - so on each move the Graphics object properties are resetted and one line is drawn. Previously on each move new line was "added" to Graphics object using sprite.moveTo and sprite.lineTo.
  • sprite = new PIXI.Graphics(); is executed in onMouseDown instead of onMouseUp. This way we create new Graphics object (a "line") when mouse is clicked - is more natural behaviour IMO.
  • Please rename your variable sprite because it suggests this is a Sprite ( https://pixijs.download/dev/docs/PIXI.Sprite.html ) but in reality it Graphics :) So line or graphics would be more suitable names IMHO ;)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!