Skip to content

ThirteeNov

My personal blog about coding and internet

Menu
  • About me
  • About Zofia Kreasi
  • Cart
  • Checkout
  • Making an airplane game from scratch in Unity
  • My account
  • Privacy Policy
  • Privacy Policy – zkLeaderboard
  • Sample Page
  • Shop
  • Tutorials on Learning JavaScript
  • ZKAccounts – Privacy Policy
Menu

Playing looped image sequences with JavaScript

Posted on September 16, 2017August 4, 2020 by Habibie

If you have a sprite image (image sequences that merged into one file) and you want to play it in your website, you can use this script.

Watch the video tutorial here:

And here is the script:

function createImloop(imagefile, wh, frms, spd){
	document.getElementById("imloop").innerHTML = "<canvas id='canvas' width='" +wh+ "' height='" +wh+ "'></canvas>";
	var canvas = document.getElementById("canvas");
	var ctx = canvas.getContext("2d");
	ctx.width = wh;
	ctx.height = wh;
	var image = new Image();
	image.src = imagefile;
	image.onload = function(){
		var cFrame = 0;
		setInterval(function(){
			if(cFrame < frms-1) cFrame ++;
			else cFrame = 0;
			ctx.clearRect(0, 0, wh, wh);
			ctx.drawImage(image, cFrame * wh, 0, wh, wh, 0, 0, wh, wh);
		}, spd);
	}
}

That one for JavaScript, and this one is for HTML:

<!DOCTYPE html>
<html>
	<head>
		<title>Imloop by Zofia Kreasi</title>
	</head>
	<body>
		
		<div id="imloop"></div>
		<script src="imloop.js"></script>
		<script>
			var newImage = new createImloop("spritestrip.png", 256, 6, 50);
		</script>
		
	</body>
</html>
Post Views: 404
ciihuy2020

Welcome!

  • My YouTube Channel
  • My GitHub Page
  • About me

Categories

  • 3DVista
  • Android
  • Apache
  • C#
  • Cordova
  • Electron & Node JS
  • HTML5, CSS & JavaScript
  • iOS
  • Let's Make Unity Games
  • Misc
  • Photoshop
  • PHP
  • Python
  • Uncategorized
  • Unity
  • WordPress

Recent Posts

  • Hover Reveal script for Unity to show and hide object on mouse hover
  • How to Prevent UI Clicks in Unity from “Bleeding Through” to 3D Objects Behind Them
  • Make objects like wires and cables easily in Unity using Ciihuy Curved Mesh
  • [SOLVED] Can’t Add Custom Domain to Blogger After Losing CNAME Verification
  • iOS App Icon Generator by CiihuyCom
© 2026 ThirteeNov | Powered by Superbs Personal Blog theme