-
Notifications
You must be signed in to change notification settings - Fork 132
/
index.ts
466 lines (396 loc) · 12.3 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
import Field from './Field';
import type { IField } from './Field';
import Vector from './Vector';
import { isFunction, isNumber, isString, isValide } from './utils';
export const defaultOptions = {
globalAlpha: 0.9, // 全局透明度
lineWidth: 1, // 线条宽度
colorScale: '#fff',
velocityScale: 1 / 25,
// particleAge: 90,
maxAge: 90, // alias for particleAge
// particleMultiplier: 1 / 300, // TODO: PATHS = Math.round(width * height * particleMultiplier);
paths: 800,
frameRate: 20,
useCoordsDraw: true,
};
type emptyFunc = (v?: any) => number;
export interface IOptions {
/**
* 全局透明度,主要影响粒子拖尾效果,默认 0.9
*/
globalAlpha: number; // 全局透明度
/**
* 线条宽度
*/
lineWidth: number | emptyFunc;
/**
* 粒子颜色配置, 默认#fff,当为回调函数时,参数 function(m:对应点风速值) => string
*/
colorScale: string | string[] | emptyFunc;
/**
* 对于粒子路径步长的乘积系数,默认 1 / 25
*/
velocityScale: number | emptyFunc;
/**
* 粒子路径能够生成的最大帧数,默认是 90
*/
particleAge?: number; // 粒子在重新生成之前绘制的最大帧数
/**
* 粒子路径能够生成的最大帧数,默认是 90;他代表的是我们的 `paths` 数量的粒子的消亡控制,最小值是 0,最大值是 `maxAge`,在运行到 age 数时会消失然后进行重启
*/
maxAge: number; // alias for particleAge
/**
* 粒子路径数量的系数,不推荐使用(视野宽度 * 高度 * 系数)
*/
particleMultiplier?: number; // TODO: PATHS = Math.round(width * height * that.particleMultiplier);
/**
* 生成的粒子数量
*/
paths: number | emptyFunc;
/**
* 用户自定义的帧率,默认是 20ms, 大概接近 50fps 帧,我们可能在某些场景需要降低帧率一保证渲染稳定性;注意此配置还会影响粒子运动的速度
*/
frameRate: number;
/**
* 用户配置的风速最小值,如果未配置会从传入的数据中计算
*/
minVelocity?: number;
/**
* 用户配置的风速最大值,如果未配置会从传入的数据中计算
*/
maxVelocity?: number;
/**
* 使用外部传入的坐标系统,默认是 `true`;某些场景下我们可能直接使用像素坐标。
*/
useCoordsDraw?: boolean;
}
function indexFor(m: number, min: number, max: number, colorScale: string[]) {
// map velocity speed to a style
return Math.max(0, Math.min(colorScale.length - 1, Math.round(((m - min) / (max - min)) * (colorScale.length - 1))));
}
class WindCore {
public static Field = Field;
public forceStop: boolean;
private ctx: CanvasRenderingContext2D;
private options: IOptions;
private field: Field;
private particles: any[] = [];
private animationLoop: number;
private then: number;
private generated = false;
private starting: boolean;
constructor(ctx: CanvasRenderingContext2D, options: Partial<IOptions>, field?: Field) {
this.ctx = ctx;
if (!this.ctx) {
throw new Error('ctx error');
}
this.animate = this.animate.bind(this);
this.setOptions(options);
if (field) {
this.updateData(field);
}
}
/**
* 设置配置项
* @param options
*/
public setOptions(options: Partial<IOptions>) {
this.options = { ...defaultOptions, ...options };
const { width, height } = this.ctx.canvas;
if ('particleAge' in options && !('maxAge' in options) && isNumber(this.options.particleAge)) {
// @ts-ignore
this.options.maxAge = this.options.particleAge;
}
if ('particleMultiplier' in options && !('paths' in options) && isNumber(this.options.particleMultiplier)) {
this.options.paths = Math.round(width * height * (this.options.particleMultiplier as number));
}
this.prerender();
}
/**
* 获取配置项
*/
public getOptions() {
return this.options;
}
/**
* 更新数据
* @param field
*/
public updateData(field: Field) {
this.field = field;
if (!this.generated) {
return;
}
this.particles = this.prepareParticlePaths();
}
// @ts-ignore
public project(...args: any[]): [number, number] | null {
throw new Error('project must be overriden');
}
// @ts-ignore
public unproject(...args: any[]): [number, number] | null {
throw new Error('unproject must be overriden');
}
/**
* 判断位置是否在当前视窗内
* @param coordinates
*/
public intersectsCoordinate(coordinates: [number, number]): boolean {
throw new Error('must be overriden');
}
/**
* 清空当前画布
*/
public clearCanvas() {
this.stop();
this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
this.forceStop = false;
}
isStop() {
return !this.starting;
}
/**
* 启动粒子动画
*/
public start() {
this.starting = true;
this.forceStop = false;
this.then = Date.now();
this.animate();
}
/**
* 停止粒子动画
*/
public stop() {
cancelAnimationFrame(this.animationLoop);
this.starting = false;
this.forceStop = true;
}
public animate() {
if (this.animationLoop) {
cancelAnimationFrame(this.animationLoop);
}
this.animationLoop = requestAnimationFrame(this.animate);
const now = Date.now();
const delta = now - this.then;
if (delta > this.options.frameRate) {
this.then = now - (delta % this.options.frameRate);
this.render();
}
}
/**
* 渲染前处理
*/
public prerender() {
this.generated = false;
if (!this.field) {
return;
}
this.particles = this.prepareParticlePaths();
this.generated = true;
if (!this.starting && !this.forceStop) {
this.starting = true;
this.then = Date.now();
this.animate();
}
}
/**
* 开始渲染
*/
public render() {
this.moveParticles();
this.drawParticles();
this.postrender();
}
/**
* each frame render end
*/
public postrender() {
//
}
private moveParticles() {
const { width, height } = this.ctx.canvas;
const particles = this.particles;
// 清空组
const maxAge = this.options.maxAge;
const velocityScale = isFunction(this.options.velocityScale)
? this.options.velocityScale()
: this.options.velocityScale;
let i = 0;
const len = particles.length;
for (; i < len; i++) {
const particle = particles[i];
if (particle.age > maxAge) {
particle.age = 0;
// restart, on a random x,y
this.field.randomize(particle, width, height, this.unproject);
}
const x = particle.x;
const y = particle.y;
const vector = this.field.interpolatedValueAt(x, y);
if (vector === null) {
particle.age = maxAge;
} else {
const xt = x + vector.u * velocityScale;
const yt = y + vector.v * velocityScale;
if (this.field.hasValueAt(xt, yt)) {
// Path from (x,y) to (xt,yt) is visible, so add this particle to the appropriate draw bucket.
particle.xt = xt;
particle.yt = yt;
particle.m = vector.m;
} else {
// Particle isn't visible, but it still moves through the field.
particle.x = xt;
particle.y = yt;
particle.age = maxAge;
}
}
particle.age++;
}
}
private fadeIn() {
const prev = this.ctx.globalCompositeOperation; // lighter
this.ctx.globalCompositeOperation = 'destination-in';
this.ctx.fillRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
this.ctx.globalCompositeOperation = prev;
}
private drawParticles() {
const particles = this.particles;
this.fadeIn();
this.ctx.globalAlpha = this.options.globalAlpha;
this.ctx.fillStyle = `rgba(0, 0, 0, ${this.options.globalAlpha})`;
this.ctx.lineWidth = (isNumber(this.options.lineWidth) ? this.options.lineWidth : 1) as number;
this.ctx.strokeStyle = (isString(this.options.colorScale) ? this.options.colorScale : '#fff') as string;
let i = 0;
const len = particles.length;
if (this.field && len > 0) {
let min: number;
let max: number;
// 如果配置了风速范围
if (isValide(this.options.minVelocity) && isValide(this.options.maxVelocity)) {
min = this.options.minVelocity as number;
max = this.options.maxVelocity as number;
} else {
// 未配置风速范围取格点数据中的最大风速和最小风速
[min, max] = this.field.range as [number, number];
}
for (; i < len; i++) {
this[this.options.useCoordsDraw ? 'drawCoordsParticle' : 'drawPixelParticle'](particles[i], min, max);
}
}
}
/**
* 用于绘制像素粒子
* @param particle
* @param min
* @param max
*/
private drawPixelParticle(particle: any, min: number, max: number) {
// TODO 需要判断粒子是否超出视野
// this.ctx.strokeStyle = color;
const pointPrev: [number, number] = [particle.x, particle.y];
// when xt isn't exit
const pointNext: [number, number] = [particle.xt, particle.yt];
if (
pointNext &&
pointPrev &&
isValide(pointNext[0]) &&
isValide(pointNext[1]) &&
isValide(pointPrev[0]) &&
isValide(pointPrev[1]) &&
particle.age <= this.options.maxAge
) {
this.ctx.beginPath();
this.ctx.moveTo(pointPrev[0], pointPrev[1]);
this.ctx.lineTo(pointNext[0], pointNext[1]);
if (isFunction(this.options.colorScale)) {
// @ts-ignore
this.ctx.strokeStyle = this.options.colorScale(particle.m) as string;
} else if (Array.isArray(this.options.colorScale)) {
const colorIdx = indexFor(particle.m, min, max, this.options.colorScale);
this.ctx.strokeStyle = this.options.colorScale[colorIdx];
}
if (isFunction(this.options.lineWidth)) {
// @ts-ignore
this.ctx.lineWidth = this.options.lineWidth(particle.m) as number;
}
particle.x = particle.xt;
particle.y = particle.yt;
this.ctx.stroke();
}
}
/**
* 用于绘制坐标粒子
* @param particle
* @param min
* @param max
*/
private drawCoordsParticle(particle: any, min: number, max: number) {
// TODO 需要判断粒子是否超出视野
// this.ctx.strokeStyle = color;
const source: [number, number] = [particle.x, particle.y];
// when xt isn't exit
const target: [number, number] = [particle.xt, particle.yt];
if (
target &&
source &&
isValide(target[0]) &&
isValide(target[1]) &&
isValide(source[0]) &&
isValide(source[1]) &&
this.intersectsCoordinate(target) &&
particle.age <= this.options.maxAge
) {
const pointPrev = this.project(source);
const pointNext = this.project(target);
if (pointPrev && pointNext) {
this.ctx.beginPath();
this.ctx.moveTo(pointPrev[0], pointPrev[1]);
this.ctx.lineTo(pointNext[0], pointNext[1]);
particle.x = particle.xt;
particle.y = particle.yt;
if (isFunction(this.options.colorScale)) {
// @ts-ignore
this.ctx.strokeStyle = this.options.colorScale(particle.m) as string;
} else if (Array.isArray(this.options.colorScale)) {
const colorIdx = indexFor(particle.m, min, max, this.options.colorScale);
this.ctx.strokeStyle = this.options.colorScale[colorIdx];
}
if (isFunction(this.options.lineWidth)) {
// @ts-ignore
this.ctx.lineWidth = this.options.lineWidth(particle.m) as number;
}
this.ctx.stroke();
}
}
}
private prepareParticlePaths() {
// 由用户自行处理,不再自动修改粒子数
const { width, height } = this.ctx.canvas;
const particleCount = typeof this.options.paths === 'function' ? this.options.paths(this) : this.options.paths;
const particles: any[] = [];
if (!this.field) {
return [];
}
let i = 0;
for (; i < particleCount; i++) {
particles.push(
this.field.randomize(
{
age: this.randomize(),
},
width,
height,
this.unproject,
),
);
}
return particles;
}
private randomize() {
return Math.floor(Math.random() * this.options.maxAge);
}
}
export * from './utils';
export { WindCore, Field, Vector, IField };