2つの円シェイプがあり、一つは固定で一つはマウスに応じて動きます。
その2点間を線でつないでいるのですが、円を動かしてもその線が追尾するようにしたいのですが、(常に二点間の中央を結ぶ線という関係)下記ですと、
動かす旅に新たな線が描かれてしまいます・・どのようにすればいいでしょうか。
<!doctype html> <html> <head> <meta charset="utf-8"> <title></title> <link rel="stylesheet" href="css/base.css" /> <meta name="viewport" content="width=device-width" /> <script src="https://code.createjs.com/1.0.0/createjs.min.js"></script> </head> <body> <canvas id="myCanvas" width="1000px" height="600"></canvas> <script> var mouseX = 0, mouseY = 0; var stage = ""; var shape,shape2 = ""; var line = ""; var lshape = ""; window.addEventListener("load", init); function init() { // Stageオブジェクトを作成。表示リストのルートになります。 stage = new createjs.Stage("myCanvas"); // シェイプを作成 shape = new createjs.Shape(); // 塗りの色を指定 shape.graphics.beginFill("#000"); // 円を描く shape.graphics.drawCircle(0, 0, 80); stage.addChild(shape); // 表示リストに追加 // 配置座標を設定 shape.x = 200; shape.y = 100; // Stageの描画を更新 // シェイプを作成 shape2 = new createjs.Shape(); // 塗りの色を指定 shape2.graphics.beginFill("#ff0000"); // 円を描く shape2.graphics.drawCircle(0, 0, 80); stage.addChild(shape2); // 表示リストに追加 // 配置座標を設定 shape2.x = 100; shape2.y = 300; // Stageの描画を更新 //線 line = new createjs.Graphics(); //線の色を指定 line.beginStroke("#ddd"); //線の始点へ移動 line.moveTo(shape.x,shape.y); //線の終点を指定して、線を描く line.lineTo(shape2.x,shape2.y); lshape = new createjs.Shape(line); stage.addChild(lshape); // 表示リストに追加 document.addEventListener( 'mousemove', onDocumentMouseMove, false ); stage.update(); animate(); } function onDocumentMouseMove(event) { mouseX = event.clientX; mouseY = event.clientY; console.log(mouseX,mouseY); } function animate() { requestAnimationFrame( animate ); render(); } function render() { shape.x += ( mouseX - shape.x ) * .05; shape.y += ( mouseY - shape.y ) * .05; line.lineTo(shape.x,shape.y); stage.update(); } </script> </body> </html>
あなたの回答
tips
プレビュー