提交 aace64f0 编写于 作者: M mehaotian

fix: 修复日历示例在低端机上卡顿的问题

上级 0abbee84
......@@ -43,8 +43,8 @@ const lunarYears = [
0x0d520, 0x0daa0, 0x16aa6, 0x056d0, 0x04ae0, 0x0a9d4, 0x0a2d0, 0x0d150, 0x0f252, 0x0d520
]
// ['月','正','一','二','三','四','五','六','七','八','九','十','冬','腊'];
const N_STR_3 = ["\u6708","\u6b63", "\u4e8c", "\u4e09", "\u56db", "\u4e94", "\u516d", "\u4e03", "\u516b", "\u4e5d", "\u5341", "\u51ac", "\u814a"]
// ['月','正','一','二','三','四','五','六','七','八','九','十','冬','腊'];
const N_STR_3 = ["\u6708", "\u6b63", "\u4e8c", "\u4e09", "\u56db", "\u4e94", "\u516d", "\u4e03", "\u516b", "\u4e5d", "\u5341", "\u51ac", "\u814a"]
// ['日','一','二','三','四','五','六','七','八','九','十']
const N_STR_1 = ["\u65e5", "\u4e00", "\u4e8c", "\u4e09", "\u56db", "\u4e94", "\u516d", "\u4e03", "\u516b", "\u4e5d", "\u5341"]
......@@ -82,6 +82,8 @@ export type LunarInfoType = {
export class Lunar {
private lunarYearDaysMap = new Map<number, number>()
private lunarMonthDaysMap = new Map<number, number[]>()
constructor() { }
/**
* 传入农历数字月份返回汉语通俗表示法
......@@ -90,10 +92,6 @@ export class Lunar {
* @eg:let cnMonth = calendar.toChinaMonth(12) ;//cnMonth='腊月'
*/
toChinaMonth(m : number, leap : boolean = false) : string { // 月 => \u6708
// if (m > 12 || m < 1) { return '' } // 若参数错误 返回-1
// let s = N_STR_3[m - 1]
// s += '\u6708'// 加上月字
// return s
return leap ? (N_STR_3[4] + N_STR_3[m] + N_STR_3[0]) : (N_STR_3[m] + N_STR_3[0]);
}
......@@ -148,27 +146,43 @@ export class Lunar {
// 某年份农历各月天数
lunarMonthDays(year : number) : number[] {
let lunarYear = lunarYears[year - 1900];
let monthDays : number[] = [];
for (let i = 4; i < 16; i++) {
let monthDay = (lunarYear >> i & 0x1) != 0 ? 30 : 29;
monthDays.push(monthDay);
}
monthDays.reverse();
// 添加闰月
let leapM = this.leapMonth(year);
if (leapM > 0) monthDays.splice(leapM, 0, leapDays(year));
return monthDays;
let monthDays = this.lunarMonthDaysMap.get(year)
if (monthDays != null) {
return monthDays
}
monthDays = [];
let lunarYear = lunarYears[year - 1900];
for (let i = 15; i >= 4; i--) {
let monthDay = (lunarYear >> i & 0x1) != 0 ? 30 : 29;
monthDays.push(monthDay);
}
// 添加闰月
let leapM = this.leapMonth(year);
if (leapM > 0) monthDays.splice(leapM, 0, leapDays(year));
this.lunarMonthDaysMap.set(year, monthDays)
return monthDays;
}
// 某年农历天数
lunarYearDays(year : number) : number {
if (this.lunarYearDaysMap.has(year)) {
return this.lunarYearDaysMap.get(year)!
}
let num = 0;
this.lunarMonthDays(year).forEach(item => {
num += item;
});
this.lunarYearDaysMap.set(year, num)
return num;
}
......@@ -188,8 +202,8 @@ export class Lunar {
let isLeap = moonDay.isLeap
// 计算农历日期
const IMonthCn = this.toChinaMonth(lMonth, isLeap)
const IMonthCn = this.toChinaMonth(lMonth, isLeap)
let IDayCn = lDay == 1 ? IMonthCn : this.toChinaDay(lDay)
// 是否今天
......@@ -280,6 +294,4 @@ export class Lunar {
return info
}
}
\ No newline at end of file
......@@ -167,9 +167,10 @@
drawWeek(weeks : Array<Array<DateType>>, time : string) {
const start_time = Date.now()
const refs = this.$refs['draw-weeks'] as INode
let ctx = refs.getDrawableContext()
const width = refs.getBoundingClientRect().width
const height = refs.getBoundingClientRect().height
let ctx = refs.getDrawableContext()
const dom = refs.getBoundingClientRect()
const width = dom.width
const height = dom.height
let week_len = weeks.length
const one_width = width / weeks[0].length
const one_height = height / week_len
......
......@@ -12,8 +12,11 @@ export type DateType = {
data ?: LunarInfoType
}
export class Calendar {
constructor() { }
export class Calendar {
private lunar:Lunar
constructor() {
this.lunar =new Lunar()
}
getDateInfo(time : string = '') : DateType {
const nowDate = this.getDate(time)
......@@ -121,8 +124,7 @@ export class Calendar {
* 计算阴历日期显示
*/
getlunar(year : number, month : number, date : number) : LunarInfoType {
const lunar = new Lunar()
return lunar.solar2lunar(year, month, date)
return this.lunar.solar2lunar(year, month, date)
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册