// ************** 02_10 **************
// *********************** (07) お絵描き(複数線)****************************
// *********************** sample7(drawingMultiLines) ************************
var pt = new Array();
var loc = new Array();
var curPt = null;
var curLoc = null;
var buttonHeight = 100;
var fontSize = 24;
var curMode = "DRAW"; //DRAW, MOVE
function setup(){
}
function loop(){
for(var i = 0; i < pt.length; ++i){
for(var j = 0; j < pt[i].length; ++j){
//それぞれの点について
//自分の場所に帰る
pt[i][j].setKasokudo((loc[i][j].x - pt[i][j].x)*20,
(loc[i][j].y - pt[i][j].y)*20);
//curModeがMOVEだったら
if(curMode == "MOVE"){
var maxDist = 100;
var dist = Math.sqrt(Math.pow(pt[i][j].x - (curYubiX-75), 2)
+ Math.pow(pt[i][j].y - (curYubiY-85), 2));
//指と点が近ければ1、遠ければ0
if(dist < maxDist){
var bai = (maxDist-dist)/maxDist;
pt[i][j].setKasokudo(((curYubiX-75) - preYubiX)*bai*300,
((curYubiY-85) - preYubiY)*bai*300);
}
}
}
}
//描画
pbCtx.clearRect(0, 0, screenWidth, screenHeight);
for(var i = 0; i < pt.length; ++i){
pbCtx.lineWidth = 8;
pbCtx.beginPath();
for(var j = 0; j < pt[i].length-1; ++j){
if(j == 0){
pbCtx.moveTo(pt[i][j].x, pt[i][j].y);
}else{
pbCtx.quadraticCurveTo(pt[i][j].x,pt[i][j].y,
(pt[i][j].x+pt[i][j+1].x)/2,
(pt[i][j].y+pt[i][j+1].y)/2);
}
}
pbCtx.stroke();
}
//ボタンの描画
pbCtx.lineWidth = 1;
pbCtx.lineCap = "round";
pbCtx.textAlign = "center";
pbCtx.font = fontSize+"px 'Times New Roman'";
if(curMode == "DRAW"){
pbCtx.fillStyle = "black";
pbCtx.beginPath();
pbCtx.rect(screenWidth/3*0, 0, screenWidth/3, buttonHeight);
pbCtx.fill();
pbCtx.fillStyle = "white";
pbCtx.fillText("draw", screenWidth/6, buttonHeight/2+fontSize/3);
pbCtx.fillStyle = "black";
pbCtx.beginPath();
pbCtx.rect(screenWidth/3*1, 0, screenWidth/3, buttonHeight);
pbCtx.stroke();
pbCtx.fillText("move", screenWidth/6*3, buttonHeight/2+fontSize/3);
pbCtx.fillStyle = "black";
pbCtx.beginPath();
pbCtx.rect(screenWidth/3*2, 0, screenWidth/3, buttonHeight);
pbCtx.stroke();
pbCtx.fillText("reset", screenWidth/6*5, buttonHeight/2+fontSize/3);
}else{
pbCtx.fillStyle = "black";
pbCtx.beginPath();
pbCtx.rect(screenWidth/3*0, 0, screenWidth/3, buttonHeight);
pbCtx.stroke();
pbCtx.fillText("draw", screenWidth/6, buttonHeight/2+fontSize/3);
pbCtx.fillStyle = "black";
pbCtx.beginPath();
pbCtx.rect(screenWidth/3*1, 0, screenWidth/3, buttonHeight);
pbCtx.fill();
pbCtx.fillStyle = "white";
pbCtx.fillText("move", screenWidth/6*3, buttonHeight/2+fontSize/3);
pbCtx.fillStyle = "black";
pbCtx.beginPath();
pbCtx.rect(screenWidth/3*2, 0, screenWidth/3, buttonHeight);
pbCtx.stroke();
pbCtx.fillText("reset", screenWidth/6*5, buttonHeight/2+fontSize/3);
}
}
function onPressed(n){
//「以前の指の場所」を初期化(今の指の場所に)して
preYubiX = (curYubiX-75);
preYubiY = (curYubiY-85);
//カンバス内が押されていたのなら、且つ「描く」モードなら、
//ptとlocを初期化して、1番目の点として指の場所を入れる
if((curYubiY-85) > 100){
if(curMode == "DRAW"){
pt.push(new Array());
curPt = pt[pt.length-1];
curPt.push(new PhysicalPoint((curYubiX-75), (curYubiY-85)));
loc.push(new Array());
curLoc = loc[loc.length-1];
curLoc.push(new PhysicalPoint((curYubiX-75), (curYubiY-85)));
}
//ボタンが押されていたのなら
}else{
//DRAWが押されていたら、モードをDRAWに
//MOVEが押されていたら、モードをMOVEに
if(0 < (curYubiX-75) && (curYubiX-75) <= screenWidth/3*1){
curMode = "DRAW";
// 18.10.15 update start
pt.push(new Array());
curPt = pt[pt.length-1];
curPt.push(new PhysicalPoint((curYubiX-75), (curYubiY-85)));
loc.push(new Array());
curLoc = loc[loc.length-1];
curLoc.push(new PhysicalPoint((curYubiX-75), (curYubiY-85)));
// 18.10.15 update end
}else if(screenWidth/3*1 < (curYubiX-75) && (curYubiX-75) <= screenWidth/3*2){
curMode = "MOVE";
}else{
curPt = null;
curLoc = null;
pt = new Array();
loc = new Array();
curMode = "DRAW"
}
}
}
function onMove(n){
//もしDRAWモードだったら
//ptとlocに今の指の位置をどんどんと追加していく
if(curMode == "DRAW"){
if(curYubiTouched){
if(curPt != null && curLoc != null){
curPt.push(new PhysicalPoint((curYubiX-75), (curYubiY-85)));
curLoc.push(new PhysicalPoint((curYubiX-75), (curYubiY-85)));
}
}
}
}
function onReleased(n){
preYubiX = (curYubiX-75);
preYubiY = (curYubiY-85);
}