Skip to content

Commit

Permalink
Type image_sprite.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Jul 10, 2017
1 parent ca33f7b commit 1dbfb54
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions src/style/image_sprite.js
@@ -1,10 +1,18 @@
// @flow

const Evented = require('../util/evented');
const ajax = require('../util/ajax');
const browser = require('../util/browser');
const normalizeURL = require('../util/mapbox').normalizeSpriteURL;

class SpritePosition {
x: number;
y: number;
width: number;
height: number;
pixelRatio: number;
sdf: boolean;

constructor() {
this.x = 0;
this.y = 0;
Expand All @@ -16,8 +24,14 @@ class SpritePosition {
}

class ImageSprite extends Evented {
base: string;
retina: boolean;

data: ?{[string]: SpritePosition};
imgData: ?HTMLImageElement;
width: ?number;

constructor(base, eventedParent) {
constructor(base: string, eventedParent?: Evented) {
super();
this.base = base;
this.retina = browser.devicePixelRatio > 1;
Expand All @@ -28,24 +42,22 @@ class ImageSprite extends Evented {
ajax.getJSON(normalizeURL(base, format, '.json'), (err, data) => {
if (err) {
this.fire('error', {error: err});
return;
} else if (data) {
this.data = (data : any);
if (this.imgData) this.fire('data', {dataType: 'style'});
}

this.data = data;
if (this.imgData) this.fire('data', {dataType: 'style'});
});

ajax.getImage(normalizeURL(base, format, '.png'), (err, img) => {
if (err) {
this.fire('error', {error: err});
return;
}

this.imgData = browser.getImageData(img);
} else if (img) {
this.imgData = browser.getImageData(img);

this.width = img.width;
this.width = img.width;

if (this.data) this.fire('data', {dataType: 'style'});
if (this.data) this.fire('data', {dataType: 'style'});
}
});
}

Expand All @@ -69,7 +81,7 @@ class ImageSprite extends Evented {
}
}

getSpritePosition(name) {
getSpritePosition(name: string) {
if (!this.loaded()) return new SpritePosition();

const pos = this.data && this.data[name];
Expand Down

0 comments on commit 1dbfb54

Please sign in to comment.