pictcode / app / webroot / js / pictcode / preview_manager.js @ 5ec4ad9d
履歴 | 表示 | アノテート | ダウンロード (10.359 KB)
1 |
define(['block_data', 'image_manager', 'sprite_drawer', 'block_manager'], function(blockData, imageManager, spriteDrawer, blockManager){ |
---|---|
2 |
|
3 |
var canvas;
|
4 |
var canvasContext;
|
5 |
var dt = 20;//ミリ秒 |
6 |
var timer;
|
7 |
var dummy = {};
|
8 |
var initialDummy = {size:125, position:{x:0, y:0}, scale:1.0, angle:0, opacity:1.0, mirrored:false}; |
9 |
|
10 |
var aspectRatio;
|
11 |
|
12 |
var ACTION_TYPE = {
|
13 |
ROTATE_RIGHT : 0, |
14 |
ROTATE_LEFT : 1, |
15 |
ZOOM : 2, |
16 |
OPACITY : 3, |
17 |
}; |
18 |
|
19 |
function initialize(cvs, cvsCtx){ |
20 |
initialDummy.image = imageManager.images.preview_001; |
21 |
canvas = cvs; |
22 |
canvasContext = cvsCtx; |
23 |
canvasContext.fillStyle = '#4682b4';
|
24 |
aspectRatio = spriteDrawer.editorInnerSize.height/spriteDrawer.editorInnerSize.width; |
25 |
initializeDummy(); |
26 |
initializePreview(); |
27 |
} |
28 |
|
29 |
function startPreview(targetBlock){ |
30 |
stopPreview(); |
31 |
initializeDummy(); |
32 |
|
33 |
switch(targetBlock.name){
|
34 |
case "move": |
35 |
move(targetBlock); |
36 |
break;
|
37 |
case "rotate_right": |
38 |
action(targetBlock, ACTION_TYPE.ROTATE_RIGHT); |
39 |
break;
|
40 |
case "rotate_left": |
41 |
action(targetBlock, ACTION_TYPE.ROTATE_LEFT); |
42 |
break;
|
43 |
case "zoom": |
44 |
action(targetBlock, ACTION_TYPE.ZOOM); |
45 |
break;
|
46 |
case "opacity": |
47 |
action(targetBlock, ACTION_TYPE.OPACITY); |
48 |
break;
|
49 |
case "self_duplicate": |
50 |
selfDuplicate(targetBlock); |
51 |
break;
|
52 |
case "self_kill": |
53 |
selfKill(targetBlock); |
54 |
break;
|
55 |
case "mirror": |
56 |
mirror(targetBlock); |
57 |
break;
|
58 |
default:
|
59 |
initializePreview(); |
60 |
break;
|
61 |
} |
62 |
} |
63 |
|
64 |
function stopPreview(){ |
65 |
if (timer) {
|
66 |
clearInterval(timer); |
67 |
}; |
68 |
} |
69 |
|
70 |
function initializePreview(){ |
71 |
var cvDummySize = canvas.height*0.25; |
72 |
var cvGbRatio = cvDummySize / dummy.size;
|
73 |
drawDummy(cvDummySize, cvGbRatio); |
74 |
} |
75 |
|
76 |
function initializeDummy(){ |
77 |
dummy.image = initialDummy.image; |
78 |
dummy.size = initialDummy.size; |
79 |
dummy.position = {x:initialDummy.position.x, y:initialDummy.position.y}; |
80 |
dummy.scale = initialDummy.scale; |
81 |
dummy.angle = initialDummy.angle; |
82 |
dummy.opacity = initialDummy.opacity; |
83 |
dummy.mirrored = initialDummy.mirrored; |
84 |
} |
85 |
|
86 |
function drawDummy(cvDummySize, cvGbRatio, isTwined){ |
87 |
var cvDummyLeft = canvas.width/2 - cvDummySize/2 + dummy.position.x*cvGbRatio; |
88 |
var cvDummyTop = canvas.height/2 - cvDummySize/2 - dummy.position.y*cvGbRatio; |
89 |
|
90 |
canvasContext.clearRect(0, 0, canvas.width, canvas.height); |
91 |
canvasContext.fillRect(0, 0, canvas.width, canvas.height); |
92 |
var cvBgSize = {width:spriteDrawer.editorInnerSize.width*cvGbRatio*1.1, height:spriteDrawer.editorInnerSize.height*cvGbRatio*1.1}; |
93 |
canvasContext.drawImage(imageManager.images.preview_002, canvas.width/2-cvBgSize.width/2, canvas.height/2-cvBgSize.height/2, cvBgSize.width, cvBgSize.height); |
94 |
canvasContext.save(); |
95 |
canvasContext.lineWidth = 10;
|
96 |
canvasContext.strokeStyle = "#8b4513";
|
97 |
canvasContext.strokeRect(0, 0, canvas.width, canvas.height); |
98 |
canvasContext.translate(canvas.width/2, canvas.height/2); |
99 |
canvasContext.rotate(dummy.angle); |
100 |
if (dummy.mirrored) {
|
101 |
canvasContext.transform(-1, 0, 0, 1, 0, 0); |
102 |
}; |
103 |
canvasContext.translate(-canvas.width/2, -canvas.height/2); |
104 |
canvasContext.globalAlpha = dummy.opacity; |
105 |
canvasContext.drawImage(dummy.image, cvDummyLeft, cvDummyTop, cvDummySize, cvDummySize); |
106 |
if (isTwined) {
|
107 |
var twinedLeft = canvas.width/2 - cvDummySize/2 - dummy.position.x*cvGbRatio; |
108 |
canvasContext.drawImage(dummy.image, twinedLeft, cvDummyTop, cvDummySize, cvDummySize); |
109 |
}; |
110 |
canvasContext.restore(); |
111 |
} |
112 |
|
113 |
function move(block){ |
114 |
var duration;
|
115 |
if (!block.value1.isVariable) {
|
116 |
duration = block.value1.value * 1000;//ミリ秒 |
117 |
}else{
|
118 |
duration = blockManager.findVariableWithId(block.value1.value).value2.value * 1000;//ミリ秒 |
119 |
}; |
120 |
var passedTime = 0; |
121 |
var isInterval = false; |
122 |
var intervalTime = 0; |
123 |
|
124 |
dummy.position = {x:-block.value2.value.x/2, y:-block.value2.value.y/2}; |
125 |
var gbDummySize = dummy.size;
|
126 |
var gbDistance;
|
127 |
var cvsDistance;
|
128 |
if (Math.abs(block.value2.value.x) > Math.abs(block.value2.value.y)) {
|
129 |
gbDistance = Math.abs(block.value2.value.x); |
130 |
cvsDistance = canvas.width; |
131 |
}else{
|
132 |
gbDistance = Math.abs(block.value2.value.y); |
133 |
cvsDistance = canvas.height; |
134 |
}; |
135 |
var cvDummySize = cvsDistance * gbDummySize / (gbDummySize+gbDistance);
|
136 |
var cvDistance = cvsDistance * gbDistance / (gbDummySize+gbDistance);
|
137 |
var cvGbRatio = cvDummySize / gbDummySize;
|
138 |
|
139 |
var gbd;
|
140 |
if (block.value1.value > 0) { |
141 |
gbd = {dx:block.value2.value.x*dt/duration, dy:block.value2.value.y*dt/duration}; |
142 |
}else{
|
143 |
gbd = {dx:block.value2.value.x, dy:block.value2.value.y}; |
144 |
}; |
145 |
|
146 |
timer = setInterval(function(){
|
147 |
if (isInterval) {
|
148 |
intervalTime += dt; |
149 |
if (intervalTime == 500) { |
150 |
dummy.position.x = -block.value2.value.x/2;
|
151 |
dummy.position.y = -block.value2.value.y/2;
|
152 |
drawDummy(cvDummySize, cvGbRatio, false);
|
153 |
}else if (intervalTime >= 1200) { |
154 |
isInterval = false;
|
155 |
passedTime = 0;
|
156 |
}; |
157 |
return;
|
158 |
}; |
159 |
|
160 |
dummy.position.x += gbd.dx; |
161 |
dummy.position.y += gbd.dy; |
162 |
passedTime += dt; |
163 |
if(passedTime >= duration){
|
164 |
isInterval = true;
|
165 |
intervalTime = 0;
|
166 |
} |
167 |
|
168 |
drawDummy(cvDummySize, cvGbRatio, false);
|
169 |
} , dt); |
170 |
} |
171 |
|
172 |
function action(block, actinType){ |
173 |
var duration;
|
174 |
if (!block.value1.isVariable) {
|
175 |
duration = block.value1.value * 1000;//ミリ秒 |
176 |
}else{
|
177 |
duration = blockManager.findVariableWithId(block.value1.value).value2.value * 1000;//ミリ秒 |
178 |
}; |
179 |
var degree;
|
180 |
if (!block.value2.isVariable) {
|
181 |
degree = block.value2.value; |
182 |
}else{
|
183 |
degree = blockManager.findVariableWithId(block.value2.value).value2.value; |
184 |
}; |
185 |
var passedTime = 0; |
186 |
var isInterval = false; |
187 |
var intervalTime = 0; |
188 |
|
189 |
var ds;
|
190 |
var cvDummySize;
|
191 |
switch(actinType){
|
192 |
case ACTION_TYPE.ROTATE_RIGHT:
|
193 |
if (block.value1.value > 0) { |
194 |
ds = (degree*Math.PI/180 - dummy.angle) * dt / duration;
|
195 |
}else{
|
196 |
ds = degree*Math.PI/180 - dummy.angle;
|
197 |
}; |
198 |
cvDummySize = canvas.height*0.45;
|
199 |
break;
|
200 |
case ACTION_TYPE.ROTATE_LEFT:
|
201 |
if (block.value1.value > 0) { |
202 |
ds = -(degree*Math.PI/180 - dummy.angle) * dt / duration;
|
203 |
}else{
|
204 |
ds = -(degree*Math.PI/180 - dummy.angle);
|
205 |
}; |
206 |
cvDummySize = canvas.height*0.45;
|
207 |
break;
|
208 |
case ACTION_TYPE.ZOOM:
|
209 |
if (block.value1.value > 0) { |
210 |
ds = (degree - dummy.scale) * dt / duration; |
211 |
}else{
|
212 |
ds = (degree - dummy.scale); |
213 |
}; |
214 |
cvDummySize = canvas.height*0.25;
|
215 |
break;
|
216 |
case ACTION_TYPE.OPACITY:
|
217 |
if (block.value1.value > 0) { |
218 |
ds = (degree - dummy.opacity) * dt / duration; |
219 |
}else{
|
220 |
ds = degree - dummy.opacity; |
221 |
}; |
222 |
cvDummySize = canvas.height*0.45;
|
223 |
break;
|
224 |
} |
225 |
if (!ds || !cvDummySize) {return;}; |
226 |
|
227 |
var cvGbRatio = cvDummySize / dummy.size;
|
228 |
|
229 |
timer = setInterval(function(){
|
230 |
if (isInterval) {
|
231 |
intervalTime += dt; |
232 |
if (intervalTime == 500) { |
233 |
initializeDummy(); |
234 |
drawDummy(cvDummySize*dummy.scale, cvGbRatio, false);
|
235 |
}else if (intervalTime >= 1200) { |
236 |
isInterval = false;
|
237 |
passedTime = 0;
|
238 |
}; |
239 |
return;
|
240 |
}; |
241 |
|
242 |
switch(actinType){
|
243 |
case ACTION_TYPE.ROTATE_RIGHT:
|
244 |
case ACTION_TYPE.ROTATE_LEFT:
|
245 |
dummy.angle += ds; |
246 |
break;
|
247 |
case ACTION_TYPE.ZOOM:
|
248 |
dummy.scale += ds; |
249 |
break;
|
250 |
case ACTION_TYPE.OPACITY:
|
251 |
dummy.opacity += ds; |
252 |
break;
|
253 |
} |
254 |
passedTime += dt; |
255 |
|
256 |
if(passedTime >= duration){
|
257 |
isInterval = true;
|
258 |
intervalTime = 0;
|
259 |
} |
260 |
|
261 |
drawDummy(cvDummySize*dummy.scale, cvGbRatio, false);
|
262 |
} , dt); |
263 |
} |
264 |
|
265 |
function selfDuplicate(block){ |
266 |
|
267 |
var passedTime = 0; |
268 |
var isInterval = false; |
269 |
var intervalTime = 0; |
270 |
var duration = 250; |
271 |
|
272 |
var cvDummySize= canvas.height*0.45; |
273 |
var ds = cvDummySize*0.28 * dt / duration; |
274 |
var cvGbRatio = cvDummySize / dummy.size;
|
275 |
|
276 |
timer = setInterval(function(){
|
277 |
if (isInterval) {
|
278 |
intervalTime += dt; |
279 |
if (intervalTime == 1000) { |
280 |
drawDummy(cvDummySize*dummy.scale, cvGbRatio, false);
|
281 |
}else if (intervalTime >= 1700) { |
282 |
isInterval = false;
|
283 |
passedTime = 0;
|
284 |
}; |
285 |
return;
|
286 |
}; |
287 |
|
288 |
dummy.position.x += ds; |
289 |
passedTime += dt; |
290 |
|
291 |
if(passedTime >= duration){
|
292 |
initializeDummy(); |
293 |
isInterval = true;
|
294 |
intervalTime = 0;
|
295 |
return;
|
296 |
} |
297 |
|
298 |
drawDummy(cvDummySize*dummy.scale, cvGbRatio, true);
|
299 |
} , dt); |
300 |
} |
301 |
|
302 |
function selfKill(block){ |
303 |
|
304 |
var passedTime = 0; |
305 |
var isInterval = false; |
306 |
var intervalTime = 0; |
307 |
var duration = 1700; |
308 |
|
309 |
var cvDummySize= canvas.height*0.45; |
310 |
var cvGbRatio = cvDummySize / dummy.size;
|
311 |
|
312 |
drawDummy(cvDummySize*dummy.scale, cvGbRatio, false);
|
313 |
timer = setInterval(function(){
|
314 |
if (isInterval) {
|
315 |
intervalTime += dt; |
316 |
if (intervalTime >= duration) {
|
317 |
isInterval = false;
|
318 |
passedTime = 0;
|
319 |
initializeDummy(); |
320 |
drawDummy(cvDummySize*dummy.scale, cvGbRatio, false);
|
321 |
}; |
322 |
return;
|
323 |
}; |
324 |
|
325 |
passedTime += dt; |
326 |
|
327 |
if(passedTime >= duration){
|
328 |
isInterval = true;
|
329 |
intervalTime = 0;
|
330 |
dummy.opacity = 0;
|
331 |
drawDummy(cvDummySize*dummy.scale, cvGbRatio, false);
|
332 |
return;
|
333 |
} |
334 |
} , dt); |
335 |
} |
336 |
|
337 |
function mirror(block){ |
338 |
|
339 |
var passedTime = 0; |
340 |
var isInterval = false; |
341 |
var intervalTime = 0; |
342 |
var duration = 1200; |
343 |
|
344 |
var cvDummySize= canvas.height*0.45; |
345 |
var cvGbRatio = cvDummySize / dummy.size;
|
346 |
|
347 |
drawDummy(cvDummySize*dummy.scale, cvGbRatio, false);
|
348 |
timer = setInterval(function(){
|
349 |
if (isInterval) {
|
350 |
intervalTime += dt; |
351 |
if (intervalTime >= duration) {
|
352 |
isInterval = false;
|
353 |
passedTime = 0;
|
354 |
initializeDummy(); |
355 |
drawDummy(cvDummySize*dummy.scale, cvGbRatio, false);
|
356 |
}; |
357 |
return;
|
358 |
}; |
359 |
|
360 |
passedTime += dt; |
361 |
|
362 |
if(passedTime >= duration){
|
363 |
isInterval = true;
|
364 |
intervalTime = 0;
|
365 |
dummy.mirrored = true;
|
366 |
drawDummy(cvDummySize*dummy.scale, cvGbRatio, false);
|
367 |
return;
|
368 |
} |
369 |
} , dt); |
370 |
} |
371 |
|
372 |
return {
|
373 |
initialize:initialize,
|
374 |
startPreview:startPreview,
|
375 |
stopPreview:stopPreview,
|
376 |
}; |
377 |
}); |