Lab For Do Game
ค้นคว้า วิจัย ลองทำไปเรื่อย
ค้นคว้า วิจัย ลองทำไปเรื่อย
Nov 29th
สำหรับการขยับวัตถุในตัวอย่างข้างบน จะใช้วิธีการ วาด canvas ลงไปใน div ด้วย javascript แล้วเก็บ object ของ div ใส่ไว้ใน array
กำหนด interval เพื่อทำ loop event แล้ว fetch array ที่เก็บ div แล้วทำการปรับค่าตำแหน่ง
code ทั้งหมด
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 | // กำหนดพื้นที่สำหรับวาดรูปวงกลม <div id="circleArea" style='width:600px; height: 400px; position:absolute'></div> <script type="text/javascript"> // array สำหรับเก็บ div ของวงกลมทั้งหมด var arDiv = new Array(); for(var i=0; i<50; i++){ var cvObj = document.createElement('canvas'); // สร้าง element arDiv.push(cvObj); // จัดเก็บ cvObj.style.position = "absolute"; // random position cvObj.style.left = (Math.random()*300).toString()+"px"; cvObj.style.top = (Math.random()*300).toString()+"px"; cvObj.speed = parseInt(Math.random()*5); // random ค่าสี var color = "#"; for(var j=0; j<3; j++){ var preColor = (Math.floor(Math.random()*255)).toString(16); if(preColor.length == 1){ preColor = "0"+preColor; }else if(preColor.length == 0){ preColor = "00"; } color += preColor; } // เรียก context เพื่อจะทำการวาด circle var ctx = cvObj.getContext('2d'); ctx.fillStyle = color; ctx.beginPath(); ctx.arc(10,10,10,0,Math.PI*2,true); ctx.closePath(); ctx.fill(); var a = document.getElementById("circleArea"); if(a != null){ // เติมลงใน circle div a.appendChild(cvObj); }else{ alert('area div is null'); } // update function updateObj(){ for(var i=0; i< arDiv.length; i++){ var obj = arDiv[i]; obj.style.left = (parseInt(obj.style.left)+obj.speed).toString()+("px"); if(parseInt(obj.style.left) > 400){ obj.style.left = 0; } } } var tInterval = setInterval(updateObj, 100); } </script> |
Nov 24th
สำหรับการทำ ดังตัวอย่าง จะใช้วิธีการ วน loop สร้าง circle ใน 1 canvas ซึ่ง code ที่ใช้จะเป็น
1 2 3 4 5 6 7 8 9 10 11 | <canvas id="areadraw" style="width:400px; height:400px; background:#000"></canvas> <script type="text/javascript"> var ctx = $('#areadraw')[0].getContext("2d"); for(var i = 0; i<100; i++){ ctx.fillStyle = "rgba("+Math.floor(255*Math.random()).toString()+", "+Math.floor( 255*Math.random()).toString()+", "+Math.floor(255*Math.random()).toString()+", "+"1)"; ctx.beginPath(); ctx.arc(Math.random()*400, Math.random() *400, Math.random() *10, 0, Math.PI*2, true); ctx.closePath(); ctx.fill(); } </script> |
Nov 24th
สามารถประกาศดักจับ event ตั้งแต่ตอนแรกโดย ใช้
1 2 3 4 | $("#area_test").mousemove(function (event){ event.pageX; event.pageY; } |
sourcecode ทั้งหมด
1 2 3 4 5 6 7 8 9 | <div id='area_test' style='width: 600px; height: 400px;'>
<div id='txtValue'></div>
</div>
<script type="text/javascript">
$("#area_test").mousemove(function (event){
var txt = "x:"+ event.pageX+", y:"+ event.pageY;
jQuery("#txtValue").html(txt);
});
</script> |
Nov 22nd
สำหรับ การใส่รูป Image ลงใน canvas จะใช้ function
1 2 3 4 5 6 | //normal context.drawImage(imageObj, 0, 0); // วาดที่ 0,0 // resize context.drawImage(imageObj, 0, 0, 100, 100); // วาดที่ 0,0 กว้างยาว 100x100 // crop context.drawImage(imageObj, sourceX, sourceY, sourceWidth, sourceHeight, destX, destY, destWidth, destHeight); |
จากตัวอย่างข้างบนจะใช้แบบที่ 2 คือ resize
ตัวอย่าง sourcecode
1 2 3 4 5 6 7 8 9 10 | <canvas id='objImage1' width=200 height=200></canvas> <script type="text/javascript"> var ctx = $('#objImage1')[0].getContext("2d"); var imageObj = new Image(); imageObj.onload = function(){ ctx.drawImage(imageObj, 0, 0, 100, 100); }; //imageObj.src = "darth-vader.jpg"; imageObj.src = "http://a2.twimg.com/profile_images/1381611495/image.jpg"; </script> |
Nov 22nd
สำหรับการวาด รูปสี่เหลี่ยมใน canvas ของ html5 จะใช้
1 2 3 | var ctx = $('#objRect')[0].getContext("2d"); // เลือก #objRect เป็น canvas สำหรับใช้วาด ctx.fillStyle = "#ff0000"; // เลือกสี #ff0000 ctx.fillRect(10,10,100,75); // วาดที่ x=10, y=10, width=100, height=75 |
ถ้าเป็น gradient ไล่สีจะใช้
1 2 3 4 5 6 | var ctx = $('#objRect')[0].getContext("2d"); var grd=ctx.createLinearGradient(0,50,100,50); // line gradient grd.addColorStop(0,"#FF0000"); //สีเริ่มต้นแรก grd.addColorStop(1,"#000000"); //สีต่อมา ctx.fillStyle = grd; ctx.fillRect(10, 110, 100, 20); |
ดู source code ทั้งหมด
1 2 3 4 5 6 7 8 9 10 11 12 13 | <canvas id='objRect' width=200 height=200></canvas> <script type="text/javascript"> var ctx = $('#objRect')[0].getContext("2d"); //normal ctx.fillStyle = "#ff0000"; ctx.fillRect(10,10,100,75); // gradient color var grd=ctx.createLinearGradient(0,50,100,50); grd.addColorStop(0,"#FF0000"); grd.addColorStop(1,"#000000"); ctx.fillStyle = grd; ctx.fillRect(10, 110, 100, 20); </script> |
Nov 18th
สำหรับ การวาดวงกลม ใน html เราจะใช้ tag:canvas
1 | <canvas id='objLine' width=200 height=200></canvas> |
จากนั้นจะใช้ jquery จับ id ที่ชื่อ objLine
1 | var ctx = $('#objLine')[0].getContext("2d"); |
แล้วทำการวาด เส้น ด้วย
1 2 3 4 | ctx.moveTo(10, 10); ctx.lineTo(100, 100); ctx.lineTo(120, 50); ctx.stroke(); |
ดู source code ทั้งหมด
1 2 3 4 5 6 7 8 | <canvas id='objLine' width=200 height=200></canvas> <script type="text/javascript"> var ctx = jQuery('#objLine')[0].getContext("2d"); ctx.moveTo(10, 10); ctx.lineTo(100, 100); ctx.lineTo(120, 120); ctx.stroke(); </script> |
Nov 18th
สำหรับ การวาดวงกลม ใน html เราจะใช้ tag:canvas
1 | <canvas id='objCircle' width=200 height=200></canvas> |
จากนั้นจะใช้ jquery จับ id ที่ชื่อ objCircle
1 | var ctx = $('#objCircle')[0].getContext("2d"); |
แล้วทำการวาดด้วย
1 2 3 4 5 6 | ctx.fillStyle="#FF0000"; ctx.beginPath(); //context.arc(centerX, centerY, radius, startingAngle, endingAngle, antiClockwise); ctx.arc(100, 100, 50, 0, Math.PI*2, true); ctx.closePath(); ctx.fill(); |
ดู source code ทั้งหมด
1 2 3 4 5 6 7 8 9 | <canvas id='objCircle' width=200 height=200></canvas> <script type="text/javascript"> var ctx = $('#objCircle')[0].getContext("2d"); ctx.fillStyle="#FF0000"; ctx.beginPath(); ctx.arc(100, 100, 50, 0, Math.PI*2, true); ctx.closePath(); ctx.fill(); </script> |
Nov 16th
[ทดลอง โดย กด key up/down/left/right]
สำหรับการดักจับ keyboard ใน jQuery เราจะใช้ function keydown, keypress
ตัวอย่าง
1 2 3 | $(document).keydown( function(e){ alert('key down'); }); |
จากตัวอย่างข้างบน จะได้ source code ทั้งหมด
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 | <div id='objKey' style="background-color:#bca; position:absolute; width:50px; height:50px;">[x]</div> <script type='text/javascript'> $(document).keydown( function(e){ var x = 0; var y = 0; var left = $('#objKey').css('left'); var top = $('#objKey').css('top'); if(left != 'auto'){ x = parseInt(left); } if(top != 'auto'){ y = parseInt(top); } //key event if(e.keyCode== 40){ //alert('down'); y += 10; } if(e.keyCode== 38){ //alert('up'); y -= 10; } if(e.keyCode== 37){ //alert('left'); x -= 10; } if(e.keyCode== 39){ //alert('right'); x += 10; } $('#objKey').css('left', x); $('#objKey').css('top', y); }); </script> |
Nov 10th
จากตัวอย่างข้างต้นจะเป็นการหมุน div ที่ทำขึ้น
โดย วิธีการหมุนจะใช้ css3 ช่วยในการหมุน div นั้นคือ
1 2 3 | // css WebkitTransform: 'rotate(10deg)' // สำหรับ chrome และ safari -moz-transform: 'rotate(10deg)' // สำหรับ firefox |
ตัวอย่าง code ทั้งหมดที่ใช้
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <div id='obj_1' style="background-color:#bca; position:'absolute'; width:50px; height:50px;">[x]</div> <script type='text/javascript'> var degree = 0; setInterval(rotateDiv, 30); function rotateDiv(){ degree += 5; var obj1 = $('#obj_1'); obj_1.css({ WebkitTransform: 'rotate(' + degree + 'deg)'}); // For Mozilla browser: e.g. Firefox obj_1.css({ '-moz-transform': 'rotate(' + degree + 'deg)'}); } </script> |
Nov 9th
จากตัวอย่างข้างบนจะเป็นการเลื่อน DIV : obj1 ซึ่งจะมีข้อความ [x] อยู่ข้างใน
ใช้ jQuery เป็น javascript library
1 2 3 4 5 6 7 8 9 10 11 | <div id='obj1' style="background-color:#bca;">[x]</div> <script type='text/javascript'> $('#obj1').css('position', 'absolute'); moveRight(); function moveRight(){ $('#obj1').animate({"left": "+=600px"}, 2000, 'linear', moveLeft); } function moveLeft(){ $('#obj1').animate({"left": "-=600px"}, 2000, 'swing', moveRight); } </script> |
จะใช้ jQuery function ที่ชื่อว่า animate ในการทำงาน
- parameter ตัวแรก จะเป็น properties div ในลักษณะของ css ตัวอย่างเช่น left, right, width, height
- parameter ตัวที่สอง จะเป็น เวลาที่ใช้ทั้งหมด (duration)
- parameter ตัวที่สาม เป็นลักษณะการเครื่องที่ ซึ่งจะเป็นแบบ ตรง ๆ (linear), หรือแบบสวิง (‘swing’) ก็สามารถปรับได้ที่นี่
default จะเป็น swing
- parameter ตัวสุดท้ายจะเป็น function ไว้เรียกตอนทำงานเสร็จสิ้นแล้ว
อ้างอิง : http://api.jquery.com/animate/