@xenra/dsh-draworld 0.1.40 → 0.1.46

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (6) hide show
  1. package/README.md +86 -12
  2. package/client.js +437 -97
  3. package/index.js +1026 -71
  4. package/package.json +1 -1
  5. package/pixelops.js +1288 -0
  6. package/pixels.js +182 -19
package/pixels.js CHANGED
@@ -783,7 +783,7 @@ export function posterizeFills(imageData, w, h, opts = {}) {
783
783
  const k = Math.max(2, Math.min(12, Math.round(Number(opts.colors) || 8)));
784
784
  const minArea = Math.max(4, Number(opts.minArea) || Math.round((w * h) / 4000));
785
785
  const tolerance = Math.max(0.5, Number(opts.tolerance) || 1.2);
786
- const maxFills = Math.max(1, Number(opts.maxFills) || 1600);
786
+ const maxFills = Math.max(1, Number(opts.maxFills) || 1600);
787
787
  const thinKeep = Math.max(0, Number(opts.thinKeep) || 30);
788
788
  const palette = kmeansColors(imageData, k, Math.max(1, Math.round(Number(opts.sampleStep) || 3)));
789
789
  const pal = palette.map((p) => p.rgb);
@@ -1070,36 +1070,199 @@ export function blitImage(surf, img, x, y, w, h) {
1070
1070
  }
1071
1071
  }
1072
1072
 
1073
+ // ---------------------------------------------------------------- v3 层缓冲合成
1074
+ // 层内所有笔迹按 normal 叠进本层 buffer,层间按 layer.blend 合成一次。
1075
+ // 这样同一层 20 笔 multiply 只压暗一次,而不是叠 20 次。
1076
+
1077
+ function blendChannel(mode, cb, cs) {
1078
+ switch (mode) {
1079
+ case "multiply": return cb * cs;
1080
+ case "screen": return cb + cs - cb * cs;
1081
+ case "overlay": return cb <= 0.5 ? 2 * cb * cs : 1 - 2 * (1 - cb) * (1 - cs);
1082
+ case "soft-light": {
1083
+ if (cs <= 0.5) return cb - (1 - 2 * cs) * cb * (1 - cb);
1084
+ const d = cb <= 0.25 ? ((16 * cb - 12) * cb + 4) * cb : Math.sqrt(cb);
1085
+ return cb + (2 * cs - 1) * (d - cb);
1086
+ }
1087
+ case "linear-dodge": return Math.min(1, cb + cs);
1088
+ default: return cs;
1089
+ }
1090
+ }
1091
+
1092
+ function compositeSurface(dst, src, opacity, mode = "normal") {
1093
+ const lo = Math.max(0, Math.min(1, Number(opacity) === undefined ? 1 : Number(opacity)));
1094
+ if (lo <= 0) return;
1095
+ const dd = dst.data;
1096
+ const sd = src.data;
1097
+ for (let i = 0; i < sd.length; i += 4) {
1098
+ const as = (sd[i + 3] / 255) * lo;
1099
+ if (as <= 0) continue;
1100
+ const ab = dd[i + 3] / 255;
1101
+ const ao = as + ab * (1 - as);
1102
+ if (ao <= 0) continue;
1103
+ for (let c = 0; c < 3; c += 1) {
1104
+ const cs = sd[i + c] / 255;
1105
+ const cb = dd[i + c] / 255;
1106
+ const b = blendChannel(mode, cb, cs);
1107
+ const co = as * (1 - ab) * cs + as * ab * b + (1 - as) * ab * cb;
1108
+ dd[i + c] = Math.round((co / ao) * 255);
1109
+ }
1110
+ dd[i + 3] = Math.round(ao * 255);
1111
+ }
1112
+ }
1113
+
1114
+ /** 用剪贴目标的 alpha 掩掉本层。 */
1115
+ function clipSurface(surf, mask) {
1116
+ const d = surf.data;
1117
+ const m = mask.data;
1118
+ for (let i = 0; i < d.length; i += 4) {
1119
+ d[i + 3] = Math.round((d[i + 3] * m[i + 3]) / 255);
1120
+ }
1121
+ }
1122
+
1123
+ /** 让本层给墨线让位:alpha *= (1 - 线稿 alpha)。铺底色块不盖住下面的线。 */
1124
+ function avoidSurface(surf, mask) {
1125
+ const d = surf.data;
1126
+ const m = mask.data;
1127
+ for (let i = 0; i < d.length; i += 4) {
1128
+ d[i + 3] = Math.round((d[i + 3] * (255 - m[i + 3])) / 255);
1129
+ }
1130
+ }
1131
+
1132
+ function hueRotate(surf, degrees) {
1133
+ if (!degrees) return;
1134
+ const rad = (Number(degrees) * Math.PI) / 180;
1135
+ const cos = Math.cos(rad);
1136
+ const sin = Math.sin(rad);
1137
+ const m = [
1138
+ 0.213 + cos * 0.787 - sin * 0.213, 0.715 - cos * 0.715 - sin * 0.715, 0.072 - cos * 0.072 + sin * 0.928,
1139
+ 0.213 - cos * 0.213 + sin * 0.143, 0.715 + cos * 0.285 + sin * 0.14, 0.072 - cos * 0.072 - sin * 0.283,
1140
+ 0.213 - cos * 0.213 - sin * 0.787, 0.715 - cos * 0.715 + sin * 0.715, 0.072 + cos * 0.928 + sin * 0.072,
1141
+ ];
1142
+ const d = surf.data;
1143
+ for (let i = 0; i < d.length; i += 4) {
1144
+ if (d[i + 3] === 0) continue;
1145
+ const r = d[i];
1146
+ const g = d[i + 1];
1147
+ const b = d[i + 2];
1148
+ d[i] = Math.max(0, Math.min(255, Math.round(r * m[0] + g * m[1] + b * m[2])));
1149
+ d[i + 1] = Math.max(0, Math.min(255, Math.round(r * m[3] + g * m[4] + b * m[5])));
1150
+ d[i + 2] = Math.max(0, Math.min(255, Math.round(r * m[6] + g * m[7] + b * m[8])));
1151
+ }
1152
+ }
1153
+
1154
+ /** 调色层:亮度/对比/饱和/色相,作用于它下面的合成结果。 */
1155
+ function applyGrade(surf, grade = {}) {
1156
+ const br = Math.max(-1, Math.min(1, Number(grade.brightness) || 0)) * 255;
1157
+ const ct = Math.max(0, Math.min(3, Number(grade.contrast) === undefined ? 1 : Number(grade.contrast)));
1158
+ const sat = Math.max(0, Math.min(3, Number(grade.saturation) === undefined ? 1 : Number(grade.saturation)));
1159
+ const d = surf.data;
1160
+ for (let i = 0; i < d.length; i += 4) {
1161
+ if (d[i + 3] === 0) continue;
1162
+ for (let c = 0; c < 3; c += 1) {
1163
+ let v = d[i + c];
1164
+ v = 128 + (v - 128) * ct + br;
1165
+ const lum = 0.299 * d[i] + 0.587 * d[i + 1] + 0.114 * d[i + 2];
1166
+ v = lum + (v - lum) * sat;
1167
+ d[i + c] = Math.max(0, Math.min(255, Math.round(v)));
1168
+ }
1169
+ }
1170
+ hueRotate(surf, Number(grade.hue) || 0);
1171
+ }
1172
+
1173
+ async function drawLayerSurface(surf, layer, opts = {}) {
1174
+ const alphaScale = opts.alphaScale === undefined ? 1 : opts.alphaScale;
1175
+ if (layer.image) {
1176
+ if (opts.withImages === false) return surf;
1177
+ const img = await loadImage(opts.resolve(layer.image.file));
1178
+ blitImage(surf, img, layer.image.x || 0, layer.image.y || 0, layer.image.w || img.width, layer.image.h || img.height);
1179
+ return surf;
1180
+ }
1181
+ for (const s of layer.strokes || []) {
1182
+ const rgb = parseColor(s.color);
1183
+ const so = s.opacity === undefined ? 1 : Math.max(0, Math.min(1, Number(s.opacity)));
1184
+ const alpha = alphaScale * so;
1185
+ if (alpha <= 0) continue;
1186
+ if (s.mode === "fill") {
1187
+ fillPolygon(surf, s.points || [], s.holes || [], rgb, alpha);
1188
+ } else {
1189
+ const size = Number(s.size) || 8;
1190
+ strokeCapsule(surf, s.points || [], size / 2, rgb, alpha);
1191
+ }
1192
+ }
1193
+ return surf;
1194
+ }
1195
+
1073
1196
  /**
1074
- * 文档 → 位图。opts.withImages=false 时不画图片层,opts.onlyImage 只画某张图片层。
1197
+ * 文档 → 位图。v3:每组/层按 role 顺序(doc.layers 已排好),
1198
+ * blend≠normal / clipTo / hue≠0 的层先画进独立 buffer,再整层合成一次。
1075
1199
  */
1076
1200
  export async function rasterDoc(doc, opts = {}) {
1077
1201
  const w = doc.width || 1024;
1078
1202
  const h = doc.height || 1024;
1079
- const surf = makeSurface(w, h);
1203
+ const out = makeSurface(w, h);
1080
1204
  const withImages = opts.withImages !== false;
1205
+ const groups = new Map((doc.groups || []).map((g) => [g.id, g]));
1206
+ const maskCache = new Map();
1207
+ const layerById = new Map((doc.layers || []).map((l) => [l.id, l]));
1208
+ const lineLayer = (doc.layers || []).find((l) => l.role === "line" && l.visible !== false && !l.image);
1209
+ const maskOf = async (id) => {
1210
+ if (maskCache.has(id)) return maskCache.get(id);
1211
+ let mask = null;
1212
+ if (typeof id === "string" && (id.startsWith("role:") || id.startsWith("group:"))) {
1213
+ const isRole = id.startsWith("role:");
1214
+ const key = id.slice(isRole ? 5 : 6);
1215
+ const targets = (doc.layers || []).filter((l) => l.visible !== false
1216
+ && (isRole ? l.role === key : l.groupId === key));
1217
+ if (targets.length > 0) {
1218
+ mask = makeSurface(w, h);
1219
+ for (const target of targets) {
1220
+ await drawLayerSurface(mask, { ...target, clipTo: null, hue: 0, opacity: 1 }, { resolve: opts.resolve, withImages, alphaScale: 1 });
1221
+ }
1222
+ }
1223
+ } else {
1224
+ const target = layerById.get(id);
1225
+ if (target) {
1226
+ mask = makeSurface(w, h);
1227
+ await drawLayerSurface(mask, { ...target, blend: "normal", clipTo: null, hue: 0, opacity: 1 }, { resolve: opts.resolve, withImages, alphaScale: 1 });
1228
+ }
1229
+ }
1230
+ maskCache.set(id, mask);
1231
+ return mask;
1232
+ };
1081
1233
  for (const layer of doc.layers || []) {
1082
1234
  if (layer.visible === false) continue;
1083
- const lo = layer.opacity === undefined ? 1 : Math.max(0, Math.min(1, Number(layer.opacity)));
1084
- if (layer.image) {
1085
- if (!withImages) continue;
1086
- const img = await loadImage(opts.resolve(layer.image.file));
1087
- blitImage(surf, img, layer.image.x || 0, layer.image.y || 0, layer.image.w || img.width, layer.image.h || img.height);
1235
+ const group = layer.groupId ? groups.get(layer.groupId) : null;
1236
+ if (group && group.visible === false) continue;
1237
+ const groupOpacity = group ? Math.max(0, Math.min(1, Number(group.opacity) === undefined ? 1 : Number(group.opacity))) : 1;
1238
+ const lo = Math.max(0, Math.min(1, Number(layer.opacity) === undefined ? 1 : Number(layer.opacity)));
1239
+ const opacity = lo * groupOpacity;
1240
+ if (opacity <= 0) continue;
1241
+ if (layer.role === "grade" && layer.grade) {
1242
+ applyGrade(out, layer.grade);
1088
1243
  continue;
1089
1244
  }
1090
- for (const s of layer.strokes || []) {
1091
- const rgb = parseColor(s.color);
1092
- const so = s.opacity === undefined ? 1 : Math.max(0, Math.min(1, Number(s.opacity)));
1093
- const alpha = lo * so;
1094
- if (s.mode === "fill") {
1095
- fillPolygon(surf, s.points || [], s.holes || [], rgb, alpha);
1096
- } else {
1097
- const size = Number(s.size) || 8;
1098
- strokeCapsule(surf, s.points || [], size / 2, rgb, alpha);
1099
- }
1245
+ const special = (layer.blend && layer.blend !== "normal") || layer.clipTo || layer.hue || (layer.image && opacity < 1) || (layer.role === "base" && lineLayer);
1246
+ if (!special) {
1247
+ await drawLayerSurface(out, layer, { resolve: opts.resolve, withImages, alphaScale: opacity });
1248
+ continue;
1249
+ }
1250
+ const buf = makeSurface(w, h);
1251
+ await drawLayerSurface(buf, layer, { resolve: opts.resolve, withImages, alphaScale: 1 });
1252
+ if (layer.hue) hueRotate(buf, layer.hue);
1253
+ if (layer.clipTo) {
1254
+ const mask = await maskOf(layer.clipTo);
1255
+ if (mask) clipSurface(buf, mask);
1256
+ else buf.data.fill(0);
1257
+ }
1258
+ // 铺底给线稿让位:否则 line 在下面会被色块整片盖住
1259
+ if (layer.role === "base" && lineLayer && layer.id !== lineLayer.id) {
1260
+ const lineMask = await maskOf(lineLayer.id);
1261
+ if (lineMask) avoidSurface(buf, lineMask);
1100
1262
  }
1263
+ compositeSurface(out, buf, opacity, layer.blend || "normal");
1101
1264
  }
1102
- return surf;
1265
+ return out;
1103
1266
  }
1104
1267
 
1105
1268
  export function cropSurface(surf, rect) {