<h2>html5 Canvas วิà¸à¸µà¸à¸³ Animation à¸à¸²à¸ Sprite Sheet</h2>
<p><strong>html5 Canvas วิà¸à¸µà¸à¸³ Animation à¸à¸²à¸ Sprite Sheet ( Frame by frame )</strong></p>
<p>Sprite Sheet หมายà¸à¸¶à¸à¸£à¸¹à¸à¹à¸à¹à¸à¹à¸«à¸à¹à¸à¸µà¹à¸ ายà¹à¸à¸à¸°à¹à¸à¹à¸à¸£à¸¹à¸à¸¢à¹à¸à¸¢ ๠à¹à¸§à¹à¸à¸µà¸à¸«à¸¥à¸²à¸¢à¸£à¸¹à¸ à¹à¸¥à¹à¸§à¸à¹à¸à¸¢à¸à¸³à¸¡à¸²à¸à¸±à¸à¹à¸à¹à¸à¹à¸à¹à¸à¸²à¸à¹à¸à¸à¸²à¸°à¸ªà¹à¸§à¸à¸à¸µà¹à¸à¸³à¹à¸à¹à¸à¸à¹à¸à¸à¹à¸ªà¸à¸à¸à¸¥à¹à¸à¹à¸²à¸à¸±à¹à¸ à¸à¸±à¸§à¸à¸¢à¹à¸²à¸à¹à¸à¹à¸à¹à¸à¸£à¸¹à¸à¸à¸à¸²à¸ 1024&#215;1024 pixel à¸à¸²à¸à¸à¸°à¸¡à¸µà¸à¸±à¹à¸à¸£à¸¹à¸à¸à¹à¸à¹à¸¡à¹ à¸à¸ à¹à¸à¹à¸à¸¡à¸à¹à¸²à¸ ๠à¹à¸à¹à¸à¸¡à¸ªà¹ à¹à¸¡à¸·à¹à¸à¸à¹à¸à¸à¸à¸²à¸£à¹à¸ªà¸à¸à¸à¸¥à¸à¹à¸à¹à¸¡à¹ à¸à¹à¹à¸à¸à¸£à¸à¸à¹à¸à¸²à¹à¸à¸à¸²à¸°à¸à¹à¸à¹à¸¡à¹à¹à¸à¹à¸²à¸à¸±à¹à¸à¸¡à¸²à¹à¸ªà¸à¸ à¹à¸à¹à¸à¸à¸²à¸£à¹à¸«à¸¥à¸à¸£à¸¹à¸à¹à¸à¹à¸à¹à¸«à¸à¹à¹à¸à¸µà¸¢à¸à¸à¸£à¸±à¹à¸à¹à¸à¸µà¸¢à¸§ à¹à¸à¹à¸ªà¸²à¸¡à¸²à¸£à¸à¸à¸³à¹à¸à¹à¸à¹à¹à¸à¹à¸à¸±à¸à¸«à¸¥à¸²à¸¢ ๠สà¹à¸§à¸à¸à¸à¸à¸à¸²à¸<!--more--></p>
<p>à¸à¸£à¸°à¹à¸¢à¸à¸à¹à¸à¸à¸à¸à¸²à¸£à¹à¸à¹à¸à¸²à¸à¸à¸²à¸£à¹à¸«à¸¥à¸à¸à¹à¸à¸¡à¸¹à¸¥à¸£à¸¹à¸à¸ าà¸à¹à¸à¸£à¸¹à¸à¹à¸à¸ sprite sheet à¸à¹à¹à¸à¸·à¹à¸à¸¥à¸à¸à¹à¸à¸à¸´à¸à¸à¸¥à¸²à¸à¸à¸à¸à¸¥à¸³à¸à¸±à¸à¸à¸²à¸£à¹à¸«à¸¥à¸à¸à¹à¸à¸¡à¸¹à¸¥ ลà¸à¸à¸£à¸´à¸¡à¸²à¸à¸à¸²à¸£à¹à¸«à¸¥à¸à¸à¹à¸à¸¡à¸¹à¸¥à¸¡à¸²à¸à¹à¸à¸´à¸à¸à¸³à¹à¸à¹à¸ à¸à¹à¸§à¸¢à¹à¸«à¹à¹à¸à¸¡à¸«à¸£à¸·à¸à¹à¸à¸£à¹à¸à¸£à¸¡à¸à¸³à¸à¸²à¸à¹à¸à¹à¹à¸£à¹à¸§à¸à¸¶à¹à¸</p>
<p><strong>à¸à¸±à¸§à¸à¸¢à¹à¸²à¸ Animation frame by frame à¸à¸²à¸ Sprite Sheet</strong><br />
<img src="https://www.codebee.co.th/labs/wp-content/uploads/2018/02/html5canvas_animation.gif" alt="html5canvas_animation" /></p>
<p>ลิà¸à¸à¹à¸à¸±à¸§à¸à¸¢à¹à¸²à¸ à¹à¸¥à¸°à¸à¸²à¸§à¸à¹à¹à¸«à¸¥à¸à¹à¸à¹à¸ <a href="https://www.codebee.co.th/labs/examples/html5canvas_animate/html5canvas_animate.html" target="_blank" rel="noopener">html5 Canvas วิà¸à¸µà¸à¸³ Animation à¸à¸²à¸ Sprite Sheet</a></p>
<h4>à¹à¸«à¸¥à¸ sprite sheet à¹à¸¥à¸°à¸à¸³ Animate à¹à¸à¸ frame by frame</h4>
<ul>
<li style="list-style-type: none">
<ul>
<li><strong>สรà¹à¸²à¸à¹à¸à¸¥à¹ sprite sheet</strong></li>
</ul>
</li>
</ul>
<p>à¹à¸à¹à¹à¸à¸£à¹à¸à¸£à¸¡à¸ªà¸³à¸«à¸£à¸±à¸à¸ªà¸£à¹à¸²à¸à¹à¸à¸¥à¹ sprite sheet à¹à¸à¹à¸à¹à¸à¸£à¹à¸à¸£à¸¡ TexturePacker หรืà¸à¸¥à¸à¸à¸à¹à¸à¸«à¸²à¸£à¸¹à¸à¸¡à¸²à¹à¸§à¹à¸ªà¸³à¸«à¸£à¸±à¸à¸à¸à¸ªà¸à¸ à¸à¸£à¸°à¸¡à¸²à¸à¸à¸µà¹</p>
<p><img src="https://www.codebee.co.th/labs/wp-content/uploads/2018/02/walking.png" alt="html5 canvas วิà¸à¸µà¸à¸³ animation à¸à¸²à¸ sprite sheet" /></p>
<ul>
<li style="list-style-type: none">
<ul>
<li><strong>สรà¹à¸²à¸ canvas div</strong></li>
</ul>
</li>
</ul>
<p>à¹à¸à¸ªà¹à¸§à¸à¸à¸à¸ html body à¹à¸«à¹à¸ªà¸£à¹à¸²à¸ div à¹à¸¥à¸°à¸à¸±à¹à¸à¸à¸·à¹à¸ id à¸à¸±à¸à¸à¸µà¹</p>
<pre class="lang:default decode:true "> 
 
	 
 
 
	<canvas id="canvas"></canvas> 
 
 
</pre>
<ul>
<li style="list-style-type: none">
<ul>
<li><strong>à¸à¸±à¸§à¹à¸à¸£à¸«à¸¥à¸±à¸ à¹à¸¥à¸°à¸à¸²à¸£à¹à¸«à¸¥à¸à¸£à¸¹à¸à¹à¸à¹à¸²à¸¡à¸²à¹à¸à¹à¸à¸²à¸à¹à¸ Canvas</strong></li>
</ul>
</li>
</ul>
<pre class="lang:default decode:true ">var hero; // à¸à¸±à¸à¸à¹à¸à¸±à¹à¸à¸«à¸¥à¸±à¸à¸ªà¸³à¸«à¸£à¸±à¸à¸à¸²à¸£ วาà¸à¹à¸¥à¸°à¹à¸ªà¸à¸à¸§à¸à¹à¸à¹à¸¥à¸°à¹à¸à¸£à¸¡ 
var heroSrc; // à¸à¸²à¸à¸à¸à¸à¸£à¸¹à¸à¸à¸µà¹ sprite à¸à¸°à¸à¹à¸à¸à¸§à¸²à¸à¸¥à¸à¸à¸ canvas 
var canvas; // canvas หรืà¸à¸à¸·à¹à¸à¸à¸µà¹à¸«à¸¥à¸±à¸à¸à¸µà¹à¸à¸°à¹à¸ªà¸à¸à¸à¸¥ animation 
 
canvas = document.getElementById("canvas"); 
canvas.width = 324; // à¸à¸§à¸²à¸¡à¸à¸§à¹à¸²à¸à¸à¸à¸ canvas 
canvas.height = 324; // à¸à¸§à¸²à¸¡à¸ªà¸¹à¸à¸à¸à¸ canvas 
 
 
heroSrc = new Image();	 
heroSrc.addEventListener("load", loopHero); // à¹à¸«à¸¥à¸à¹à¸ªà¸£à¹à¸à¹à¸«à¹à¹à¸£à¸´à¹à¸¡à¸à¸³à¸à¸²à¸à¸à¸µà¹à¸à¸±à¸à¸à¹à¸à¸±à¹à¸ loopHero 
heroSrc.src = "walking.png"; // à¹à¸«à¸¥à¸à¸£à¸¹à¸ sprite sheet</pre>
<p> ;</p>
<ul>
<li style="list-style-type: none">
<ul>
<li><strong>à¸à¸±à¸à¸à¹à¸à¸±à¹à¸à¸ªà¸³à¸«à¸£à¸±à¸à¸à¸²à¸£à¸§à¸²à¸à¹à¸à¹à¸¥à¸°à¹à¸à¸£à¸¡à¸¥à¸à¸à¸ canvas à¹à¸¥à¸°à¸§à¸ loop frame</strong></li>
</ul>
</li>
</ul>
<p>à¸à¸²à¸à¸£à¸¹à¸à¸à¸±à¸§à¸à¸¢à¹à¸²à¸ à¸à¸°à¸¡à¸µà¸à¸à¸²à¸ 1980&#215;324 px à¹à¸¥à¸°à¸¡à¸µà¸à¸±à¹à¸à¸«à¸¡à¸ 12 à¹à¸à¸£à¸¡ à¹à¸à¸à¸²à¸£à¸§à¸²à¸à¹à¸à¹à¸¥à¸°à¸à¸£à¸±à¹à¸ à¸à¸°à¹à¸à¹à¸²à¸à¸±à¸ 1980/12 = 165 à¹à¸à¸£à¸²à¸°à¸à¸°à¸à¸±à¹à¸à¹à¸à¹à¸¥à¸°à¸£à¸¹à¸à¸à¸°à¸¡à¸µà¸à¸à¸²à¸ 165&#215;324 à¹à¸à¸à¸²à¸£à¸à¸³à¸£à¸¹à¸ sprite sheet à¸à¸±à¹à¸ à¸à¸à¸²à¸à¸à¸à¸à¸£à¸¹à¸à¸à¸³à¹à¸à¹à¸à¸à¸°à¸à¹à¸à¸à¸à¸³à¸¡à¸²à¸«à¸²à¸£à¸à¹à¸§à¸¢à¸à¸³à¸à¸§à¸à¹à¸à¸£à¸¡à¹à¸à¹à¸¥à¸à¸à¸±à¸§ à¹à¸à¸·à¹à¸à¹à¸«à¹ animation à¸à¸¹ smooth à¸à¸µà¹à¸ªà¸¸à¸</p>
<p><strong>// à¸à¸±à¸à¸à¹à¸à¸±à¹à¸ update สำหรัà¸à¸à¸±à¸à¸à¸³à¸à¸§à¸à¹à¸à¸£à¸¡ à¹à¸¡à¸·à¹à¸à¸ªà¸´à¹à¸à¸ªà¸¸à¸à¸à¹à¹à¸«à¹à¸§à¸à¸à¸¥à¸±à¸à¸¡à¸²à¹à¸£à¸´à¹à¸¡à¸à¸µà¹à¹à¸à¸£à¸¡à¹à¸£à¸à¹à¸«à¸¡à¹</strong></p>
<pre class="lang:default decode:true ">function sprite (options) { 
	var ops = {}, 
		frameIndex = 0, 
		tickCount = 0, 
		ticksPerFrame = options.ticksPerFrame || 0, 
		numberOfFrames = options.numberOfFrames || 1; 
	 
	ops.context = options.context; 
	ops.width = options.width; 
	ops.height = options.height; 
	ops.image = options.image; 
	 
	 
	ops.update = function () { 
		tickCount += 1; 
		if (tickCount >; ticksPerFrame) { 
			tickCount = 0; 
			if (frameIndex <; numberOfFrames - 1) { 
				frameIndex += 1; 
			} else { 
				frameIndex = 0; 
			} 
		} 
	}; 
</pre>
<p><strong>// à¸à¸±à¸à¸à¹à¸à¸±à¹à¸ render สำหรัà¸à¸§à¸²à¸à¹à¸à¹à¸¥à¸°à¸£à¸¹à¸à¸¥à¸à¹à¸à¸à¸ canvas </strong><br />
à¹à¸à¹à¹à¸¡à¸à¸à¸ context.drawImage() à¸à¸³à¸«à¸à¸à¸à¸³à¹à¸«à¸à¹à¸à¸à¸µà¹à¸à¸°à¹à¸£à¸´à¹à¸¡à¸§à¸²à¸à¹à¸¥à¸°à¸à¸à¸²à¸à¸à¸à¸à¹à¸à¹à¸¥à¸°à¹à¸à¸£à¸¡</p>
<pre class="lang:default decode:true ">	ops.render = function () { 
	 this.context.clearRect(0, 0, this.width, this.height); // à¹à¸à¸¥à¸µà¸¢à¸£à¹à¸£à¸¹à¸à¹à¸à¹à¸²à¸à¸¸à¸à¸à¸£à¸±à¹à¸à¸à¹à¸à¸à¸§à¸²à¸à¸£à¸¹à¸à¹à¸«à¸¡à¹à¸¥à¸à¹à¸ 
	 this.context.drawImage( 
		this.image, 
		frameIndex * this.width / numberOfFrames, 
		0, 
		this.width / numberOfFrames, 
		this.height, 
		0, 
		0, 
		this.width / numberOfFrames, 
		this.height); 
	}; 
	 
	return ops; 
}</pre>
<p><strong>// à¸à¸§à¸²à¸¡à¸«à¸¡à¸²à¸¢à¸à¸à¸ à¸à¸²à¸£à¸²à¸¡à¸´à¹à¸à¸à¸£à¹ (Parameters) à¹à¸à¹à¸¥à¸°à¸à¸±à¸§à¸à¸à¸à¹à¸¡à¸à¸à¸ drawImage()</strong><br />
<ins datetime="2018-02-05T08:51:18+00:00"></ins></p>
<p><img src="https://www.codebee.co.th/labs/wp-content/uploads/2018/02/html5_drawImage.png" alt="html5_drawImage" /></p>
<p>ลิà¸à¸à¹à¸à¸±à¸§à¸à¸¢à¹à¸²à¸ à¹à¸¥à¸°à¸à¸²à¸§à¸à¹à¹à¸«à¸¥à¸à¹à¸à¹à¸ <a href="https://www.codebee.co.th/labs/examples/html5canvas_animate/html5canvas_animate.html" target="_blank" rel="noopener">html5 Canvas วิà¸à¸µà¸à¸³ Animation à¸à¸²à¸ Sprite Sheet</a></p>

html5 Canvas วิธีทำ Animation จาก Sprite Sheet

html5 canvas animation
