Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update webglAtlas.js #273

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/WebGL/webglAtlas.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Texture = require('./texture.js');
let Texture = require('./texture.js');

module.exports = webglAtlas;

Expand All @@ -11,7 +11,7 @@ module.exports = webglAtlas;
* parameter a new canvas will be created.
*/
function webglAtlas(tilesPerTexture) {
var tilesPerRow = Math.sqrt(tilesPerTexture || 1024) << 0,
let tilesPerRow = Math.sqrt(tilesPerTexture || 1024) << 0,
tileSize = tilesPerRow,
lastLoadedIdx = 1,
loadedImages = {},
Expand All @@ -25,7 +25,7 @@ function webglAtlas(tilesPerTexture) {
}

// this is the return object
var api = {
let api = {
/**
* indicates whether atlas has changed texture in it. If true then
* some of the textures has isDirty flag set as well.
Expand Down Expand Up @@ -67,15 +67,15 @@ function webglAtlas(tilesPerTexture) {
return api;

function clearDirty() {
var i;
let i;
api.isDirty = false;
for (i = 0; i < textures.length; ++i) {
textures[i].isDirty = false;
}
}

function remove(imgUrl) {
var coordinates = loadedImages[imgUrl];
let coordinates = loadedImages[imgUrl];
if (!coordinates) {
return false;
}
Expand All @@ -87,12 +87,12 @@ function webglAtlas(tilesPerTexture) {
return true; // Ignore if it's last image in the whole set.
}

var tileToRemove = getTileCoordinates(coordinates.offset),
let tileToRemove = getTileCoordinates(coordinates.offset),
lastTileInSet = getTileCoordinates(lastLoadedIdx);

copy(lastTileInSet, tileToRemove);

var replacedOffset = loadedImages[trackedUrls[lastLoadedIdx]];
let replacedOffset = loadedImages[trackedUrls[lastLoadedIdx]];
replacedOffset.offset = coordinates.offset;
trackedUrls[coordinates.offset] = trackedUrls[lastLoadedIdx];

Expand All @@ -112,7 +112,7 @@ function webglAtlas(tilesPerTexture) {
if (loadedImages.hasOwnProperty(imgUrl)) {
callback(loadedImages[imgUrl]);
} else {
var img = new window.Image(),
let img = new window.Image(),
imgId = lastLoadedIdx;

lastLoadedIdx += 1;
Expand All @@ -127,20 +127,20 @@ function webglAtlas(tilesPerTexture) {
}

function createTexture() {
var texture = new Texture(tilesPerRow * tileSize);
let texture = new Texture(tilesPerRow * tileSize);
textures.push(texture);
}

function drawAt(tileNumber, img, callback) {
var tilePosition = getTileCoordinates(tileNumber),
let tilePosition = getTileCoordinates(tileNumber),
coordinates = {
offset: tileNumber
};

if (tilePosition.textureNumber >= textures.length) {
createTexture();
}
var currentTexture = textures[tilePosition.textureNumber];
let currentTexture = textures[tilePosition.textureNumber];

currentTexture.ctx.drawImage(img, tilePosition.col * tileSize, tilePosition.row * tileSize, tileSize, tileSize);
trackedUrls[tileNumber] = img.src;
Expand All @@ -152,7 +152,7 @@ function webglAtlas(tilesPerTexture) {
}

function getTileCoordinates(absolutePosition) {
var textureNumber = (absolutePosition / tilesPerTexture) << 0,
let textureNumber = (absolutePosition / tilesPerTexture) << 0,
localTileNumber = (absolutePosition % tilesPerTexture),
row = (localTileNumber / tilesPerRow) << 0,
col = (localTileNumber % tilesPerRow);
Expand Down Expand Up @@ -186,7 +186,7 @@ function webglAtlas(tilesPerTexture) {
}

function copy(from, to) {
var fromCanvas = textures[from.textureNumber].canvas,
let fromCanvas = textures[from.textureNumber].canvas,
toCtx = textures[to.textureNumber].ctx,
x = to.col * tileSize,
y = to.row * tileSize;
Expand Down