This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
174 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
<html> | ||
<head> | ||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> | ||
<script> | ||
|
||
var g_sheets = {}; | ||
var g_anims = {}; | ||
var g_index = {}; | ||
|
||
var g_anim = ''; | ||
var g_next_frame = 0; | ||
|
||
|
||
$(function(){ | ||
|
||
jQuery.support.cors = true; | ||
|
||
$.ajax({ | ||
'url' : 'http://api.glitch.com/simple/players.getAnimations', | ||
'dataType' : 'json', | ||
'data' : { 'player_tsid' : 'PLI10VSF8V7IE' }, | ||
'success' : function(data, textStatus, jqXHR){ | ||
if (data.ok){ | ||
g_sheets = data.sheets; | ||
g_anims = data.anims; | ||
build_index(); | ||
$('#loading').text("Loading sheets..."); | ||
load_sheets(); | ||
}else{ | ||
alert('api error'); | ||
} | ||
}, | ||
'error' : function(jqXHR, textStatus, errorThrown){ | ||
alert('api error'); | ||
alert(errorThrown); | ||
} | ||
}); | ||
}); | ||
|
||
function build_index(){ | ||
for (var i in g_sheets){ | ||
|
||
for (var j=0; j<g_sheets[i].frames.length; j++){ | ||
|
||
var col = j % g_sheets[i].cols; | ||
var row = Math.floor(j / g_sheets[i].cols); | ||
|
||
g_index[g_sheets[i].frames[j]] = [i, col, row]; | ||
} | ||
} | ||
} | ||
|
||
function load_sheets(){ | ||
for (var i in g_sheets){ | ||
g_sheets[i].img = new Image(); | ||
g_sheets[i].img.onload = function(i){ | ||
return function(){ | ||
image_loaded(i); | ||
}; | ||
}(i); | ||
g_sheets[i].img.onerror = image_failed; | ||
g_sheets[i].img.onabort = image_failed; | ||
g_sheets[i].loaded = false; | ||
g_sheets[i].img.src = g_sheets[i].url; | ||
} | ||
} | ||
|
||
function image_failed(){ | ||
alert('failed to load an image :('); | ||
} | ||
|
||
function image_loaded(idx){ | ||
|
||
g_sheets[idx].loaded = true; | ||
g_sheets[idx].frame_width = Math.round(g_sheets[idx].img.width / g_sheets[idx].cols); | ||
g_sheets[idx].frame_height = Math.round(g_sheets[idx].img.height / g_sheets[idx].rows); | ||
|
||
var done = 0; | ||
var num = 0; | ||
for (var i in g_sheets){ | ||
num++; | ||
if (g_sheets[i].loaded) done++; | ||
} | ||
|
||
if (done == num){ | ||
|
||
var choices = ''; | ||
for (var i in g_anims){ | ||
choices += "<option>"+i+"</option>"; | ||
} | ||
|
||
$('#choose-anim').html(choices).val('walk1x').change(function(){ | ||
g_anim = $('#choose-anim').val(); | ||
g_next_frame = 0; | ||
}); | ||
|
||
$('#loading').hide(); | ||
$('#content').show(); | ||
|
||
animate(); | ||
}else{ | ||
$('#loading').text("Loading sheets: "+done+"/"+num); | ||
} | ||
} | ||
|
||
function drawFrame(id){ | ||
|
||
//console.log('draw frame '+id); | ||
|
||
// find source coords | ||
var sheet = g_index[id][0]; | ||
var col = g_index[id][1]; | ||
var row = g_index[id][2]; | ||
|
||
var f_w = g_sheets[sheet].frame_width; | ||
var f_h = g_sheets[sheet].frame_height; | ||
var f_x = f_w * col; | ||
var f_y = f_h * row; | ||
|
||
var p_x = (300 / 2) - (f_w / 2); | ||
var p_y = (300 / 2) - (f_h / 2); | ||
|
||
var canvas = document.getElementById("my-canvas"); | ||
var context = canvas.getContext("2d"); | ||
|
||
context.clearRect(0, 0, 300, 300); | ||
context.drawImage(g_sheets[sheet].img, f_x, f_y, f_w, f_h, p_x, p_y, f_w, f_h); | ||
} | ||
|
||
function nextFrame(){ | ||
|
||
drawFrame(g_anims[g_anim][g_next_frame]); | ||
|
||
g_next_frame++; | ||
if (g_next_frame >= g_anims[g_anim].length){ | ||
g_next_frame = 0; | ||
} | ||
} | ||
|
||
function animate(){ | ||
|
||
g_anim = 'walk1x'; | ||
g_next_frame = 0; | ||
|
||
window.setInterval(nextFrame, 33); | ||
} | ||
|
||
</script> | ||
</head> | ||
<body> | ||
|
||
<h1>Avatar JS Tester</h1> | ||
|
||
<div id="loading">Loading...</div> | ||
<div id="content" style="display: none"> | ||
<canvas id="my-canvas" width="300" height="300" style="border: 1px solid black"></canvas><br /> | ||
Animation: | ||
<select id="choose-anim"></select> | ||
</div> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters