Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Draw elastic curve in Canvas?

Participant ,
Oct 05, 2017 Oct 05, 2017

I want to draw a curve that starts at a set location and the endpoint follows the mouse coordinates.  I want the simulate connecting a wire from one point to another. My code does this but it draws multiple curves on the canvas. I need to find a way to clear each curve before the next one is drawn. It breaks when I use graphics.clear(); stage.update();

var startX=150;

var startY=150;

this.addEventListener('tick', drawCurve.bind(this));

function drawCurve(){

var cptX=(stage.mouseX-startX)/3;

var cptY=(stage.mouseY-startY)*4;

   

var cord1 = new createjs.Shape();

stage.addChild(cord1);

cord1.graphics.setStrokeStyle(4).beginStroke("#ff0000");   

cord1.graphics.moveTo(startX, startY);

cord1.graphics.quadraticCurveTo(cptX, cptY, stage.mouseX, stage.mouseY);

cord1.graphics.endStroke();

cord1.graphics.clear();

stage.update();

}

617
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 05, 2017 Oct 05, 2017

use:

var startX=150;
var startY=150;

this.addEventListener('tick', drawCurve.bind(this));

function drawCurve(){

var cptX=(stage.mouseX-startX)/3;
var cptY=(stage.mouseY-startY)*4;
   
var cord1 = new createjs.Shape();
stage.addChild(cord1);
cord1.graphics.setStrokeStyle(4).beginStroke("#ff0000");   
cord1.graphics.moveTo(startX, startY);
cord1.graphics.quadraticCurveTo(cptX, cptY, stage.mouseX, stage.mouseY);
cord1.graphics.endStroke();

cord1.graphics.clear();
stage.update();

}
var startX=150;
var startY=150;

v

...
Translate
Community Expert ,
Oct 05, 2017 Oct 05, 2017

use:

var startX=150;
var startY=150;

this.addEventListener('tick', drawCurve.bind(this));

function drawCurve(){

var cptX=(stage.mouseX-startX)/3;
var cptY=(stage.mouseY-startY)*4;
   
var cord1 = new createjs.Shape();
stage.addChild(cord1);
cord1.graphics.setStrokeStyle(4).beginStroke("#ff0000");   
cord1.graphics.moveTo(startX, startY);
cord1.graphics.quadraticCurveTo(cptX, cptY, stage.mouseX, stage.mouseY);
cord1.graphics.endStroke();

cord1.graphics.clear();
stage.update();

}
var startX=150;
var startY=150;

var cord1 = new createjs.Shape();

stage.addChild(cord1);

cord1.graphics.setStrokeStyle(4).



this.addEventListener('tick', drawCurve.bind(this));

function drawCurve(){

var cptX=(stage.mouseX-startX)/3;  // these don't look like they'd be correct.
var cptY=(stage.mouseY-startY)*4;
    /*
these are more likely what you want:

var cptX = startX+(stage.mouseX - startX) / 2;

var cptY = startY+Math.abs(stage.mouseY - startY) * 4;

*/
cord1.graphics.clear();

cord1.graphics.beginStroke("#ff0000");   
cord1.graphics.moveTo(startX, startY);
cord1.graphics.quadraticCurveTo(cptX, cptY, stage.mouseX, stage.mouseY);
cord1.graphics.endStroke();

}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 05, 2017 Oct 05, 2017

Thanks for your reply.  Did you try this?  It didn't work for me.  No curves were drawn,

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 05, 2017 Oct 05, 2017

remove the typo:

var startX=150;

var startY=150;

var cord1 = new createjs.Shape();

stage.addChild(cord1);

cord1.graphics.setStrokeStyle(4);

this.addEventListener('tick', drawCurve.bind(this));

function drawCurve(){

var cptX=(stage.mouseX-startX)/3;  // these don't look like they'd be correct.

var cptY=(stage.mouseY-startY)*4;

    /*

these are more likely what you want:

var cptX = startX+(stage.mouseX - startX) / 2;

var cptY = startY+Math.abs(stage.mouseY - startY) * 4;

*/

cord1.graphics.clear();

cord1.graphics.beginStroke("#ff0000");   

cord1.graphics.moveTo(startX, startY);

cord1.graphics.quadraticCurveTo(cptX, cptY, stage.mouseX, stage.mouseY);

cord1.graphics.endStroke();

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 05, 2017 Oct 05, 2017

It does work! Your changes for the control point is a nice improvement too. Thanks!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 05, 2017 Oct 05, 2017
LATEST

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines