我遇到以下问题:
我正在将一个纯 Javascript 项目迁移到 Angular2,我需要迁移或查看如何包含以下SunCalc库,我尝试迁移它,但是在调整代码时出现问题,我明确地只需要计算照明的部分某一天的月亮百分比。
我将我正在构建的服务留在库中,以防有人可以更正它或告诉我如何直接从 javascript 导入。
moon-calc.service.ts
import { Injectable } from '@angular/core';
import { Http, Headers, Response, RequestOptions, URLSearchParams } from '@angular/http';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/map';
const LUNAS = [
'MALA',
'REGULAR',
'BUENA',
'EXELENTE'
];
const COLORES_PIQUE = [
'p-mala',
'p-regular',
'p-buena',
'p-exelente'
];
const MONTHS = [
'ENERO',
'FEBRERO',
'MARZO',
'ABRIL',
'MAYO',
'JUNIO',
'JULIO',
'AGOSTO',
'SEPTIEMBRE',
'OCTUBRE',
'NOVIEMBRE',
'DICIEMBRE',
];
const DAYS_OF_MONTH = [
31,
28,
31,
30,
31,
30,
31,
31,
30,
31,
30,
31
];
const dayMs = 1000 * 60 * 60 * 24;
const J1970 = 2440588;
const J2000 = 2451545;
const rad = Math.PI / 180;
const e = rad * 23.4397; // obliquity of the Earth
/*
* Implements part of SunCalc.js
*/
@Injectable()
export class MoonCalcService {
constructor() {}
public calculateCalendar(month, year) {
let nd = this.getDaysOfMonth(month, year);
let fd = this.getFirstDay(month, year);
let week = new Array();
let weeks = new Array();
for (let i = 1; i <= fd; i++) {
week.push({ day: ''});
}
let k = 1;
for (let d = 1; d <= nd; d++) {
if (week.length == 7) {
weeks.push(week);
week = new Array();
k += 1;
}
// correcion porque $scope.month va de 0 a 11 en ves de 1 a 12
let day_ = year + '-' + (month + 1) + '-' + this.pad(d);
week.push({
day: this.pad(d),
type: COLORES_PIQUE[ this.fishingMoon(day_) ],
});
// console.log(day_, this.fishingMoon(day_));
}
while (week.length != 7) {
week.push({day:''});
}
weeks.push(week);
let bwe = [
{day: '', type: ''},
{day: '', type: ''},
{day: '', type: ''},
{day: '', type: ''},
{day: '', type: ''},
{day: '', type: ''},
{day: '', type: ''}
];
while (k < 6) {
weeks.push(bwe);
k += 1;
// console.log(k);
}
return weeks;
}
private getDaysOfMonth(month: number, year: number) {
if (month === 1 && year % 4 === 0) {
return 29; // febrero año bisiesto
} else {
return DAYS_OF_MONTH[month];
}
}
private getFirstDay(month: number, year: number) {
let d = new Date(year, month, 1);
if (d.getDay() === 0) {
return 6;
} else {
return d.getDay() - 1;
}
}
private toDays(date) {
return this.toJulian(date) - J2000;
}
private toJulian(date) {
return date.valueOf() / dayMs - 0.5 + J1970;
}
private rightAscension(l: number, b: number) {
let value = (Math.sin(l) * Math.cos(e) - Math.tan(b) * Math.sin(e), Math.cos(l));
return Math.atan(value);
}
private declination(l: number, b: number) {
return Math.asin(Math.sin(b) * Math.cos(e) + Math.cos(b) * Math.sin(e) * Math.sin(l));
}
private solarMeanAnomaly(d: number) {
return rad * (357.5291 + 0.98560028 * d);
}
private eclipticLongitude(M) {
let C = rad * (1.9148 * Math.sin(M) + 0.02 * Math.sin(2 * M) + 0.0003 * Math.sin(3 * M)); // equation of center
let P = rad * 102.9372; // perihelion of the Earth
return M + C + P + Math.PI;
}
private getMoonIllumination(date) {
let d = this.toDays(date);
let s = this.sunCoords(d);
let m = this.moonCoords(d);
console.log(d, s, m);
const sdist = 149598000; // distance from Earth to Sun in km
let phi = Math.acos(Math.sin(s.dec) * Math.sin(m.dec) +
Math.cos(s.dec) * Math.cos(m.dec) * Math.cos(s.ra - m.ra));
let _inc = (sdist * Math.sin(phi), m.dist - sdist * Math.cos(phi));
let inc = Math.atan(_inc);
let _angle = (Math.cos(s.dec) * Math.sin(s.ra - m.ra), Math.sin(s.dec) * Math.cos(m.dec) -
Math.cos(s.dec) * Math.sin(m.dec) * Math.cos(s.ra - m.ra));
console.log(_angle, Math.atan(_angle));
let angle = Math.atan(_angle);
return {
fraction: (1 + Math.cos(inc)) / 2,
phase: 0.5 + 0.5 * inc * (angle < 0 ? -1 : 1) / Math.PI,
angle: angle
};
}
private moonCoords(d) {
let L = rad * (218.316 + 13.176396 * d); // ecliptic longitude
let M = rad * (134.963 + 13.064993 * d); // mean anomaly
let F = rad * (93.272 + 13.229350 * d); // mean distance
let l = L + rad * 6.289 * Math.sin(M); // longitude
let b = rad * 5.128 * Math.sin(F); // latitude
let dt = 385001 - 20905 * Math.cos(M); // distance to the moon in km
return {
ra: this.rightAscension(l, b),
dec: this.declination(l, b),
dist: dt
};
}
private sunCoords(d): any {
let M = this.solarMeanAnomaly(d);
let L = this.eclipticLongitude(M);
return {
dec: this.declination(L, 0),
ra: this.rightAscension(L, 0)
};
}
private fishingMoon(fecha: string): number {
let date = new Date(fecha);
// correcion de un dia para coincidir con el calendario pesca estandar
let _date = date.setDate(date.getDate() + 1);
let p = this.getMoonIllumination(_date).phase;
console.log('phase', p);
if (p <= 0.25) {
return 3;
} else if (p <= 0.5) {
return 1;
} else if (p <= 0.75) {
return 0;
} else {
return 2;
}
}
private fishingMoonStatus(fecha: string): string {
return LUNAS[this.fishingMoon(fecha)];
}
private pad(s): string {
return parseInt(s, null) < 10 ? '0' + s : s;
}
}
明确地,我只需要更正getMoonIllumination()
. 从已经非常感谢了。一些替代库将是另一种解决方案。
谢谢
首先以纯 JavaScript (no-typescript) 导入库,例如这里是 jquery 和 toastr 的示例:
当你使用它们时,将变量声明为任何类型,你就可以毫无问题地使用它。
现在,在您的问题中,我们有 SunCalc:
然后我们声明变量,以便它允许使用库函数。
参考:https ://weblog.west-wind.com/posts/2016/Sep/12/External-JavaScript-dependencies-in-Typescript-and-Angular-2