// ************** 02_09 **************
// *********************** (06) お絵描き *******************************
// *********************** sample6(drawing) ****************************
//*** 問題2-6 ***
//【C】○
var pt = new Array(); //点の配列(線が描かれているが、実は点の連なり)
var loc = new Array(); //自分のホームポジションの配列
var buttonHeight = 100; //上部のボタンの高さ
var fontSize = 24; //ボタンに表示される文字の大きさ
var curMode = "DRAW"; //現在のモード、"DRAW"か"MOVE"
function setup(){
}
function loop(){
for(var i = 0; i < pt.length; ++i){
//それぞれの点について
//自分の場所に帰る
pt[i].setKasokudo((loc[i].x - pt[i].x)*20, (loc[i].y - pt[i].y)*20);
//curModeがMOVEだったら
if(curMode == "MOVE"){
var maxDist = 100;
var dist = Math.sqrt(Math.pow(pt[i].x - (curYubiX-75), 2)
+ Math.pow(pt[i].y - (curYubiY-85), 2));
//指と点が近ければ1、遠ければ0
if(dist < maxDist){
var par = (maxDist-dist)/maxDist;
pt[i].setKasokudo(((curYubiX-75) - preYubiX)*par*300,
((curYubiY-85) - preYubiY)*par*300);
}
}
}
//描画
pbCtx.clearRect(0, 0, screenWidth, screenHeight);
pbCtx.lineWidth = 8;
pbCtx.lineJoin = "round";
pbCtx.beginPath();
for(var i = 0; i < pt.length-1; ++i){
if(i == 0){
pbCtx.moveTo((pt[i].x+pt[i+1].x)/2, (pt[i].y+pt[i+1].y)/2);
}else{
pbCtx.quadraticCurveTo(pt[i].x, pt[i].y, (pt[i].x+pt[i+1].x)/2, (pt[i].y+pt[i+1].y)/2);
}
}
pbCtx.stroke();
//ボタンの描画
pbCtx.lineWidth = 1;
pbCtx.textAlign = "center";
pbCtx.font = fontSize+"px 'Times New Roman'";
if(curMode == "DRAW"){
pbCtx.fillStyle = "black";
pbCtx.beginPath();
pbCtx.rect(screenWidth/2*0, 0, screenWidth/2, buttonHeight);
pbCtx.fill();
pbCtx.fillStyle = "white";
pbCtx.fillText("draw", screenWidth/4, buttonHeight/2+fontSize/3);
pbCtx.fillStyle = "black";
pbCtx.beginPath();
pbCtx.rect(screenWidth/2*1, 0, screenWidth/2, buttonHeight);
pbCtx.stroke();
pbCtx.fillText("move", screenWidth/4*3, buttonHeight/2+fontSize/3);
}else{
pbCtx.fillStyle = "black";
pbCtx.beginPath();
pbCtx.rect(screenWidth/2*0, 0, screenWidth/2, buttonHeight);
pbCtx.stroke();
pbCtx.fillText("draw", screenWidth/4, buttonHeight/2+fontSize/3);
pbCtx.fillStyle = "black";
pbCtx.beginPath();
pbCtx.rect(screenWidth/2*1, 0, screenWidth/2, buttonHeight);
pbCtx.fill();
pbCtx.fillStyle = "white";
pbCtx.fillText("move", screenWidth/4*3, buttonHeight/2+fontSize/3);
}
}
function onPressed(n){
//「以前の指の場所」を初期化(今の指の場所に)して
preYubiX = (curYubiX-75);
preYubiY = (curYubiY-85);
//カンバス内が押されていたのなら、且つ「描く」モードなら、
//ptとlocを初期化して、1番目の点として指の場所を入れる
if((curYubiY-85) > buttonHeight){
if(curMode == "DRAW"){
pt = new Array();
pt.push(new PhysicalPoint((curYubiX-75), (curYubiY-85)));
loc = new Array();
loc.push(new PhysicalPoint((curYubiX-75), (curYubiY-85)));
}
//ボタンが押されていたのなら
}else{
//DRAWが押されていたら、モードをDRAWに
//MOVEが押されていたら、モードをMOVEに
if((curYubiX-75) <= screenWidth/2){
curMode = "DRAW";
pt = new Array(); // 18.10.13 add
pt.push(new PhysicalPoint((curYubiX-75), (curYubiY-85))); // 18.10.13 add
loc = new Array(); // 18.10.13 add
loc.push(new PhysicalPoint((curYubiX-75), (curYubiY-85))); // 18.10.13 add
}else if(screenWidth/2 < (curYubiX-75)){
curMode = "MOVE";
}
}
}
function onMove(n){
//もしDRAWモードだったら
//ptとlocに今の指の位置をどんどんと追加していく
if(curMode == "DRAW"){
if(curYubiTouched){
pt.push(new PhysicalPoint((curYubiX-75), (curYubiY-85)));
loc.push(new PhysicalPoint((curYubiX-75), (curYubiY-85)));
}
}
}
function onReleased(n){
}