Lab For Do Game

ค้นคว้า วิจัย ลองทำไปเรื่อย

Follow me on TwitterRSS Feeds

  • Home
  • About

วาด circle ใน div ด้วย html 5

Nov 29th

Posted by admin in HTML5

No comments

สำหรับการขยับวัตถุในตัวอย่างข้างบน จะใช้วิธีการ วาด 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>
algorithm, Array, canvas, Javascript, Timer

ใส่ รูปวงกลม หลายอัน ใน Canvas โดยใช้ HTML 5 + jQuery

Nov 24th

Posted by admin in HTML5

No comments

สำหรับการทำ ดังตัวอย่าง จะใช้วิธีการ วน 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>
canvas, idea, Javascript

ดักจับตำแหน่ง mouse ใน html5 ด้วย jQuery

Nov 24th

Posted by admin in HTML5

No comments

สามารถประกาศดักจับ 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>

อ้างอิง http://api.jquery.com/category/events/mouse-events/

event, Javascript, mouse, MouseCursor

ใส่รูป Image ลงใน Canvas ด้วย HTML 5 + javascript

Nov 22nd

Posted by admin in HTML5

No comments

สำหรับ การใส่รูป 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>
canvas, Image, resize

วาดกรอบสี่เหลี่ยมใน HTML5 ด้วย canvas

Nov 22nd

Posted by admin in HTML5

No comments

สำหรับการวาด รูปสี่เหลี่ยมใน 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>
canvas, rectangle

การวาดเส้นใน HTML5 โดยใช้ canvas

Nov 18th

Posted by admin in HTML5

No comments


สำหรับ การวาดวงกลม ใน 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>
canvas, line

การวาดรูปวงกลมใน HTML5 โดยใช้ Canvas

Nov 18th

Posted by admin in HTML5

No comments


สำหรับ การวาดวงกลม ใน 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>
canvas, circle

keyboard input ใน javascript

Nov 16th

Posted by admin in HTML5

2 comments

[x]


[ทดลอง โดย กด 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>
keydown, keypress

หมุน div ใน html5 ด้วย jquery

Nov 10th

Posted by admin in HTML5

No comments

[x]


จากตัวอย่างข้างต้นจะเป็นการหมุน 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>
DIV, rotation

ทดสอบการเคลื่อนที่ของ div ใน html5

Nov 9th

Posted by admin in HTML5

No comments

[x]

จากตัวอย่างข้างบนจะเป็นการเลื่อน 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/

movement
12345»10...Last »
    • Recent comments
    • Popular posts
    • Archives
    • Tags
    • Categories
    • Air (1)
    • AS3 (105)
      • AI (3)
      • Away3D (7)
      • BitmapData (3)
      • geometry (52)
      • Tile Map (8)
    • Facebook (2)
    • Flex (5)
    • HTML5 (10)
    • iphone (2)
    • java (1)
    • Javascript (12)
      • jQuery (10)
    • PHP (4)
    AI Air algorithm amfPHP Array as3 away3d Background BitmapData canvas colision Date dispatchEvent embed source Facebook filter Flex flickr flv font fullscreen geometry google map idea iphone Javascript loading MouseCursor panorama Particle perspective php progress RegEx rotation sound sql injection TextField TileMap Timer tweener UI webcam xml Youtube
    • November 2011 (10)
    • May 2011 (1)
    • March 2011 (1)
    • February 2011 (1)
    • January 2011 (1)
    • December 2010 (2)
    • November 2010 (5)
    • October 2010 (9)
    • September 2010 (24)
    • August 2010 (31)
    • July 2010 (40)
    • June 2010 (1)
    • ทำป้อมปืน Turrets ใน actionscript 3 (4)
    • ทำ isometric tilemap ใน actionscript3 (4)
    • เชื่อมต่อ flash as3 กับ amfPHP(1.9) โดย flashDevelop (3)
    • ทำ Tile Map ใน actionscript3 (3)
    • ทำไฟกระพริบด้วย actionscript 3 (3)
    • ทำพลุไฟ ใน flash ด้วย actionscript 3 (3)
    • นับเวลากับ Timer ใน actionscript3 (2)
    • เปลี่ยน Mouse Cursor ใน flash ด้วย as3 (2)
    • ตรวจสอบการชน ด้วย hitTestObject (AS3) (2)
    • Font ใน TextField (actionscript 3) (2)
    • MayCM: ขอบคุณมากครับ นั่งหาทั้งวันกว่าจะเจอ T^T...
    • admin: รู้สึกว่า จะใช้ e.preventDefault(); แทน return 0 นะ :D
    • iporsut: ผมว่าพี่ต้อง return 0 ใน function ด้วยนะครับ ไม่งั้นมันจะทำงานปกติของ event ด้วยเช่นผมกดลง มันก็ยังค...
    • admin: เขียนใน flashDeveloper ครับ
    • ปวีณา: ช่วยแนะนำวิธีการใช้ หรือใส่ไฟล์หน่อยค๊า มือใหม่...
    • puk: Sprite คือไฟล์อะไรอ่าค่ะ
    • puk: ต้องเอาโค๊ดพวกนี้ไมปวางไว้ตรงไหนอ่าค่ะ ในหน้าหลักรึว่าในตัวซิมโบลวงกลมที่เราสรา้งอ่าา...
    • SupamaN: เจ๋งดีนะคับ ขอเอาไปใช้หน่อยนะ ขอบคุนคับ อิอิ...
  • My latest tweets

    Loading tweets...
    Follow me on Twitter!
  • User Login






    • Lost your password?
  • Blogroll

    • ActionScript 3- kirupaForum
    • as3isolib
    • as3url
    • emanuele feronato
    • facebook-actionscript-api
    • Flash Develop
    • master page
    • tweener
    • wonderfl




Mystique theme by digitalnature | Powered by WordPress
RSS Feeds XHTML 1.1 Top