Web笔记··By/蜜汁炒酸奶

Canvas初实现拍照小游戏

由于实现的比较简单,且在部分机型上会出现点小问题,此处仅作为js代码的记录,暂不打算写相关教程。 代码会在项目结束后公布。

var ctx = null;// global variable 2d context
var frame = 0;
var imageReady = false;
var myImage = null;	//girl
var myImages =null; //背景
var myImage2 =null; //海鸥
var myImage3 =null; //倒计时
var myImage4 =null; //倒计时girl
var frames= 0;
var started = false;
var suijisum=Math.round(Math.random()*10);//随机数-定位情况
var suijisum2= 0;//随机数--定位海鸥
var suijisum3= 0;//随机数--frame

var sEnd=0;//一种情况是否完成,若完成开始下一种情况。0 没有 1 完成

var frame = 0; //图片

var score=0;//成绩
var isGod=0;//情况判断
//清空canvas画布
function clearCanvas(){
    if(typeof ctx != "undefined"){
        ctx.clearRect(0, 0, window.innerWidth, window.innerHeight);
    }
}

//图片集合
var pic = [

         {
             id: "phone-0",
             src: "img/P6/phone-0.png?S"
         },
         {
             id: "phone-1",
             src: "img/P6/phone-1.png?S"
         },
 		 {
             id: "phone-2",
             src: "img/P6/phone-2.png?S"
         },
 		{
             id: "phone-3",
             src: "img/P6/phone-3.png?S"
         },
	     {
	    	 id: "phone-4",
	    	 src: "img/P6/phone-4.png?s"
	     },
         {
        	 id: "phone-5",
        	 src: "img/P6/phone-5.png?s"
         }
     ];
/*
 * 创建对象
 * 通过对象的getSrc获取图片地址。
 */

var Re={
		getSrc:function(name){
			var i =0;

			while(1){
			if(pic[i].id==name){
					return pic[i].src;
				}

				i++;

			}

		}
	};

/*
 * touchstart执行函数,截取拍照页面
 * */
function eventDown(){

	imageReady =false;
	//实现小图
//	clearCanvas();
//	ctx.translate(window.innerWidth/6,window.innerHeight/7);
//	ctx.scale(0.6, 0.6);
//	ctx.rotate(10*Math.PI/180);

	//加载背景
	preImage(myImages.src,function(){
		ctx.drawImage(myImages,0,0, window.innerWidth,window.innerHeight);
	});
	//加载海鸥图片
	if(suijisum2<11&&suijisum2>9){
		preImage(myImage2.src,function(){
			ctx.drawImage(myImage2,window.innerWidth/2+30,window.innerHeight/7,myImage2.width/2,myImage2.height/2);
		});
		$("#haioutext").show();
		$("#zouguang").hide();
		$("#wanmei").hide();
		$("#kanlian").hide();
		$("#shuaidao").hide();
		$("#zhunbei").hide();
		score = score*2;
	}else{
		$("#haioutext").hide();
		$("#zouguang").hide();
		$("#wanmei").hide();
		$("#kanlian").hide();
		$("#shuaidao").hide();
		$("#zhunbei").hide();

	}
	//加载gril图片
	myImage.src =Re.getSrc("phone-"+frame);
 	preImage(myImage.src,function(){
 		if(window.innerHeight<500){
   			if(frame==0){
	   			ctx.drawImage(myImage,window.innerWidth/3+26,window.innerHeight/6,myImage.width/3,myImage.height/3);
					if(9>=suijisum2){
						$("#zhunbei").show();
						$("#wanmei").hide();
						$("#kanlian").hide();
						$("#shuaidao").hide();
						$("#zouguang").hide();
					}
	   		}else if(frame==1){
	   			ctx.drawImage(myImage,window.innerWidth/5+26,window.innerHeight/6,myImage.width/3,myImage.height/3);
				if(9>=suijisum2){
	   				$("#wanmei").show();
		   			$("#kanlian").hide();
		   			$("#shuaidao").hide();
		   			$("#zouguang").hide();
		   			$("#zhunbei").hide();
	   			}
	   		}else if(frame==2){
	   			ctx.drawImage(myImage,window.innerWidth/5+26,window.innerHeight/6,myImage.width/3,myImage.height/3);
				if(9>=suijisum2){
	   				$("#kanlian").show();
		   			$("#wanmei").hide();
		   			$("#shuaidao").hide();
		   			$("#zouguang").hide();
		   			$("#zhunbei").hide();
	   			}
	   		}else if(frame==3){
	   			ctx.drawImage(myImage,window.innerWidth/5+26,window.innerHeight/3,myImage.width/3,myImage.height/3);
				if(9>=suijisum2){
	   				$("#shuaidao").show();
		   			$("#kanlian").hide();
		   			$("#wanmei").hide();
		   			$("#zouguang").hide();
		   			$("#zhunbei").hide();
	   			}
	   		}else if(frame==4){
	   			ctx.drawImage(myImage,window.innerWidth/5+26,window.innerHeight/6,myImage.width/3,myImage.height/3);
				if(9>=suijisum2){
	   				$("#zouguang").show();
		   			$("#kanlian").hide();
		   			$("#shuaidao").hide();
		   			$("#wanmei").hide();
		   			$("#zhunbei").hide();
	   			}
	   		}

   		}else{
   			if(frame==0){
   	   			ctx.drawImage(myImage,window.innerWidth/3+26,window.innerHeight/7,myImage.width/2,myImage.height/2);
					if(9>=suijisum2){
						$("#zhunbei").show();
						$("#wanmei").hide();
						$("#kanlian").hide();
						$("#shuaidao").hide();
						$("#zouguang").hide();
					}
   	   		}else if(frame==1){
   	   			ctx.drawImage(myImage,window.innerWidth/5+26,window.innerHeight/7,myImage.width/2,myImage.height/2);
				if(9>=suijisum2){
	   				$("#wanmei").show();
		   			$("#kanlian").hide();
		   			$("#shuaidao").hide();
		   			$("#zouguang").hide();
		   			$("#zhunbei").hide();
	   			}
   	   		}else if(frame==2){
   	   			ctx.drawImage(myImage,window.innerWidth/5+26,window.innerHeight/7,myImage.width/2,myImage.height/2);
				if(9>=suijisum2){
	   				$("#kanlian").show();
		   			$("#wanmei").hide();
		   			$("#shuaidao").hide();
		   			$("#zouguang").hide();
		   			$("#zhunbei").hide();
	   			}
   	   		}else if(frame==3){
   	   			ctx.drawImage(myImage,window.innerWidth/5+26,window.innerHeight/3,myImage.width/2,myImage.height/2);
				if(9>=suijisum2){
	   				$("#shuaidao").show();
		   			$("#kanlian").hide();
		   			$("#wanmei").hide();
		   			$("#zouguang").hide();
		   			$("#zhunbei").hide();
	   			}
   	   		}else if(frame==4){
   	   			ctx.drawImage(myImage,window.innerWidth/5+26,window.innerHeight/7,myImage.width/2,myImage.height/2);
				if(9>=suijisum2){
	   				$("#zouguang").show();
		   			$("#kanlian").hide();
		   			$("#shuaidao").hide();
		   			$("#wanmei").hide();
		   			$("#zhunbei").hide();
	   			}
   	   		}
   		}

		//ctx.save();
		//ctx.restore();
	//	ctx.rect(10,10,100,120);
	//	ctx.clip();
	//	ctx.save();

	//	ctx.scale(0.5,0.5);
	//	ctx.rotate(20*Math.PI/180);
	//	clearCanvas();
	//	 ctx.drawImage(myImage,150,150,1000,1009,0,0,100,100);

    });
 		//添加白框,绘制照片效果
 	/**/setTimeout(function(){
 		//添加阴影
 		ctx.shadowColor="black";
		ctx.shadowOffsetX = 0;
		ctx.shadowOffsetY = 0;
		ctx.shadowBlur = 2;
 		//var newImgData=ctx.createImageData(canvas.width,canvas.height);
		var imgData=ctx.getImageData(window.innerWidth/6,window.innerHeight/7,window.innerWidth-63,window.innerHeight-202);//复制图层
		clearCanvas();//清除画布
		//绘制白框
		ctx.strokeStyle="#ffffff";
		ctx.lineWidth="10";
		ctx.rect(35,45,window.innerWidth-53,window.innerHeight-192);
		ctx.stroke();
		ctx.clip();


		ctx.putImageData(imgData,40,50);//拷贝imData图层

 	},200);



 	imageReady =false;
 	//更新得分
	if($("#zuigaofen").html()<score){
		$("#zuigaofen").html(score);
		$("#me_paizuigao").html(score);
		updateZqlMember_points_game();

	}
	getResultGame();//执行特殊分享
	//显示游戏结束的得分界面
	$("#scoresums").html(score);
	$("#photo").hide();
	$("#photo_paihang").hide();
	$("#zuigaofen").hide();
	imageReady =false;
	if(score<10){
		$("#scoresums").css("left","12%");
	}else if(score<99){
		$("#scoresums").css("left","7%");
	}else{
		$("#scoresums").css("left","3%");
	}
	$("#zhongqing_o4").show();
	imageReady =false;


}
/*
 * touchend执行函数
 * */
function eventUp(){
	     canvas.removeEventListener('touchstart', eventDown);
     canvas.removeEventListener('touchend', eventUp);


}
/*
 * 图片循环出现,并根据不同情况计算分数
 * */
function update() {


	    if(imageReady){
	    	if(frames==0){
	    		frames=1;
	    	}else if(frames==1){
	    		frames=0;
	    	}
	    	/*
	    	 * 0-1-0 正常
	    	 * 0-2-0 挡脸
	    	 * 0-1-3 摔倒
	    	 * 0-4-0 禁
	    	 * */
	    	if(sEnd==1){
	    		suijisum =Math.round(Math.random()*10);

	    	}
	    	suijisum2 =Math.round(Math.random()*10);
	    	if(2>suijisum){
	    		//站立
	    		 clearCanvas();
	    		frame=0;
	    		score =0;
	    		drawPicture();

	    	}else if((4>suijisum&&suijisum>=2)){
	    		//1正常
	    		 clearCanvas();

    			frame=1;
    			score =50;
	    		drawPicture();

	    	}else if(6>suijisum&&suijisum>=4){
	    		//2遮脸
	    		clearCanvas();
    			frame=2;
    			score =40;
	    		drawPicture();
	    	}else if(8>suijisum&&suijisum>=6){
	    		//3 摔倒
	    		clearCanvas();
    			frame=3;
    			score =20;
	    		drawPicture();
	    	}else if(10>=suijisum&&suijisum>=8){
	    		//4禁
	    		clearCanvas();
    			frame=4;
    			score =30;
	    		drawPicture();
	    	}


	    }


	}
/* 绘制girl图片
 *frame为图片名称中的数字
 *sEnd 判定一种情况是否结束以便执行下一种,0未结束,1结束
 *
 * */

function drawPicture(){
    if(typeof ctx != "undefined"){
    	myImage = document.createElement('img');
    	myImages = document.createElement('img');
    	myImage2 = document.createElement('img');
    	myImages.src ="img/P5/bg.png?S";
    	myImage2.src="img/P6/phone-5.png";

    	myImage.src =Re.getSrc("phone-"+frames);
    	if(imageReady){
    		/*
    		 * 图片展示
    		 * */
    	   	preImage(myImage.src,function(){
    	   		if(suijisum2<11&&suijisum2>9){
    	    		ctx.drawImage(myImage2,window.innerWidth/2+26,window.innerHeight/7,myImage2.width/2,myImage2.height/2);
    	    	}
    	   		if(window.innerHeight<500){
    	   			if(frames==0){
        	   			ctx.drawImage(myImage,window.innerWidth/3+20,window.innerHeight/6,myImage.width/3,myImage.height/3);
        	   		}else if(frames==1){
        	   			ctx.drawImage(myImage,window.innerWidth/5+20,window.innerHeight/6,myImage.width/3,myImage.height/3);
        	   		}

    	   		}else{
    	   			if(frames==0){
    	   	   			ctx.drawImage(myImage,window.innerWidth/3+20,window.innerHeight/7,myImage.width/2,myImage.height/2);
    	   	   		}else if(frames==1){
    	   	   			ctx.drawImage(myImage,window.innerWidth/5+20,window.innerHeight/7,myImage.width/2,myImage.height/2);
    	   	   		}
    	   		}


    	    });
    	   	/*
	    	 * 0-1-0 正常
	    	 * 0-2-0 挡脸
	    	 * 0-1-3 摔倒
	    	 * 0-4-0 禁
	    	 * */
    	   	if(2>suijisum){
	    		//站立
	    		frame=0;
	    		score =0;

	    	}else if((4>suijisum&&suijisum>=2)){
	    		//1正常

    			frame=1;
    			score =50;

	    	}else if(6>suijisum&&suijisum>=4){
	    		//2遮脸
    			frame=2;
    			score =40;
	    	}else if(8>suijisum&&suijisum>=6){
	    		//3 摔倒
    			frame=3;
    			score =20;
	    	}else if(10>=suijisum&&suijisum>=8){
	    		//4禁
    			frame=4;
    			score =30;
	    	}

    	}


    	/*循环
    	 * sEnd=0 情况未变
    	 * sEnd=1 情况改变
    	 * */
    	setTimeout(function(){
			setTimeout( update, 1000/30);
     },500);
   /* 	if(sEnd==1){

    		setTimeout(function(){
    				frame=0;
        			setTimeout( update, 1000/30);
             },700);
    	}else{
    		);
    	}*/

    	 //点击事件-拍照
    	 canvas.addEventListener('touchstart', eventDown);
         canvas.addEventListener('touchend', eventUp);

    }

}
/*倒计时,并开始游戏
 * isnum2计数 3 2 1
 *
 * */
var isnum2 = 3;
function playgo(){

	myImage3 = document.createElement('img');
	myImage4 = document.createElement('img');

	myImage4.src="img/P6/phone-0.png";


		if(isnum2==3){
			clearCanvas();
			myImage3.src="img/P4/"+isnum2+".png";
			preImage(myImage4.src,function(){
					 if(window.innerHeight<500){
							ctx.drawImage(myImage4,window.innerWidth/3+20,window.innerHeight/6,myImage4.width/3,myImage4.height/3);
						}else{
							ctx.drawImage(myImage4,window.innerWidth/3+20,window.innerHeight/7,myImage4.width/2,myImage4.height/2);
						}
			 });
			preImage(myImage3.src,function(){
			 ctx.drawImage(myImage3, window.innerWidth/5+30,window.innerHeight/4);
				 });
			 isnum2 =2;
			 setTimeout(function(){

				 playgo();

			 },600);
		}else if(isnum2==2){
			clearCanvas();
			myImage3.src="img/P4/"+isnum2+".png";
			preImage(myImage4.src,function(){
					 if(window.innerHeight<500){
							ctx.drawImage(myImage4,window.innerWidth/3+20,window.innerHeight/6,myImage4.width/3,myImage4.height/3);
						}else{
							ctx.drawImage(myImage4,window.innerWidth/3+20,window.innerHeight/7,myImage4.width/2,myImage4.height/2);
						}
			 });
			preImage(myImage3.src,function(){
				ctx.drawImage(myImage3, window.innerWidth/5+30,window.innerHeight/4);
			 });
			isnum2 =1;
			 setTimeout(function(){

				 playgo();

			 },600);
		}else if(isnum2==1){
			clearCanvas();
			myImage3.src="img/P4/"+isnum2+".png";
			 preImage(myImage4.src,function(){
					 if(window.innerHeight<500){
							ctx.drawImage(myImage4,window.innerWidth/3+20,window.innerHeight/6,myImage4.width/3,myImage4.height/3);
						}else{
							ctx.drawImage(myImage4,window.innerWidth/3+20,window.innerHeight/7,myImage4.width/2,myImage4.height/2);
						}
			 });
			preImage(myImage3.src,function(){
				ctx.drawImage(myImage3, window.innerWidth/5+30,window.innerHeight/4);
			});
			isnum2 =0;
			 setTimeout(function(){
				//开始游戏


				 setTimeout(function(){

					 clearCanvas();
					 frames=1;
					 drawPicture();
				 },300);
			 },700);
		}

}
function star_game() {
	imageReady = true;
    score =0;
    frame = 0;
    frames = 0;
    isnum2 = 3;
   suijisum=Math.round(Math.random()*10);
   suijisum3 = Math.round(Math.random()*10);
    suijisum2= Math.round(Math.random()*10);
	canvas = document.getElementById("canvas");

	 canvas.width = window.innerWidth;// window.innerWidth;
     canvas.height = window.innerHeight;//window.innerHeight;
     $("#canvas").css("width",window.innerWidth);
     $("#canvas").css("height",window.innerHeight);
	 $("#canvas").css("left","0%");
	 $("#canvas").css("top","0%");

     ctx =canvas.getContext("2d");
     //取消拍照点击事件
     canvas.removeEventListener('touchstart', eventDown);
     canvas.removeEventListener('touchend', eventUp);
     //倒计时
     clearCanvas();
     playgo();



}
//图片预加载
function preImage(url,callback){
    var img = new Image(); //创建一个Image对象,实现图片的预下载
    img.src = url;



    	 img.onload = function () { //图片下载完毕时异步调用callback函数。

    	        callback.call(img);//将回调函数的this替换为Image对象
    	     //   alert(img.src);
    	    };


}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569

预览
Loading comments...
3 条评论
  • W

    canvas太复杂,一堆的坐标

  • W

    点击不可见啊

example
预览