Skip to content

Commit

Permalink
polyfill for Math.log2() and Float32Array(),#27
Browse files Browse the repository at this point in the history
  • Loading branch information
iSpring committed Dec 9, 2016
1 parent 828537e commit 411aa4c
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 77 deletions.
7 changes: 0 additions & 7 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@

window.Kernel = Kernel;

window.testSearchService = function(wd) {
//http://apis.map.qq.com/jsapi?qt=syn&wd=酒店&pn=0&rn=5&output=json&b=115.63160389892579,40.208203447043054,117.18273610107423,39.59982601642098&l=11&c=000000
SearchService.search(wd, 11, 115.63160389892579, 40.208203447043054, 117.18273610107423, 39.59982601642098, function(response) {
console.table(response.detail.pois);
});
};

function startWebGL() {
var canvas = document.getElementById("canvasId");
window.globe = new Globe(canvas);
Expand Down
10 changes: 5 additions & 5 deletions src/world/Camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ class Camera extends Object3D {
private _getSafeThresholdLevelForNear() {
var thresholdNear = this.near * this.nearFactor;
var pow2level = this.baseTheoryDistanceFromCamera2EarthSurface / thresholdNear;
var level = (<any>Math).log2(pow2level);
var level = MathUtils.log2(pow2level);
//return Math.floor(level);
return level;
}
Expand Down Expand Up @@ -291,7 +291,7 @@ class Camera extends Object3D {
var halfRadianNewFov = radianNewFov / 2;
var tanNew = Math.tan(halfRadianNewFov);

var deltaLevel = (<any>Math).log2(tanOld / tanNew);
var deltaLevel = MathUtils.log2(tanOld / tanNew);
return deltaLevel;
}

Expand Down Expand Up @@ -533,10 +533,10 @@ class Camera extends Object3D {
var cameraPntCopy = cameraPnt.clone();
var targetPntCopy = targetPnt.clone();
var up = upDirection.clone();

var zAxis = new Vector(
cameraPntCopy.x - targetPntCopy.x,
cameraPntCopy.y - targetPntCopy.y,
cameraPntCopy.x - targetPntCopy.x,
cameraPntCopy.y - targetPntCopy.y,
cameraPntCopy.z - targetPntCopy.z
);
zAxis.normalize();
Expand Down
1 change: 0 additions & 1 deletion src/world/Renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class Renderer {
initWebGL(canvas);

if (!gl) {
alert("浏览器不支持WebGL或将WebGL禁用!");
console.debug("浏览器不支持WebGL或将WebGL禁用!");
return;
}
Expand Down
70 changes: 12 additions & 58 deletions src/world/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,81 +112,35 @@ const Utils = {
return v instanceof HTMLElement;
},

forEach(arr: Array<any>, func: ArrayVoidCallbackFunction): void{
if(this.isFunction(Array.prototype.forEach)){
arr.forEach(func);
}else{
for(var i=0;i<arr.length;i++){
func(arr[i],i,arr);
}
}
forEach(arr: ArrayLike<any>, func: ArrayVoidCallbackFunction): void{
return this.isFunction((<any>arr).forEach) ? (<any>arr).forEach(func) : Array.prototype.forEach.call(arr, func);
},

filter(arr: Array<any>, func: ArrayBooleanCallbackFunction): any[]{
var result: Array<any> = [];
if(this.isFunction(Array.prototype.filter)){
result = arr.filter(func);
}
else{
for(var i=0;i<arr.length;i++){
if(func(arr[i],i,arr)){
result.push(arr[i]);
}
}
}
return result;
filter(arr: ArrayLike<any>, func: ArrayBooleanCallbackFunction): any[]{
return this.isFunction((<any>arr).filter) ? (<any>arr).filter(func) : Array.prototype.filter.call(arr, func);
},

map(arr: Array<any>, func: ArrayAnyCallbackFunction): any[]{
var result:any[] = [];
if(this.isFunction(Array.prototype.map)){
result = arr.map(func);
}
else{
for(var i=0;i<arr.length;i++){
result.push(func(arr[i],i,arr));
}
}
return result;
map(arr: ArrayLike<any>, func: ArrayAnyCallbackFunction): any[]{
return this.isFunction((<any>arr).map) ? (<any>arr).map(func) : Array.prototype.map.call(arr, func);
},

some(arr: Array<any>, func: ArrayBooleanCallbackFunction): boolean{
if(this.isFunction(Array.prototype.some)){
return arr.some(func);
}
else{
for(var i=0;i<arr.length;i++){
if(func(arr[i],i,arr)){
return true;
}
}
return false;
}
some(arr: ArrayLike<any>, func: ArrayBooleanCallbackFunction): boolean{
return this.isFunction((<any>arr).some) ? (<any>arr).some(func) : Array.prototype.some.call(arr, func);
},

every(arr: Array<any>, func: ArrayBooleanCallbackFunction): boolean{
if(this.isFunction(Array.prototype.every)){
return arr.every(func);
}
else{
for(var i=0;i<arr.length;i++){
if(!func(arr[i],i,arr)){
return false;
}
}
return true;
}
every(arr: ArrayLike<any>, func: ArrayBooleanCallbackFunction): boolean{
return this.isFunction((<any>arr).every) ? (<any>arr).every(func) : Array.prototype.every.call(arr, func);
},

//过滤掉数组中重复的元素
filterRepeatArray(arr: Array<any>): any[]{
var cloneArray = this.map(arr,function(item: any){
var cloneArray = arr.map(function(item: any){
return item;
});
var simplifyArray: Array<any> = [];
while(cloneArray.length > 0){
var e = cloneArray[0];
var exist = this.some(simplifyArray,function(item: any){
var exist = simplifyArray.some(function(item: any){
return e.equals(item);
});
if(!exist){
Expand Down
2 changes: 1 addition & 1 deletion src/world/layers/TiledLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ abstract class TiledLayer extends GraphicGroup {

for (subLevel = lastLevel; subLevel >= 2; subLevel--) {
levelsTileGrids[subLevel] = parentTileGrids;//此行代码表示第subLevel层级的可见切片
parentTileGrids = Utils.map(parentTileGrids, function (item) {
parentTileGrids = parentTileGrids.map(function (item) {
return item.getParent();
});
parentTileGrids = Utils.filterRepeatArray(parentTileGrids);
Expand Down
9 changes: 9 additions & 0 deletions src/world/math/Math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import Vector = require('./Vector');
import Line = require('./Line');
import Plan = require('./Plan');

//Math.log2() method is defined in ES6
if(!(<any>Math).log2){
(<any>Math).log2 = (value: number) => (Math.log(value) / Math.log(2));
}

const MathUtils = {
ONE_RADIAN_EQUAL_DEGREE:57.29577951308232,//180/Math.PI
ONE_DEGREE_EQUAL_RADIAN:0.017453292519943295,//Math.PI/180
Expand All @@ -18,6 +23,10 @@ const MathUtils = {
TOP:"TOP",
BOTTOM:"BOTTOM",

log2(value: number){
return (<any>Math).log2(value);
},

izZero(value: any) : boolean {
if(!Utils.isNumber(value)){
throw "invalid value";
Expand Down
14 changes: 9 additions & 5 deletions src/world/math/Matrix.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
///<amd-module name="world/math/Matrix"/>

import Utils = require('../Utils');
import Vertice = require('./Vertice');
import Vector = require('./Vector');

Expand All @@ -18,7 +20,7 @@ class Matrix{
if(this === matrix){
return true;
}
return this.elements.every((ele: number, index: number) => {
return Utils.every(this.elements, (ele: number, index: number) => {
return ele === matrix.elements[index];
});
}
Expand Down Expand Up @@ -212,9 +214,11 @@ class Matrix{
}

setUniqueValue(value: number){
this.elements.forEach((ele, index) => {
this.elements[index] = value;
});
//TypeArray of IE11 doesn't support forEach method
var length = this.elements.length;
for(var i = 0; i < length; i++){
this.elements[i] = value;
}
}

/**
Expand Down Expand Up @@ -290,7 +294,7 @@ class Matrix{
}

hasNaN():boolean{
return this.elements.some(function(v){
return Utils.some(this.elements, function(v){
return isNaN(v);
});
}
Expand Down

0 comments on commit 411aa4c

Please sign in to comment.