pictcode / app / webroot / js / pictcode / vector_input.js @ 7b85fad4
履歴 | 表示 | アノテート | ダウンロード (10.199 KB)
1 |
define(["canvas_manager", "image_manager", "modal_manager", "sprite_drawer"], function(canvasManager, imageManager, modalManager, spriteDrawer){ |
---|---|
2 |
|
3 |
var canvas;
|
4 |
var canvasContext;
|
5 |
|
6 |
var inputWorldSize = {width:3000, height:2400}; |
7 |
var inputAreaSize = spriteDrawer.editorInnerSize;
|
8 |
var displayAreaSize = spriteDrawer.dislayInnerSize;
|
9 |
var dpInpRatio = displayAreaSize.width/inputAreaSize.width;
|
10 |
var inputAreaPosition = {x:0, y:0}; |
11 |
|
12 |
var pointer;
|
13 |
var isOnPointer = false; |
14 |
var dummyImage;
|
15 |
|
16 |
var previousPosition;
|
17 |
var cvInpRatio;
|
18 |
var distanceRatio = 1.0; |
19 |
var isDragging = false; |
20 |
|
21 |
//四捨五入した角度と距離
|
22 |
var settledAngle360 = 0; |
23 |
var settledDistance = 0; |
24 |
|
25 |
var didSelectVector;
|
26 |
|
27 |
function openVectorInput(value, dSlctVctr){ |
28 |
|
29 |
didSelectVector = dSlctVctr; |
30 |
|
31 |
$("#modal_coding_vector_input").html(""); |
32 |
|
33 |
$("#modal_coding_vector_input")[0].style.display = "inline"; |
34 |
$("#modal_coding_vector_variable")[0].style.display = "none"; |
35 |
|
36 |
var modalSize = document.documentElement.clientHeight * 0.75; |
37 |
$("#modal_coding_vector").css({width:modalSize, height:modalSize, bottom:0, display:"none"}); |
38 |
|
39 |
pointer = {image:imageManager.images.input_vector_003, size:100, position:{x:0, y:0}, angle:0}; |
40 |
dummyImage = {image:imageManager.images.input_vector_001, size:150}; |
41 |
|
42 |
inputAreaPosition = {x:0, y:0}; |
43 |
|
44 |
var canvasSize = canvasManager.getCanvasSize(modalSize*0.8, modalSize*0.8); |
45 |
canvas = canvasManager.initializeCanvas(canvasSize.width, canvasSize.height, touchStart, touchMove, touchEnd); |
46 |
canvasContext = canvas.getContext("2d");
|
47 |
canvas.style.border = "2px solid black";
|
48 |
cvInpRatio = canvas.width/inputAreaSize.width; |
49 |
$("#modal_coding_vector_input")[0].appendChild(canvas); |
50 |
|
51 |
pointer.position.x = value.value.x; |
52 |
pointer.position.y =value.value.y; |
53 |
pointer.angle = Math.atan2(pointer.position.y, pointer.position.x); |
54 |
|
55 |
drawCanvas(); |
56 |
modalManager.showUpModal($("#modal_coding_vector")); |
57 |
} |
58 |
|
59 |
function drawCanvas(){ |
60 |
canvasContext.clearRect(0, 0, canvas.width, canvas.height); |
61 |
//背景
|
62 |
var cvPositionFromInit = {x:inputAreaPosition.x*cvInpRatio, y:-inputAreaPosition.y*cvInpRatio}; |
63 |
// var cvBgDrawInitLeftTop = {x:(-inputWorldSize.width/2+inputAreaSize.width/2)*cvInpRatio, y:(-inputWorldSize.height/2+inputAreaSize.height/2)*cvInpRatio};
|
64 |
// var cvBgDrawSize = {width:canvas.width/inputAreaSize.width*inputWorldSize.width, height:canvas.height/inputAreaSize.height*inputWorldSize.height};
|
65 |
// canvasContext.drawImage(imageManager.images.bg_main_001, cvBgDrawInitLeftTop.x-cvPositionFromInit.x, cvBgDrawInitLeftTop.y-cvPositionFromInit.y, cvBgDrawSize.width, cvBgDrawSize.height);
|
66 |
//グリッド
|
67 |
var cvGrDrawInitLeftTop = {x:0, y:0}; |
68 |
var cvGrDrawSize = {width:canvas.width, height:canvas.height}; |
69 |
canvasContext.globalAlpha = 0.25;
|
70 |
canvasContext.drawImage(imageManager.images.bg_grid_001, cvGrDrawInitLeftTop.x-cvPositionFromInit.x, cvGrDrawInitLeftTop.y-cvPositionFromInit.y, cvGrDrawSize.width, cvGrDrawSize.height); |
71 |
canvasContext.globalAlpha = 1.0;
|
72 |
//ポインターの座標
|
73 |
var cvCenter = {x:canvas.width/2, y:canvas.height/2}; |
74 |
var inpAreaLeftTopPosition = {x:inputAreaPosition.x-inputAreaSize.width/2, y:inputAreaPosition.y+inputAreaSize.height/2}; |
75 |
var inpPointerLeftTopPosition = {x:pointer.position.x-pointer.size/2, y:pointer.position.y}; |
76 |
var cvPointerLeftTopPosition = {x:(inpPointerLeftTopPosition.x - inpAreaLeftTopPosition.x)*cvInpRatio, y:(inpAreaLeftTopPosition.y - inpPointerLeftTopPosition.y)*cvInpRatio}; |
77 |
var cvTapSize = {width:pointer.size*cvInpRatio, height:pointer.size*cvInpRatio}; |
78 |
var pointerLength = Math.sqrt(pointer.position.x*pointer.position.x + pointer.position.y*pointer.position.y);
|
79 |
var cvPointerSize = {width:pointer.size*cvInpRatio, height:pointerLength*cvInpRatio}; |
80 |
var cvPointerCenter = {x:cvPointerLeftTopPosition.x+cvPointerSize.width/2, y:cvPointerLeftTopPosition.y+cvPointerSize.width/2}; |
81 |
pointer.drawnRect = {x:cvPointerLeftTopPosition.x, y:cvPointerLeftTopPosition.y, width:cvPointerSize.width, height:cvPointerSize.height}; |
82 |
pointer.tapRect = {x:cvPointerLeftTopPosition.x - cvTapSize.width/2*Math.cos(pointer.angle), y:cvPointerLeftTopPosition.y - cvTapSize.width*(-0.5*Math.sin(pointer.angle) + 0.5), width:cvTapSize.width, height:cvTapSize.height}; |
83 |
// canvasContext.fillStyle = 'rgb(200,200,200)';
|
84 |
// canvasContext.fillRect(pointer.tapRect.x, pointer.tapRect.y, pointer.tapRect.width, pointer.tapRect.height);
|
85 |
//ポインターの回転と描画
|
86 |
canvasContext.save(); |
87 |
canvasContext.translate(cvPointerLeftTopPosition.x+cvPointerSize.width/2, cvPointerLeftTopPosition.y);
|
88 |
canvasContext.rotate( Math.PI/2-pointer.angle);
|
89 |
canvasContext.translate(-(cvPointerLeftTopPosition.x+cvPointerSize.width/2), -cvPointerLeftTopPosition.y);
|
90 |
canvasContext.drawImage(pointer.image, pointer.drawnRect.x, pointer.drawnRect.y, pointer.drawnRect.width, pointer.drawnRect.height); |
91 |
canvasContext.restore(); |
92 |
//ダミー画像の表示
|
93 |
var cvDmDrawSize = {width:dummyImage.size*cvInpRatio, height:dummyImage.size*cvInpRatio}; |
94 |
var cvDmDrawInitLeftTop = {x:canvas.width/2-cvDmDrawSize.width/2 - inputAreaPosition.x*cvInpRatio, y:canvas.height/2-cvDmDrawSize.height/2 + inputAreaPosition.y*cvInpRatio}; |
95 |
canvasContext.drawImage(dummyImage.image, cvDmDrawInitLeftTop.x, cvDmDrawInitLeftTop.y, cvDmDrawSize.width, cvDmDrawSize.height); |
96 |
//円の描画
|
97 |
canvasContext.beginPath(); |
98 |
canvasContext.arc(cvPointerCenter.x - cvPointerSize.width/2*Math.cos(pointer.angle), cvPointerCenter.y - cvPointerSize.width*(-0.5*Math.sin(pointer.angle) + 0.5), cvPointerSize.width/2, 0, Math.PI*2, false); |
99 |
canvasContext.lineWidth = canvas.width*0.005;
|
100 |
canvasContext.strokeStyle = "red";
|
101 |
canvasContext.stroke(); |
102 |
//数値の背景の描画
|
103 |
var numbgWidth = canvas.width*0.4; |
104 |
var numbgHeight = numbgWidth*0.4; |
105 |
canvasContext.drawImage(imageManager.images.input_vector_005, canvas.width*0.02, canvas.height*0.78, numbgWidth, numbgHeight); |
106 |
canvasContext.drawImage(imageManager.images.input_vector_006, canvas.width*0.55, canvas.height*0.78, numbgWidth, numbgHeight); |
107 |
//数値の描画
|
108 |
var angle360 = pointer.angle / Math.PI *180; |
109 |
if (angle360 < 0) { |
110 |
angle360 += 360;
|
111 |
}; |
112 |
angle360 /= 10;
|
113 |
angle360 = Math.round(angle360); |
114 |
angle360 *= 10;
|
115 |
if (angle360 == 360) {angle360 = 0;}; |
116 |
settledAngle360 = angle360; |
117 |
angle360 += '°';
|
118 |
var distance = Math.sqrt(pointer.position.x*pointer.position.x + pointer.position.y*pointer.position.y);
|
119 |
var dpDistance = distance * dpInpRatio;
|
120 |
dpDistance /= 5;
|
121 |
dpDistance = Math.round(dpDistance); |
122 |
dpDistance *= 5;
|
123 |
// dpDistance *= 5;
|
124 |
// distance /= 10;
|
125 |
// distance = Math.round(distance);
|
126 |
// distance *= 10;
|
127 |
settledDistance = dpDistance / dpInpRatio; |
128 |
var fontSize = canvas.width*0.070; |
129 |
canvasContext.font = "italic bold "+ fontSize + "px " + "sans-serif"; |
130 |
// canvasContext.fillStyle = "blue";
|
131 |
canvasContext.fillText(angle360, canvas.width*0.21, canvas.height*0.92); |
132 |
canvasContext.fillText(dpDistance, canvas.width*0.73, canvas.height*0.92); |
133 |
} |
134 |
|
135 |
function touchStart(x, y){ |
136 |
if (pointer.tapRect.x<x && pointer.tapRect.x+pointer.tapRect.width>x && pointer.tapRect.y<y && pointer.tapRect.y+pointer.tapRect.height>y) {
|
137 |
console.log("Pointer touched!");
|
138 |
isOnPointer = true;
|
139 |
var inpPosition = convertFromCanvasPositionToInputPosition({x:x, y:y}); |
140 |
distanceRatio = Math.sqrt(inpPosition.x*inpPosition.x + inpPosition.y*inpPosition.y)/Math.sqrt(pointer.position.x*pointer.position.x + pointer.position.y*pointer.position.y); |
141 |
}else{
|
142 |
console.log("UI canvas touched!");
|
143 |
isOnPointer = false;
|
144 |
}; |
145 |
previousPosition = {x:x, y:y}; |
146 |
isDragging = true;
|
147 |
} |
148 |
|
149 |
function touchMove(x, y){ |
150 |
if (!isDragging) {return}; |
151 |
var dx = (x - previousPosition.x)/cvInpRatio/distanceRatio;
|
152 |
var dy = (y - previousPosition.y)/cvInpRatio/distanceRatio;
|
153 |
if (isOnPointer) {
|
154 |
pointer.position.x += dx; |
155 |
pointer.position.y -= dy; |
156 |
pointer.angle = Math.atan2(pointer.position.y, pointer.position.x); |
157 |
}else{
|
158 |
inputAreaPosition.x -= dx; |
159 |
inputAreaPosition.y += dy; |
160 |
var leftLimit = -inputWorldSize.width/2 + inputAreaSize.width/2; |
161 |
var rightLimit = inputWorldSize.width/2 - inputAreaSize.width/2; |
162 |
var topLimit = inputWorldSize.height/2 - inputAreaSize.height/2; |
163 |
var bottomLimit = -inputWorldSize.height/2 + inputAreaSize.height/2; |
164 |
if (inputAreaPosition.x < leftLimit) {
|
165 |
inputAreaPosition.x = leftLimit; |
166 |
}; |
167 |
if (inputAreaPosition.x > rightLimit) {
|
168 |
inputAreaPosition.x = rightLimit; |
169 |
}; |
170 |
if (inputAreaPosition.y > topLimit) {
|
171 |
inputAreaPosition.y = topLimit; |
172 |
}; |
173 |
if (inputAreaPosition.y < bottomLimit) {
|
174 |
inputAreaPosition.y = bottomLimit; |
175 |
}; |
176 |
if (x<10 || x>canvas.width-10 || y<10 || y>canvas.height-10) { |
177 |
touchEnd(x, y); |
178 |
}; |
179 |
}; |
180 |
previousPosition = {x:x, y:y}; |
181 |
drawCanvas(); |
182 |
} |
183 |
|
184 |
function touchEnd(canvasX, canvasY){ |
185 |
if (isOnPointer) {
|
186 |
var setteldAngle = settledAngle360/360*Math.PI*2; |
187 |
var settledPosition = {x:settledDistance*Math.cos(setteldAngle), y:settledDistance*Math.sin(setteldAngle)}; |
188 |
pointer.angle = setteldAngle; |
189 |
pointer.position = {x:settledPosition.x, y:settledPosition.y} |
190 |
}; |
191 |
isOnPointer = false;
|
192 |
isDragging = false;
|
193 |
drawCanvas(); |
194 |
} |
195 |
|
196 |
function convertFromCanvasPositionToInputPosition(cvsPpsition){ |
197 |
var cvsDistanceFromCenter = {dx:cvsPpsition.x-canvas.width/2, dy:canvas.height/2 - cvsPpsition.y}; |
198 |
var inpPosition = {x:cvsDistanceFromCenter.dx/cvInpRatio+inputAreaPosition.x, y:cvsDistanceFromCenter.dy/cvInpRatio+inputAreaPosition.y}; |
199 |
return inpPosition;
|
200 |
} |
201 |
|
202 |
function getPointerPosition(){ |
203 |
return {x:pointer.position.x, y:pointer.position.y}; |
204 |
} |
205 |
|
206 |
$(document).on("click","#modal_coding_vector_ok", function(event) { |
207 |
didSelectVector(getPointerPosition()); |
208 |
modalManager.closeDownModal($("#modal_coding_vector")); |
209 |
}); |
210 |
|
211 |
$(document).on("click","#modal_coding_vector_cancel", function(event) { |
212 |
didSelectVector(null);
|
213 |
modalManager.closeDownModal($("#modal_coding_vector")); |
214 |
}); |
215 |
|
216 |
return {
|
217 |
openVectorInput:openVectorInput,
|
218 |
getPointerPosition:getPointerPosition,
|
219 |
}; |
220 |
}); |