<canvas id="sprite"> </canvas>
var first = '000'; // var last = 49; // var num = 0; // var maskFileName = ['2HULL_PEOPLE_P_A_', '.png'];// var dir = 'ship'; //,
var canvas = document.getElementById("sprite"); // canvas.width = (last + 1) * 75; // , canvas.height = 75; var width = 0; //, var context = canvas.getContext("2d"); //
function zerofikator(int, length) { // var prefix = ''; for (var i = num.toString().length; i < length; i++) { prefix += '0'; } return prefix + num; }
function draw() { var img = document.createElement('img'); /* - */ img.onload = function () { // context.drawImage(img, width, 0); width += 75; // , , if (zerofikator(num, first.length) != zerofikator(last, first.length)) { // num++; // draw(); // } } img.src = dir + '/' + maskFileName[0] + zerofikator(num, first.length) + maskFileName[1]; // } draw(); //
canvas.onclick = function () { window.location = context.canvas.toDataURL('image/png'); };
<div id="gun"></div> <div id="ship"></div>
var styles = {}; styles.cursor = 'pointer'; // , styles.width = '75px'; // styles.height = '75px'; var elementId = 'gun'; // id , var imgName = 'canvas.png'; //
function spriteAnimation(elementId, imgName, styles) { var img = document.createElement('img'); var offset = 0; img.onload = function () { // var element = document.getElementById(elementId); element.style.cursor = styles.cursor; element.style.width = styles.width; element.style.height = styles.height; element.style.background = "url('" + imgName + "') " + offset + "px 50%"; // var i = 0; element.onmouseover = function() { // interval = setInterval(function() { // if (offset < img.width) { // i++; // } else { i = 0; // } offset = 75 * i; // element.style.background = "url('" + imgName + "') " + offset + "px 50%"; // } , 1000/24) //24 } element.onmouseout = function(){ // clearInterval(interval) // ( ) } } img.src = imgName; // }
spriteAnimation(elementId, imgName, styles); spriteAnimation('ship', 'ship.png', styles);
<img src="fon.png" /> <div class="conteiner"><div id="gun"></div></div>
Source: https://habr.com/ru/post/130193/
All Articles