Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
2659 lines (2405 loc) · 96 KB
/
Copy pathapp.js
File metadata and controls
2659 lines (2405 loc) · 96 KB
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
/* ═══════════════════════ 墨韵 · 水墨绘制引擎 ═══════════════════════ */
const $ = s => document.querySelector(s);
const TAU = Math.PI * 2;
const clamp = (v, a, b) => v < a ? a : v > b ? b : v;
const rand = (a = 1, b) => b === undefined ? Math.random() * a : a + Math.random() * (b - a);
// 近似高斯分布,范围 -1..1,中心密集 —— 模拟笔毫聚锋
const gauss = () => (Math.random() + Math.random() + Math.random()) / 1.5 - 1;
const lerp = (a, b, t) => a + (b - a) * t;
const IS_COARSE = matchMedia('(pointer: coarse)').matches; // 触屏设备
// 逻辑绘制坐标系(笔刷/文字/位置等所有设定均以此为准,永不随分辨率变化)
const CANVAS_W = 1000, CANVAS_H = 1390;
// 超采样倍率:内部画布按 SS 倍像素渲染,再以同尺寸显示 —— 仅提升清晰度,相对比例不变
// 触屏内存预算紧,取较低倍率以防 iOS Safari 闪退
const SS = IS_COARSE ? 1.5 : 2;
const DEV_W = Math.round(CANVAS_W * SS), DEV_H = Math.round(CANVAS_H * SS);
/* ───────────── 文房定义 ───────────── */
const BRUSHES = [
{ id: 'gongbi', name: '狼毫', sub: '工笔', size: 8, varia: 0.35, type: 'fine' },
{ id: 'xieyi', name: '羊毫', sub: '写意', size: 36, varia: 0.9, type: 'soft' },
{ id: 'cunca', name: '散锋', sub: '皴擦', size: 46, varia: 0.7, type: 'bristle' },
{ id: 'pomo', name: '斗笔', sub: '泼墨', size: 104, varia: 0.5, type: 'broad' },
];
// 起收锋:起笔与提笔的粗细关系(全局运笔 + 停笔顺线共用)
const STROKE_TAPERS = [
{ id: 'out', name: '收锋', sub: '渐细' }, // 先粗后细
{ id: 'in', name: '入锋', sub: '渐粗' }, // 先细后粗
{ id: 'even', name: '匀劲', sub: '均衡' },
{ id: 'belly', name: '鼓腹', sub: '中肥' }, // 两头细、中间粗
];
// 皴法:仅散锋可用,重写毫束形态/侧锋/干擦/节奏,模拟四种山石皴擦
// spread 簇散开度 · clumpMul 簇数 · hairMul 毫数 · gap 起手断毫率 · skip 干擦跳笔率
// side 侧锋系数 · body 笔腹墨体 · bodyR 墨体半径 · hairW 毫宽区间 · taper 起收锋 · dab 短点断笔(雨点)
const CUN_METHODS = [
{ id: 'pima', name: '披麻', sub: '长披',
spread: 0.94, clumpMul: 1.18, hairMul: 1.25, gap: 0.05, skip: 0.10,
side: 0.42, body: 0.16, bodyR: 1.0, hairW: [0.5, 1.3], taper: 'even', dab: 0 },
{ id: 'fupi', name: '斧劈', sub: '侧扫',
spread: 1.18, clumpMul: 0.88, hairMul: 0.95, gap: 0.13, skip: 0.24,
side: 1.15, body: 0.20, bodyR: 0.85, hairW: [0.8, 2.1], taper: 'out', dab: 0 },
{ id: 'yudian', name: '雨点', sub: '短点',
spread: 0.92, clumpMul: 1.25, hairMul: 0.9, gap: 0.16, skip: 0.42,
side: 0.34, body: 0.03, bodyR: 0.6, hairW: [1.5, 3.4], taper: 'even', dab: 1, scatter: 1 },
{ id: 'juanyun', name: '卷云', sub: '圆浑',
spread: 0.66, clumpMul: 1.2, hairMul: 1.5, gap: 0.05, skip: 0.10,
side: 0.5, body: 0.42, bodyR: 1.4, hairW: [0.7, 1.8], taper: 'belly', dab: 0, grain: 1 },
];
const SIZE_TIERS = [
{ id: 'nuo', name: '搦', sub: '细毫' },
{ id: 'bao', name: '饱', sub: '饱毫' },
{ id: 'kuo', name: '阔', sub: '阔笔' },
{ id: 'ju', name: '巨', sub: '巨笔' },
];
// 每笔四档:size / varia / 戳印间距 / 流体注入 / 散锋簇密
const BRUSH_SIZE_TUNING = {
gongbi: {
nuo: { sizeMul: 0.5, variaMul: 0.72, spacingMul: 0.88, depositMul: 0.42, bristleMul: 1 },
bao: { sizeMul: 1, variaMul: 1, spacingMul: 1, depositMul: 1, bristleMul: 1 },
kuo: { sizeMul: 1.42, variaMul: 0.9, spacingMul: 1.06, depositMul: 1.12, bristleMul: 1 },
ju: { sizeMul: 1.88, variaMul: 0.82, spacingMul: 1.12, depositMul: 1.22, bristleMul: 1 },
},
xieyi: {
nuo: { sizeMul: 0.52, variaMul: 0.78, spacingMul: 0.9, depositMul: 0.62, bristleMul: 1 },
bao: { sizeMul: 1, variaMul: 1, spacingMul: 1, depositMul: 1, bristleMul: 1 },
kuo: { sizeMul: 1.48, variaMul: 1.06, spacingMul: 1.08, depositMul: 1.22, bristleMul: 1 },
ju: { sizeMul: 2.08, variaMul: 1.12, spacingMul: 1.14, depositMul: 1.48, bristleMul: 1 },
},
cunca: {
nuo: { sizeMul: 0.52, variaMul: 0.82, spacingMul: 0.94, depositMul: 0.68, bristleMul: 0.82 },
bao: { sizeMul: 1, variaMul: 1, spacingMul: 1, depositMul: 1, bristleMul: 1 },
kuo: { sizeMul: 1.52, variaMul: 1.06, spacingMul: 1.1, depositMul: 1.18, bristleMul: 1.18 },
ju: { sizeMul: 2.12, variaMul: 1.14, spacingMul: 1.18, depositMul: 1.38, bristleMul: 1.38 },
},
pomo: {
nuo: { sizeMul: 0.55, variaMul: 0.86, spacingMul: 0.9, depositMul: 0.72, bristleMul: 1 },
bao: { sizeMul: 1, variaMul: 1, spacingMul: 1, depositMul: 1, bristleMul: 1 },
kuo: { sizeMul: 1.5, variaMul: 1.02, spacingMul: 1.1, depositMul: 1.28, bristleMul: 1 },
ju: { sizeMul: 2.18, variaMul: 1.1, spacingMul: 1.16, depositMul: 1.58, bristleMul: 1 },
},
};
function brushProfile() {
const b = state.brush;
const tierId = state.sizeByBrush[b.id] || 'bao';
const tune = BRUSH_SIZE_TUNING[b.id][tierId] || BRUSH_SIZE_TUNING[b.id].bao;
return {
...b,
tierId,
size: b.size * tune.sizeMul,
varia: b.varia * tune.variaMul,
spacingMul: tune.spacingMul,
depositMul: tune.depositMul,
bristleMul: tune.bristleMul,
};
}
const WETNESS = [
{ id: 'jiao', name: '焦墨', alpha: 0.96, dry: 0.85, bleed: 0, spread: 0 },
{ id: 'nong', name: '浓墨', alpha: 0.84, dry: 0.3, bleed: 0.15, spread: 0.08 },
{ id: 'zhong', name: '重墨', alpha: 0.68, dry: 0.15, bleed: 0.3, spread: 0.15 },
{ id: 'run', name: '润墨', alpha: 0.52, dry: 0.08, bleed: 0.45, spread: 0.22 },
{ id: 'dan', name: '淡墨', alpha: 0.38, dry: 0.04, bleed: 0.6, spread: 0.3 },
{ id: 'qingd', name: '轻墨', alpha: 0.26, dry: 0.02, bleed: 0.75, spread: 0.4 },
{ id: 'qing', name: '清墨', alpha: 0.16, dry: 0, bleed: 0.9, spread: 0.5 },
{ id: 'yan', name: '烟墨', alpha: 0.09, dry: 0, bleed: 1, spread: 0.6 },
];
const COLORS = [
{ id: 'xuanmo', name: '玄墨', hex: '#23211e' },
{ id: 'daiqing', name: '黛青', hex: '#3f4a5c' },
{ id: 'huaqing', name: '花青', hex: '#2f5a70' },
{ id: 'zheshi', name: '赭石', hex: '#8a4f2d' },
{ id: 'zhusha', name: '朱砂', hex: '#a73a2a' },
{ id: 'tenghuang', name: '藤黄', hex: '#ad7e12' },
{ id: 'shilv', name: '石绿', hex: '#43755f' },
{ id: 'jiangzi', name: '绛紫', hex: '#6b3a4e' },
];
const PAPERS = [
{
id: 'yuban', name: '玉版宣', tint: '#f4eedd',
// 细滑皮纸:肌理轻、纤维短,洇散克制
tex: {
wash: 0.07, cloud: 0, grain: 2200, mottle: 180, pulp: 0, gain: 1.55,
fiber: 120, fiberLen: [6, 22], fiberWarm: 0.48,
grainDark: 0.58, vignette: 0.048,
},
fluid: {
bleedMul: 0.68, flowMul: 0.72, absorbMul: 0.78,
permScale: 0.72, permCoarse: 52, permFine: 138,
blockRate: 0.21, depositMul: 0.88,
},
},
{
id: 'fanggu', name: '仿古宣', tint: '#eadfc0',
// 旧纸黄润:纤维可见,洇散适中
tex: {
wash: 0.16, cloud: 0, grain: 5000, mottle: 420, pulp: 58, gain: 1.5,
fiber: 290, fiberLen: [12, 42], fiberWarm: 0.82,
grainDark: 0.74, vignette: 0.1,
},
fluid: {
bleedMul: 1.02, flowMul: 1.05, absorbMul: 1.05,
permScale: 1.05, permCoarse: 38, permFine: 96,
blockRate: 0.15, depositMul: 1.05,
},
},
{
id: 'chajian', name: '茶笺', tint: '#ddcaa9',
// 粗松茶纸:絮团、暗点,吸墨强、洇散大
tex: {
wash: 0.26, cloud: 0, grain: 7600, mottle: 680, pulp: 210, gain: 1.55,
fiber: 360, fiberLen: [14, 48], fiberWarm: 0.9,
grainDark: 0.8, vignette: 0.12,
},
fluid: {
bleedMul: 1.38, flowMul: 1.28, absorbMul: 1.32,
permScale: 1.38, permCoarse: 28, permFine: 72,
blockRate: 0.11, depositMul: 1.22,
},
},
{
id: 'yuebai', name: '月白笺', tint: '#e7ebe5',
// 冷白绢感:云斑轻、纤维少,洇散略柔
tex: {
wash: 0.1, cloud: 20, grain: 1800, mottle: 160, pulp: 0, gain: 1.48,
fiber: 88, fiberLen: [5, 18], fiberWarm: 0.22,
grainDark: 0.48, vignette: 0.045,
},
fluid: {
bleedMul: 0.82, flowMul: 0.86, absorbMul: 0.88,
permScale: 0.82, permCoarse: 46, permFine: 124,
blockRate: 0.17, depositMul: 0.92,
},
},
];
const NAMES = ['浣溪沙', '卜算子', '水调歌头', '临江仙', '念奴娇', '清平乐', '蝶恋花',
'虞美人', '声声慢', '鹧鸪天', '青玉案', '定风波', '如梦令', '长相思',
'空山新雨', '寒江独钓', '溪山行旅', '烟江叠嶂'];
// 预计算 RGB 与各通道吸光系数(颜料越深,吸光越强)
for (const c of COLORS) {
const n = parseInt(c.hex.slice(1), 16);
c.r = n >> 16 & 255; c.g = n >> 8 & 255; c.b = n & 255;
c.kR = (1 - c.r / 255) * 1.7 + 0.05;
c.kG = (1 - c.g / 255) * 1.7 + 0.05;
c.kB = (1 - c.b / 255) * 1.7 + 0.05;
}
const parseTint = hex => {
const n = parseInt(hex.slice(1), 16);
return { r: n >> 16 & 255, g: n >> 8 & 255, b: n & 255 };
};
function paperSeed(id) {
let h = 2166136261 >>> 0;
for (let i = 0; i < id.length; i++) {
h ^= id.charCodeAt(i);
h = Math.imul(h, 16777619);
}
return h >>> 0 || 1;
}
function makePaperRng(id, salt = 0) {
let s = (paperSeed(id) + salt) >>> 0;
const next = () => {
s = (Math.imul(1664525, s) + 1013904223) >>> 0;
return s / 4294967296;
};
return {
next,
range: (a, b) => a + next() * (b - a),
chance: p => next() < p,
};
}
function paperTex(p = state.paper) { return p.tex; }
function paperFluid(p = state.paper) { return p.fluid; }
function tintShade(tint, dr, dg, db, a) {
const t = parseTint(tint);
return `rgba(${clamp(t.r + dr, 0, 255)},${clamp(t.g + dg, 0, 255)},${clamp(t.b + db, 0, 255)},${a})`;
}
// 墨级调制颜料:淡墨低饱和、高明度、融纸 —— 非线性设色(非 色×α 线性)
function pigmentForInk(color, wet, paperTint) {
const t = clamp(wet.alpha, 0.05, 0.98);
const paper = parseTint(paperTint);
const chromatic = color.id !== 'xuanmo';
const pigment = Math.pow(t, chromatic ? 0.62 : 0.8);
const wash = 1 - pigment;
let r = color.r, g = color.g, b = color.b;
if (chromatic) {
const lum = color.r * 0.31 + color.g * 0.55 + color.b * 0.14;
const desatK = clamp(wash * 0.82 + wet.bleed * 0.12, 0, 0.9);
r = lerp(color.r, lum, desatK);
g = lerp(color.g, lum, desatK);
b = lerp(color.b, lum, desatK);
const paperK = Math.pow(wash, 0.55) * 0.85;
r = lerp(r, paper.r, paperK);
g = lerp(g, paper.g, paperK);
b = lerp(b, paper.b, paperK);
const lift = Math.pow(wash, 1.02) * 40;
r = clamp(r + lift, 0, 248);
g = clamp(g + lift, 0, 248);
b = clamp(b + lift * 0.86, 0, 248);
} else {
const paperK = Math.pow(wash, 0.68) * 0.58;
r = lerp(color.r, paper.r, paperK);
g = lerp(color.g, paper.g, paperK);
b = lerp(color.b, paper.b, paperK);
const lift = Math.pow(wash, 1.15) * 20;
r = clamp(r + lift, 0, 242);
g = clamp(g + lift, 0, 242);
b = clamp(b + lift, 0, 242);
}
return { r: r | 0, g: g | 0, b: b | 0, pigment, wash };
}
function pigmentCoeffs(color, wet, paperTint) {
const p = pigmentForInk(color, wet, paperTint);
return {
kR: (1 - p.r / 255) * 1.7 + 0.05,
kG: (1 - p.g / 255) * 1.7 + 0.05,
kB: (1 - p.b / 255) * 1.7 + 0.05,
};
}
const rgbaInk = (c, a) => {
const p = pigmentForInk(c, state.wet, state.paper.tint);
return `rgba(${p.r},${p.g},${p.b},${a})`;
};
const pigmentHex = c => {
const p = pigmentForInk(c, state.wet, state.paper.tint);
return `rgb(${p.r},${p.g},${p.b})`;
};
/* ───────────── 状态与画布 ───────────── */
const state = {
brush: BRUSHES[1],
strokeTaper: 'belly',
cun: 'pima', // 皴法(散锋专用)
sizeByBrush: { gongbi: 'bao', xieyi: 'bao', cunca: 'bao', pomo: 'bao' },
wet: WETNESS[1],
color: COLORS[0],
paper: PAPERS[0],
washing: false,
painting: false,
last: null,
lastT: 0,
vel: 0,
stroke: null,
editingId: null,
unsaved: false,
placing: null,
qMid: null,
holdRefine: true, // 停笔顺线(狼毫 / 羊毫)
pointerClient: null,
};
// 绘制上下文:画布按 DEV 像素,叠加 SS 基础变换,使所有绘制仍用逻辑坐标
function hiCtx(canvas) {
canvas.width = DEV_W; canvas.height = DEV_H;
const ctx = canvas.getContext('2d');
ctx.setTransform(SS, 0, 0, SS, 0, 0);
return ctx;
}
const view = $('#paper');
// 合成上下文:DEV 像素、单位变换,直接 1:1 叠合各 DEV 缓冲
view.width = DEV_W; view.height = DEV_H;
const vctx = view.getContext('2d');
const inkC = document.createElement('canvas');
const ink = hiCtx(inkC);
// 单笔缓冲:一笔先画在这里(笔内不自累积变深),
// 抬笔时整笔按墨级透明度压入墨层 —— 墨色梯度因此真实线性
const strokeC = document.createElement('canvas');
const sctx = hiCtx(strokeC);
let strokePending = false;
// 整笔压墨的透明度;淡彩 RGB 已水化,略抬低墨级透明度以免过薄
function strokeAlpha() {
const { wet, color } = state;
let base = wet.alpha * (1 - wet.bleed * 0.25);
if (color.id !== 'xuanmo' && wet.alpha < 0.72) {
base += Math.pow((0.72 - wet.alpha) / 0.72, 0.85) * 0.2;
}
return clamp(base, 0.04, 0.97);
}
function commitStroke() {
if (!strokePending) return;
// 单笔缓冲与墨层同为 DEV 像素:临时还原单位变换做精确 1:1 叠合,避免重采样发虚
ink.setTransform(1, 0, 0, 1, 0, 0);
ink.globalAlpha = strokeAlpha();
ink.drawImage(strokeC, 0, 0);
ink.globalAlpha = 1;
ink.setTransform(SS, 0, 0, SS, 0, 0);
sctx.clearRect(0, 0, CANVAS_W, CANVAS_H);
strokePending = false;
dirty = true;
}
const paperC = document.createElement('canvas');
const pctx = hiCtx(paperC);
const WASH_PAD = 18, WASH_MAX = 150;
const washTmp = document.createElement('canvas');
washTmp.width = washTmp.height = (WASH_MAX + WASH_PAD) * 2;
const wctx = washTmp.getContext('2d');
const FILTER_OK = typeof wctx.filter === 'string';
let dirty = true;
/* ───────────── 宣纸流体网格 ─────────────
湿墨不再用规整圆形扩散,而是落入一张细胞网格:
水沿"纸纤维渗透率场"向外渗流并携带颜料,
渗透率的随机性形成不规则的指状洇散;
多色颜料在格子里以吸光密度叠加,得到真实的减色混合。 */
const GRID_F = 4; // 1 格 = 4px
const GW = Math.ceil(CANVAS_W / GRID_F); // 250
const GH = Math.ceil(CANVAS_H / GRID_F); // 348
const CELLS = GW * GH;
const water = new Float32Array(CELLS); // 水量
const pigR = new Float32Array(CELLS); // 游离颜料(随水流动)
const pigG = new Float32Array(CELLS);
const pigB = new Float32Array(CELLS);
const fixR = new Float32Array(CELLS); // 固着颜料(已干入纸,不再流动)
const fixG = new Float32Array(CELLS);
const fixB = new Float32Array(CELLS);
const perm = new Float32Array(CELLS); // 纸纤维渗透率
const inkAge = new Float32Array(CELLS); // 墨层最近落墨时刻(秒),水洗随墨龄衰减
const fluidC = document.createElement('canvas'); // 网格渲染小图,放大合成时自带柔化
fluidC.width = GW; fluidC.height = GH;
const fctx = fluidC.getContext('2d');
const fimg = fctx.createImageData(GW, GH);
let fluidDirty = true;
let fluidFlip = false;
let fluidIdle = false; // 纸面无水时跳过渗流扫描,省电省热
// 生成纤维渗透率场:双尺度纹理 + 细随机 + 致密阻滞点(共同产生指状洇散边缘)
function noiseLayer(cw, ch, rng) {
const a = new Float32Array((cw + 1) * (ch + 1));
for (let i = 0; i < a.length; i++) a[i] = rng.next();
return (gx, gy) => {
const fx = gx / GW * cw, fy = gy / GH * ch;
const ix = Math.floor(fx), iy = Math.floor(fy);
const tx = fx - ix, ty = fy - iy;
return lerp(
lerp(a[iy * (cw + 1) + ix], a[iy * (cw + 1) + ix + 1], tx),
lerp(a[(iy + 1) * (cw + 1) + ix], a[(iy + 1) * (cw + 1) + ix + 1], tx), ty);
};
}
function genPerm() {
const pf = paperFluid();
const rng = makePaperRng(state.paper.id, 91);
const chRatio = CANVAS_H / CANVAS_W;
const n1 = noiseLayer(pf.permCoarse, Math.max(4, Math.round(pf.permCoarse * chRatio)), rng);
const n2 = noiseLayer(pf.permFine, Math.max(8, Math.round(pf.permFine * chRatio)), rng);
for (let y = 0; y < GH; y++) {
for (let x = 0; x < GW; x++) {
const v = n1(x, y) * 0.5 + n2(x, y) * 0.5;
let p = (0.06 + v * 0.94) * rng.range(0.2, 1.8);
p = p * p * 1.4 * pf.permScale;
if (rng.chance(pf.blockRate)) p *= 0.05;
perm[y * GW + x] = clamp(p, 0.015, 2.4);
}
}
}
function resetFluid() {
water.fill(0); pigR.fill(0); pigG.fill(0); pigB.fill(0);
fixR.fill(0); fixG.fill(0); fixB.fill(0);
inkAge.fill(0);
fluidDirty = true;
fluidIdle = false;
}
// 墨龄 → 新鲜度:刚落的墨易被水打散,干透的墨咬纸难洗
function inkFreshness(x, y) {
const cx = clamp((x / GRID_F) | 0, 1, GW - 2);
const cy = clamp((y / GRID_F) | 0, 1, GH - 2);
let t = 0;
for (let dy = -1; dy <= 1; dy++) {
for (let dx = -1; dx <= 1; dx++) {
const v = inkAge[(cy + dy) * GW + cx + dx];
if (v > t) t = v;
}
}
if (!t) return 0;
return clamp(1 - (performance.now() / 1000 - t) / 10, 0, 1); // 约 10 秒干透
}
function markInkAge(x, y) {
const cx = (x / GRID_F) | 0, cy = (y / GRID_F) | 0;
if (cx < 0 || cy < 0 || cx >= GW || cy >= GH) return;
inkAge[cy * GW + cx] = performance.now() / 1000;
}
// 落墨入纸:在网格中注入水与颜料
function deposit(x, y, r, strength) {
const { wet, color } = state;
if (wet.bleed <= 0 || strength <= 0) return;
const pf = paperFluid();
const gx = x / GRID_F, gy = y / GRID_F;
const gr = Math.max(1.2, r / GRID_F);
const x0 = Math.max(1, Math.floor(gx - gr)), x1 = Math.min(GW - 2, Math.ceil(gx + gr));
const y0 = Math.max(1, Math.floor(gy - gr)), y1 = Math.min(GH - 2, Math.ceil(gy + gr));
const wAmt = wet.bleed * 0.6 * strength * pf.bleedMul;
const chromatic = color.id !== 'xuanmo';
const coeff = pigmentCoeffs(color, wet, state.paper.tint);
const dTarget = Math.pow(wet.alpha, chromatic ? 0.68 : 0.82) * 1.15 * Math.min(1, strength);
const tR = dTarget * coeff.kR, tG = dTarget * coeff.kG, tB = dTarget * coeff.kB;
for (let cy = y0; cy <= y1; cy++) {
for (let cx = x0; cx <= x1; cx++) {
const dx = cx - gx, dy = cy - gy;
const dist = Math.sqrt(dx * dx + dy * dy) / gr;
if (dist > 1) continue;
const i = cy * GW + cx;
const fall = 1 - dist;
water[i] = Math.min(2.5, water[i] + wAmt * fall * perm[i]);
const rate = 0.16 * fall * (0.35 + 0.65 * Math.min(1, perm[i])) * pf.depositMul;
if (pigR[i] < tR) pigR[i] += (tR - pigR[i]) * rate;
if (pigG[i] < tG) pigG[i] += (tG - pigG[i]) * rate;
if (pigB[i] < tB) pigB[i] += (tB - pigB[i]) * rate;
}
}
fluidDirty = true;
fluidIdle = false;
}
// 每帧渗流:水往低处与纤维疏松处走,颜料随水迁移(略滞后,干后边缘留痕)
function fluidStep() {
if (fluidIdle) return;
const pf = paperFluid();
fluidFlip = !fluidFlip;
let any = false;
for (let yy = 1; yy < GH - 1; yy++) {
const y = fluidFlip ? yy : GH - 1 - yy;
const row = y * GW;
for (let xx = 1; xx < GW - 1; xx++) {
const x = fluidFlip ? xx : GW - 1 - xx;
const i = row + x;
let w = water[i];
if (w < 0.002) continue;
any = true;
const cap = w * 0.15;
let n = i - GW;
for (let k = 0; k < 4; k++) {
const dw = w - water[n];
let f = (dw * 0.1 + w * 0.06) * perm[n] * pf.flowMul;
if (f > 0) {
if (f > cap) f = cap;
const frac = (f / w) * 0.5;
water[n] += f;
const mR = pigR[i] * frac, mG = pigG[i] * frac, mB = pigB[i] * frac;
pigR[i] -= mR; pigR[n] += mR;
pigG[i] -= mG; pigG[n] += mG;
pigB[i] -= mB; pigB[n] += mB;
w -= f;
}
n = k === 0 ? i + 1 : k === 1 ? i + GW : i - 1;
}
const fr = w < 0.08 ? 0.025 * pf.absorbMul : 0.002 * pf.absorbMul;
if (pigR[i] > 0) { const m = pigR[i] * fr; pigR[i] -= m; fixR[i] += m; }
if (pigG[i] > 0) { const m = pigG[i] * fr; pigG[i] -= m; fixG[i] += m; }
if (pigB[i] > 0) { const m = pigB[i] * fr; pigB[i] -= m; fixB[i] += m; }
const evap = 0.0025 * pf.absorbMul;
water[i] = Math.max(0, w * (1 - evap) - 0.0001 * pf.absorbMul);
}
}
if (any) { fluidDirty = true; dirty = true; }
else fluidIdle = true;
}
// 吸光密度 → 透射率(Beer-Lambert),乘法合成即真实减色混色
function renderFluid() {
const d = fimg.data;
for (let i = 0, j = 0; i < CELLS; i++, j += 4) {
d[j] = 255 * Math.exp(-(pigR[i] + fixR[i]));
d[j + 1] = 255 * Math.exp(-(pigG[i] + fixG[i]));
d[j + 2] = 255 * Math.exp(-(pigB[i] + fixB[i]));
d[j + 3] = 255;
}
fctx.putImageData(fimg, 0, 0);
}
// 将墨层与流体层压平为一张含透明度的墨图(存档 / 续画用)
function flattenInk() {
const c = document.createElement('canvas');
c.width = DEV_W; c.height = DEV_H;
const x = c.getContext('2d');
x.drawImage(inkC, 0, 0);
x.globalCompositeOperation = 'multiply';
x.imageSmoothingEnabled = true;
x.drawImage(fluidC, 0, 0, DEV_W, DEV_H);
return c;
}
/* ───────────── 宣纸纹理 ───────────── */
function paintPaper() {
const paper = state.paper;
const tex = paperTex(paper);
const gain = tex.gain ?? 1.45;
const rng = makePaperRng(paper.id, 17);
const tint = paper.tint;
pctx.fillStyle = tint;
pctx.fillRect(0, 0, CANVAS_W, CANVAS_H);
// 底色不均:纸面局部略深/略浅
if (tex.wash > 0) {
for (let i = 0; i < 24; i++) {
const x = rng.range(0, CANVAS_W), y = rng.range(0, CANVAS_H);
const rad = rng.range(90, 320);
const warm = rng.chance(tex.fiberWarm);
const g = pctx.createRadialGradient(x, y, 0, x, y, rad);
if (warm) {
g.addColorStop(0, tintShade(tint, -22, -14, -32, tex.wash * 0.72 * gain));
g.addColorStop(1, tintShade(tint, 0, 0, 0, 0));
} else {
g.addColorStop(0, tintShade(tint, 10, 10, 14, tex.wash * 0.58 * gain));
g.addColorStop(1, tintShade(tint, 0, 0, 0, 0));
}
pctx.fillStyle = g;
pctx.fillRect(x - rad, y - rad, rad * 2, rad * 2);
}
}
// 月白笺:大块柔云
if (tex.cloud > 0) {
for (let i = 0; i < tex.cloud; i++) {
const x = rng.range(0, CANVAS_W), y = rng.range(0, CANVAS_H);
const rad = rng.range(130, 380);
const g = pctx.createRadialGradient(x, y, 0, x, y, rad);
g.addColorStop(0, tintShade(tint, 8, 12, 18, rng.range(0.06, 0.13) * gain));
g.addColorStop(0.55, tintShade(tint, 3, 5, 10, rng.range(0.025, 0.05) * gain));
g.addColorStop(1, tintShade(tint, 0, 0, 0, 0));
pctx.fillStyle = g;
pctx.fillRect(x - rad, y - rad, rad * 2, rad * 2);
}
}
// 纸絮团(茶笺等粗纸)
for (let i = 0; i < tex.pulp; i++) {
const x = rng.range(0, CANVAS_W), y = rng.range(0, CANVAS_H);
const rx = rng.range(5, 18), ry = rng.range(4, 13);
const g = pctx.createRadialGradient(x, y, 0, x, y, Math.max(rx, ry));
g.addColorStop(0, tintShade(tint, -28, -22, -16, rng.range(0.09, 0.2) * gain));
g.addColorStop(0.7, tintShade(tint, -10, -8, -5, rng.range(0.035, 0.08) * gain));
g.addColorStop(1, tintShade(tint, 0, 0, 0, 0));
pctx.fillStyle = g;
pctx.save();
pctx.translate(x, y);
pctx.rotate(rng.range(0, TAU));
pctx.scale(1, ry / rx);
pctx.beginPath();
pctx.arc(0, 0, rx, 0, TAU);
pctx.fill();
pctx.restore();
}
// 中尺度斑驳(介于细颗粒与絮团之间,肉眼更易察觉)
const mottle = tex.mottle ?? 0;
for (let i = 0; i < mottle; i++) {
const x = rng.range(0, CANVAS_W), y = rng.range(0, CANVAS_H);
const s = rng.range(2, 5.5);
const dark = rng.chance(tex.grainDark);
pctx.fillStyle = dark
? tintShade(tint, -48, -40, -30, rng.range(0.035, 0.11) * gain)
: tintShade(tint, 14, 12, 8, rng.range(0.04, 0.12) * gain);
pctx.fillRect(x, y, s, s * rng.range(0.75, 1.15));
}
// 细颗粒肌理
for (let i = 0; i < tex.grain; i++) {
const x = rng.range(0, CANVAS_W), y = rng.range(0, CANVAS_H);
const dark = rng.chance(tex.grainDark);
if (dark) {
pctx.fillStyle = tintShade(tint, -62, -52, -38, rng.range(0.022, 0.085) * gain);
} else {
pctx.fillStyle = tintShade(tint, 20, 18, 14, rng.range(0.03, 0.095) * gain);
}
pctx.fillRect(x, y, rng.range(0.6, 2.8), rng.range(0.6, 2.8));
}
// 纸纤维
pctx.lineCap = 'round';
const [fMin, fMax] = tex.fiberLen;
for (let i = 0; i < tex.fiber; i++) {
const x = rng.range(0, CANVAS_W), y = rng.range(0, CANVAS_H);
const a = rng.range(0, TAU);
const len = rng.range(fMin, fMax);
const warm = rng.chance(tex.fiberWarm);
pctx.strokeStyle = warm
? tintShade(tint, -48, -38, -24, rng.range(0.024, 0.072) * gain)
: tintShade(tint, -22, -18, -28, rng.range(0.018, 0.058) * gain);
pctx.lineWidth = rng.range(0.45, 1.15);
pctx.beginPath();
pctx.moveTo(x, y);
pctx.quadraticCurveTo(
x + Math.cos(a) * len * 0.5 + (rng.next() - 0.5) * 8,
y + Math.sin(a) * len * 0.5 + (rng.next() - 0.5) * 8,
x + Math.cos(a) * len, y + Math.sin(a) * len);
pctx.stroke();
}
// 四边微暗
if (tex.vignette > 0) {
const g = pctx.createRadialGradient(
CANVAS_W / 2, CANVAS_H / 2, Math.min(CANVAS_W, CANVAS_H) * 0.42,
CANVAS_W / 2, CANVAS_H / 2, Math.max(CANVAS_W, CANVAS_H) * 0.75);
g.addColorStop(0, tintShade(tint, -40, -32, -24, 0));
g.addColorStop(1, tintShade(tint, -40, -32, -24, tex.vignette * gain));
pctx.fillStyle = g;
pctx.fillRect(0, 0, CANVAS_W, CANVAS_H);
}
dirty = true;
}
/* ───────────── 笔触引擎 ───────────── */
// 当前皴法(仅散锋返回非空)
function cunMethod() {
if (brushProfile().type !== 'bristle') return null;
return CUN_METHODS.find(c => c.id === state.cun) || CUN_METHODS[0];
}
function strokeTaperApplies() {
const t = brushProfile().type;
return t === 'fine' || t === 'soft';
}
function strokeTaperMul(t) {
let id;
const cm = cunMethod();
if (cm) id = cm.taper; // 散锋:起收锋取自皴法
else if (strokeTaperApplies()) id = state.strokeTaper || 'belly';
else return 1;
t = clamp(t, 0, 1);
if (id === 'out') return lerp(0.96, 0.24, Math.pow(t, 0.82));
if (id === 'in') return lerp(0.24, 0.96, Math.pow(t, 0.82));
if (id === 'even') return 0.82;
const s = Math.sin(Math.PI * t);
return 0.26 + 0.74 * s * s;
}
function strokeStartWidthMul() {
if (cunMethod()) return strokeTaperMul(0) * 0.6;
return strokeTaperApplies() ? strokeTaperMul(0) * 0.55 : 0.35;
}
function strokeTEstTotal(st) {
const d = st.dist || 0;
const brush = brushProfile();
if (!st.estLen) st.estLen = Math.max(brush.size * 18, 48);
if (d > st.estLen * 0.9) st.estLen = d / 0.9;
return st.estLen;
}
function strokeTProg(st, extra = 0) {
const dist = (st.dist || 0) + extra;
return clamp(dist / strokeTEstTotal(st), 0, 1);
}
function finalizeStrokeLength(st) {
if (!st) return;
const d = st.dist || 0;
if (d > 0) st.estLen = d;
}
function strokeWidth(vel) {
const { wet } = state;
const brush = brushProfile();
const base = brush.size * (0.8 + wet.spread * 0.7);
const f = clamp(1.15 - vel * 0.14 * brush.varia, 1 - brush.varia * 0.72, 1.18);
return Math.max(1.2, base * f);
}
// 收笔出锋时的淡出系数(拖尾期间 < 1)
let strokeFade = 1;
// 单个墨点戳印:实心笔腹 + 柔和墨缘,沿运笔方向略拉长成笔锋
// 画入单笔缓冲,墨色深浅由 commitStroke 统一按墨级决定
function stamp(x, y, r, vel, ang = 0) {
const { wet, color } = state;
const brush = brushProfile();
const dry = clamp(wet.dry + vel * 0.05 * brush.varia, 0, 0.95);
const alpha = (brush.type === 'broad' ? 0.78 : 0.88) * strokeFade;
// 枯笔断墨(工笔须线条连贯,不断墨)
if (brush.type !== 'fine' && dry > 0.55 && Math.random() < dry * 0.3) return;
// 墨体:中心实、边缘柔,是"墨"而非颗粒
// 侧锋:墨心偏向一侧,一侧边缘重实、一侧轻虚,笔越粗越明显
const a = alpha * (1 - dry * 0.55);
if (a > 0.003) {
const side = state.stroke ? state.stroke.side : 1;
const sideK = clamp(0.2 + brush.size / 115, 0.22, 1) * side;
const off = r * 0.32 * sideK;
sctx.save();
sctx.translate(x, y);
sctx.rotate(ang);
sctx.scale(1, 0.82);
const g = sctx.createRadialGradient(0, off, 0, 0, off, r);
g.addColorStop(0, rgbaInk(color, a));
g.addColorStop(0.55, rgbaInk(color, a * 0.9));
g.addColorStop(0.82, rgbaInk(color, a * 0.4));
g.addColorStop(1, rgbaInk(color, 0));
sctx.fillStyle = g;
sctx.beginPath();
sctx.arc(0, 0, r, 0, TAU);
sctx.fill();
// 重边:侧锋一侧再压一道淡淡的实边
const ea = a * 0.3 * Math.abs(sideK);
if (ea > 0.004) {
const eg = sctx.createRadialGradient(0, off * 1.9, 0, 0, off * 1.9, r * 0.55);
eg.addColorStop(0, rgbaInk(color, ea));
eg.addColorStop(1, rgbaInk(color, 0));
sctx.fillStyle = eg;
sctx.beginPath();
sctx.arc(0, off * 1.9, r * 0.55, 0, TAU);
sctx.fill();
}
sctx.restore();
}
// 颗粒只在偏枯时显现(飞白与纸纹),湿墨保持流畅墨体
if (dry > 0.18) {
const n = Math.round(r * dry * 5);
sctx.fillStyle = pigmentHex(color);
for (let i = 0; i < n; i++) {
if (Math.random() < dry * 0.5) continue;
const angr = rand(TAU), rr = Math.abs(gauss()) * r;
sctx.globalAlpha = alpha * rand(0.15, 0.5);
sctx.fillRect(x + Math.cos(angr) * rr, y + Math.sin(angr) * rr, rand(0.6, 1.6), rand(0.6, 1.6));
}
sctx.globalAlpha = 1;
}
strokePending = true;
markInkAge(x, y);
// 湿墨入纸:注入流体网格,由渗流形成不规则洇散
// 细笔注水大减——线条须凝练干净,只余隐约洇意
if (wet.bleed > 0) {
const sizeK = brush.type === 'fine' ? 0.22 : brush.type === 'soft' ? 0.5 : 1;
deposit(x + gauss() * r * 0.4, y + gauss() * r * 0.4, r * (0.8 + Math.random() * 0.5),
(1 - dry) * (brush.type === 'broad' ? 1.4 : 1) * sizeK * brush.depositMul * strokeFade);
}
}
// 散锋:一束分叉的笔毫(皴法重塑簇散开度、簇/毫数、毫宽与起手断毫)
function makeBristles() {
const { bristleMul } = brushProfile();
const cm = cunMethod();
const spread = cm ? cm.spread : 0.72;
const clumpMul = cm ? cm.clumpMul : 1;
const hairMul = cm ? cm.hairMul : 1;
const [hw0, hw1] = cm ? cm.hairW : [0.5, 1.6];
const gapRate = cm ? cm.gap : 0.12;
const arr = [];
const clumps = Math.max(2, Math.round((3 + (Math.random() * 2 | 0)) * bristleMul * clumpMul));
for (let c = 0; c < clumps; c++) {
const center = ((c + 0.5) / clumps * 2 - 1) * spread + gauss() * 0.08;
const hairs = Math.max(1, Math.round((2 + (Math.random() * 3 | 0)) * Math.sqrt(bristleMul) * hairMul));
for (let h = 0; h < hairs; h++) {
arr.push({
off: clamp(center + gauss() * 0.07, -0.92, 0.92),
w: rand(hw0, hw1),
a: rand(0.35, 1),
gap: Math.random() < gapRate,
});
}
}
return arr;
}
function drawBristles(p0, p1, w) {
const { wet, color } = state;
const dx = p1.x - p0.x, dy = p1.y - p0.y;
const len = Math.hypot(dx, dy) || 1;
const nx = -dy / len, ny = dx / len;
const segAng = Math.atan2(dy, dx);
const cm = cunMethod();
const skipRate = cm ? cm.skip : 0.16;
const sideK = cm ? cm.side : 0.55;
const dab = cm ? cm.dab : 0;
const bodyR = cm ? cm.bodyR : 1;
const scatter = cm && cm.scatter ? cm.scatter : 0;
const grain = cm && cm.grain ? cm.grain : 0;
const ph = pigmentHex(color);
// 笔腹墨体:散锋中心一层墨,让笔毫连成"一笔"而非毛刷;卷云加大半径并叠层成圆浑厚体
const midx = (p0.x + p1.x) / 2, midy = (p0.y + p1.y) / 2;
const ba = (cm ? cm.body : 0.14) * strokeFade;
if (ba > 0.004) {
const br = w * 0.5 * bodyR;
const bg = sctx.createRadialGradient(midx, midy, 0, midx, midy, br);
bg.addColorStop(0, rgbaInk(color, ba));
bg.addColorStop(0.6, rgbaInk(color, ba * 0.5));
bg.addColorStop(1, rgbaInk(color, 0));
sctx.fillStyle = bg;
sctx.beginPath();
sctx.arc(midx, midy, br, 0, TAU);
sctx.fill();
if (bodyR > 1.15) { // 卷云:内核打散成数团,墨体圆浑而不呆板
sctx.fillStyle = ph;
for (let k = 0; k < 3; k++) {
sctx.globalAlpha = ba * rand(0.7, 1.1) * strokeFade;
sctx.beginPath();
sctx.arc(midx + gauss() * br * 0.35, midy + gauss() * br * 0.35,
br * rand(0.28, 0.46), 0, TAU);
sctx.fill();
}
sctx.globalAlpha = 1;
}
// 卷云:颗粒墨斑 + 飞白破孔,破除平滑感,做出云块肌理
if (grain) {
const gN = Math.round(br * 1.3 * grain);
for (let i = 0; i < gN; i++) {
const a = Math.random() * TAU, rr = Math.sqrt(Math.random()) * br * 0.95;
const gx = midx + Math.cos(a) * rr, gy = midy + Math.sin(a) * rr;
if (Math.random() < 0.62) {
sctx.globalAlpha = clamp(ba * rand(0.5, 1.6) * strokeFade, 0, 1);
sctx.fillStyle = ph;
sctx.beginPath();
sctx.arc(gx, gy, rand(0.5, 1.9), 0, TAU);
sctx.fill();
} else {
sctx.globalCompositeOperation = 'destination-out';
sctx.globalAlpha = rand(0.18, 0.55);
sctx.beginPath();
sctx.arc(gx, gy, rand(0.7, 2.2), 0, TAU);
sctx.fill();
sctx.globalCompositeOperation = 'source-over';
}
}
sctx.globalAlpha = 1;
}
}
const side = state.stroke.side || 1;
sctx.lineCap = 'round';
sctx.strokeStyle = ph;
sctx.fillStyle = ph;
for (const b of state.stroke.bristles) {
if (Math.random() < 0.03) b.gap = !b.gap; // 笔毫起落
if (b.gap) continue;
if (Math.random() < skipRate) continue; // 干擦的断续残痕
const off = b.off * w * 0.5;
const jx = gauss() * 0.5, jy = gauss() * 0.5;
// 侧锋:一侧的笔毫吃墨重、另一侧轻擦
const sideW = clamp(1 + b.off * side * sideK, 0.3, 1.7);
if (dab) {
// 雨点皴:粗短圆点,松散层叠成豆瓣点簇,而非连续毫线
const u = Math.random();
const exN = off + gauss() * w * 0.42 * (1 + scatter); // 法向散开
const exT = gauss() * w * 0.34 * scatter; // 切向散开
const cx = lerp(p0.x, p1.x, u) + nx * exN + (dx / len) * exT + jx;
const cy = lerp(p0.y, p1.y, u) + ny * exN + (dy / len) * exT + jy;
const dlen = b.w * (0.55 + w * 0.045) * rand(0.6, 1.3); // 点的长度
const dwid = b.w * (0.45 + w * 0.045) * rand(0.6, 1.2); // 点的宽度
sctx.globalAlpha = clamp(0.78 * b.a * sideW * rand(0.65, 1.05) * strokeFade, 0, 1);
sctx.save();
sctx.translate(cx, cy);
sctx.rotate(segAng + gauss() * 0.5);
sctx.beginPath();
sctx.ellipse(0, 0, dlen, dwid, 0, 0, TAU);
sctx.fill();
sctx.restore();
sctx.globalAlpha = 1;
continue;
}
sctx.globalAlpha = 0.8 * b.a * sideW * rand(0.6, 1) * strokeFade;
sctx.lineWidth = b.w * (0.55 + w * 0.04) * (0.8 + sideW * 0.25);
sctx.beginPath();
sctx.moveTo(p0.x + nx * off + jx, p0.y + ny * off + jy);
sctx.lineTo(p1.x + nx * off + jx, p1.y + ny * off + jy);
sctx.stroke();
// 毫边碎墨:沿笔毫拖出细碎颗粒,皴擦的粗粝肌理
if (Math.random() < 0.5) {
sctx.globalAlpha = 0.5 * b.a * sideW * strokeFade;
sctx.fillRect(midx + nx * off + gauss() * 2.2, midy + ny * off + gauss() * 2.2,
rand(0.5, 1.4), rand(0.5, 1.4));
}
}
sctx.globalAlpha = 1;
strokePending = true;
markInkAge(p1.x, p1.y);
if (wet.bleed > 0) {
const { depositMul } = brushProfile();
deposit(p1.x + gauss() * w * 0.3, p1.y + gauss() * w * 0.3, w * 0.45, 0.6 * depositMul * strokeFade);
}
}
function stampSegment(p0, p1, vel, optT0, optT1) {
const st = state.stroke;
const brush = brushProfile();
const dx = p1.x - p0.x, dy = p1.y - p0.y;
const d = Math.hypot(dx, dy) || 0.01;
const explicit = optT0 !== undefined && optT1 !== undefined;
const segT0 = explicit ? optT0 : strokeTProg(st, 0);
const segT1 = explicit ? optT1 : strokeTProg(st, d);
const tMid = (segT0 + segT1) * 0.5;
const target = strokeWidth(vel) * strokeTaperMul(tMid);
if (st.curW === undefined) st.curW = target;
if (d > 0.5) st.dir = Math.atan2(dy, dx);
const ang = st.dir || 0;
if (brush.type === 'bristle') {
st.curW += (target - st.curW) * 0.3;
drawBristles(p0, p1, st.curW);
} else {
if (brush.type === 'fine') {
sctx.lineCap = 'round';
sctx.strokeStyle = pigmentHex(state.color);
sctx.globalAlpha = 0.65 * strokeFade;
sctx.lineWidth = Math.max(0.8, target * 0.72);
sctx.beginPath();
sctx.moveTo(p0.x, p0.y);
sctx.lineTo(p1.x, p1.y);
sctx.stroke();
sctx.globalAlpha = 1;
strokePending = true;
}
const spacing = Math.max(1.2, st.curW * (brush.type === 'fine' ? 0.16 : 0.22) * brush.spacingMul);
const steps = Math.max(1, Math.ceil(d / spacing));