graphein 0.16.0 → 0.16.2

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.
package/dist/index.d.ts CHANGED
@@ -354,6 +354,14 @@ declare class RoughPen {
354
354
  private readonly rng;
355
355
  constructor(ctx: RoughContext, style?: Partial<RoughStyle>);
356
356
  private resolve;
357
+ /**
358
+ * Run `draw` with `factor` folded into the *ambient* `globalAlpha`, then restore
359
+ * it. Callers honoring a cross-highlight set `ctx.globalAlpha` below 1 to dim a
360
+ * non-selected mark; composing (rather than overwriting and resetting to 1) keeps
361
+ * that dim while still applying the pen's own fill/stroke translucency, so sketch
362
+ * marks dim during a highlight exactly like their solid-rendered counterparts.
363
+ */
364
+ private withAlpha;
357
365
  /** A symmetric random offset in [-x, x], scaled by a length-based gain. */
358
366
  private off;
359
367
  /**
package/dist/index.js CHANGED
@@ -219,35 +219,43 @@ function fixedDuration(interval) {
219
219
  }
220
220
  }
221
221
  function floorFixed(value, interval) {
222
- const duration = fixedDuration(interval);
223
- if (interval.unit !== "week") {
224
- return Math.floor(value / duration) * duration;
225
- }
226
222
  const date = new Date(value);
227
- const midnight = Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate());
228
- const day = new Date(midnight).getUTCDay();
229
- const weekStart = midnight - day * DAY;
230
- return weekStart - Math.floor((weekStart % duration + duration) % duration);
223
+ if (interval.unit === "day") {
224
+ return new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime();
225
+ }
226
+ if (interval.unit === "week") {
227
+ const day = date.getDay();
228
+ return new Date(date.getFullYear(), date.getMonth(), date.getDate() - day).getTime();
229
+ }
230
+ const duration = fixedDuration(interval);
231
+ return Math.floor(value / duration) * duration;
231
232
  }
232
233
  function addFixed(value, interval) {
234
+ const date = new Date(value);
235
+ if (interval.unit === "day") {
236
+ return new Date(date.getFullYear(), date.getMonth(), date.getDate() + interval.step).getTime();
237
+ }
238
+ if (interval.unit === "week") {
239
+ return new Date(date.getFullYear(), date.getMonth(), date.getDate() + 7 * interval.step).getTime();
240
+ }
233
241
  return value + fixedDuration(interval);
234
242
  }
235
243
  function floorMonth(value, step) {
236
244
  const date = new Date(value);
237
- const month = Math.floor(date.getUTCMonth() / step) * step;
238
- return Date.UTC(date.getUTCFullYear(), month, 1);
245
+ const month = Math.floor(date.getMonth() / step) * step;
246
+ return new Date(date.getFullYear(), month, 1).getTime();
239
247
  }
240
248
  function floorYear(value, step) {
241
249
  const date = new Date(value);
242
- const year = Math.floor(date.getUTCFullYear() / step) * step;
243
- return Date.UTC(year, 0, 1);
250
+ const year = Math.floor(date.getFullYear() / step) * step;
251
+ return new Date(year, 0, 1).getTime();
244
252
  }
245
253
  function addCalendar(value, interval) {
246
254
  const date = new Date(value);
247
255
  if (interval.unit === "month") {
248
- return Date.UTC(date.getUTCFullYear(), date.getUTCMonth() + interval.step, 1);
256
+ return new Date(date.getFullYear(), date.getMonth() + interval.step, 1).getTime();
249
257
  }
250
- return Date.UTC(date.getUTCFullYear() + interval.step, 0, 1);
258
+ return new Date(date.getFullYear() + interval.step, 0, 1).getTime();
251
259
  }
252
260
  function floorTime(value, interval) {
253
261
  if (interval.unit === "month") {
@@ -295,17 +303,17 @@ function timeTickFormat(values) {
295
303
  const span = ordered.length > 1 ? Math.abs(ordered[1] - ordered[0]) : 0;
296
304
  return (time) => {
297
305
  const date = new Date(time);
298
- const year = date.getUTCFullYear();
299
- const month = MONTH_NAMES[date.getUTCMonth()];
300
- const day = date.getUTCDate();
301
- const hours = pad2(date.getUTCHours());
302
- const minutes = pad2(date.getUTCMinutes());
303
- const seconds = pad2(date.getUTCSeconds());
306
+ const year = date.getFullYear();
307
+ const month = MONTH_NAMES[date.getMonth()];
308
+ const day = date.getDate();
309
+ const hours = pad2(date.getHours());
310
+ const minutes = pad2(date.getMinutes());
311
+ const seconds = pad2(date.getSeconds());
304
312
  if (span >= YEAR * 0.8) {
305
313
  return String(year);
306
314
  }
307
315
  if (span >= MONTH * 0.8) {
308
- return ordered.some((tick) => new Date(tick).getUTCFullYear() !== year) ? `${month} ${year}` : month;
316
+ return ordered.some((tick) => new Date(tick).getFullYear() !== year) ? `${month} ${year}` : month;
309
317
  }
310
318
  if (span >= DAY * 0.8) {
311
319
  return `${month} ${day}`;
@@ -1291,6 +1299,20 @@ var RoughPen = class {
1291
1299
  resolve(opts) {
1292
1300
  return { ...this.base, ...opts };
1293
1301
  }
1302
+ /**
1303
+ * Run `draw` with `factor` folded into the *ambient* `globalAlpha`, then restore
1304
+ * it. Callers honoring a cross-highlight set `ctx.globalAlpha` below 1 to dim a
1305
+ * non-selected mark; composing (rather than overwriting and resetting to 1) keeps
1306
+ * that dim while still applying the pen's own fill/stroke translucency, so sketch
1307
+ * marks dim during a highlight exactly like their solid-rendered counterparts.
1308
+ */
1309
+ withAlpha(factor, draw) {
1310
+ const ctx = this.ctx;
1311
+ const prev = ctx.globalAlpha;
1312
+ ctx.globalAlpha = prev * factor;
1313
+ draw();
1314
+ ctx.globalAlpha = prev;
1315
+ }
1294
1316
  /** A symmetric random offset in [-x, x], scaled by a length-based gain. */
1295
1317
  off(x, gain) {
1296
1318
  return gain * (this.rng() * 2 * x - x);
@@ -1375,9 +1397,7 @@ var RoughPen = class {
1375
1397
  ctx.lineCap = "round";
1376
1398
  ctx.lineJoin = "round";
1377
1399
  ctx.strokeStyle = m.stroke ?? "#000";
1378
- ctx.globalAlpha = m.strokeAlpha ?? 1;
1379
- ctx.stroke();
1380
- ctx.globalAlpha = 1;
1400
+ this.withAlpha(m.strokeAlpha ?? 1, () => ctx.stroke());
1381
1401
  }
1382
1402
  /** Fill a closed polygon with hachure / cross-hatch / solid. */
1383
1403
  fillPolygon(points, m) {
@@ -1388,9 +1408,7 @@ var RoughPen = class {
1388
1408
  this.tracePolyline(points, { ...m, roughness: m.roughness * 0.6 }, true);
1389
1409
  ctx.closePath();
1390
1410
  ctx.fillStyle = m.fill;
1391
- ctx.globalAlpha = m.fillAlpha ?? 1;
1392
- ctx.fill();
1393
- ctx.globalAlpha = 1;
1411
+ this.withAlpha(m.fillAlpha ?? 1, () => ctx.fill());
1394
1412
  return;
1395
1413
  }
1396
1414
  const gap = m.hachureGap > 0 ? m.hachureGap : Math.max(3, m.strokeWidth * 3.2);
@@ -1406,9 +1424,7 @@ var RoughPen = class {
1406
1424
  ctx.lineWidth = weight;
1407
1425
  ctx.lineCap = "round";
1408
1426
  ctx.strokeStyle = m.fill;
1409
- ctx.globalAlpha = m.fillAlpha ?? 0.85;
1410
- ctx.stroke();
1411
- ctx.globalAlpha = 1;
1427
+ this.withAlpha(m.fillAlpha ?? 0.85, () => ctx.stroke());
1412
1428
  }
1413
1429
  /** Hand-drawn open polyline (axis rules, box-plot whiskers, short features). */
1414
1430
  polyline(points, opts = {}) {
@@ -1482,9 +1498,7 @@ var RoughPen = class {
1482
1498
  ctx.beginPath();
1483
1499
  this.traceSmoothClosed(pts);
1484
1500
  ctx.fillStyle = m.fill;
1485
- ctx.globalAlpha = m.fillAlpha ?? 1;
1486
- ctx.fill();
1487
- ctx.globalAlpha = 1;
1501
+ this.withAlpha(m.fillAlpha ?? 1, () => ctx.fill());
1488
1502
  }
1489
1503
  if (m.stroke) {
1490
1504
  ctx.beginPath();