@guardian/interactive-component-library 0.3.2 → 0.4.0
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/components/molecules/canvas-map/lib/interpolators/interpolateStyles.js +1 -1
- package/dist/components/molecules/canvas-map/lib/layers/TextLayer.js +1 -1
- package/dist/components/molecules/canvas-map/lib/layers/VectorLayer.js +1 -1
- package/dist/components/molecules/canvas-map/lib/renderers/FeatureRenderer.js +1 -2
- package/dist/components/molecules/canvas-map/lib/styles/Fill.d.ts +29 -3
- package/dist/components/molecules/canvas-map/lib/styles/Fill.js +22 -0
- package/dist/components/molecules/canvas-map/lib/styles/HashPattern.d.ts +38 -0
- package/dist/components/molecules/canvas-map/lib/styles/HashPattern.js +72 -0
- package/dist/components/molecules/canvas-map/lib/styles/Style.d.ts +1 -1
- package/dist/components/molecules/canvas-map/lib/styles/index.d.ts +2 -1
- package/dist/index.js +3 -1
- package/package.json +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Style } from "../styles/Style.js";
|
|
2
1
|
import { Stroke } from "../styles/Stroke.js";
|
|
3
2
|
import { Fill } from "../styles/Fill.js";
|
|
3
|
+
import { Style } from "../styles/Style.js";
|
|
4
4
|
function interpolateStyles(firstStyle, secondStyle, interpolateColors, interpolateNumbers) {
|
|
5
5
|
const fillInterpolator = interpolateFill(
|
|
6
6
|
firstStyle.fill,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { TextLayerRenderer } from "../renderers/TextLayerRenderer.js";
|
|
2
|
-
import { Style } from "../styles/Style.js";
|
|
3
2
|
import { Text } from "../styles/Text.js";
|
|
3
|
+
import { Style } from "../styles/Style.js";
|
|
4
4
|
import { Dispatcher } from "../events/Dispatcher.js";
|
|
5
5
|
import { combineExtents } from "../util/extent.js";
|
|
6
6
|
import { MapEvent } from "../events/MapEvent.js";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { VectorLayerRenderer } from "../renderers/VectorLayerRenderer.js";
|
|
2
|
-
import { Style } from "../styles/Style.js";
|
|
3
2
|
import { Stroke } from "../styles/Stroke.js";
|
|
3
|
+
import { Style } from "../styles/Style.js";
|
|
4
4
|
import { combineExtents } from "../util/extent.js";
|
|
5
5
|
import { Dispatcher } from "../events/Dispatcher.js";
|
|
6
6
|
import { MapEvent } from "../events/MapEvent.js";
|
|
@@ -1,7 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @class Fill
|
|
3
|
+
* @description Represents a fill style for a feature
|
|
4
|
+
* @property {string} color - The color of the fill
|
|
5
|
+
* @property {number} opacity - The opacity of the fill
|
|
6
|
+
* @property {HashPattern} pattern - The pattern of the fill
|
|
7
|
+
*/
|
|
1
8
|
export class Fill {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
9
|
+
/**
|
|
10
|
+
* @constructor
|
|
11
|
+
* @description Creates a new instance of the Fill class
|
|
12
|
+
* @param {Object} [options] - The options for the fill
|
|
13
|
+
* @param {string} [options.color="#CCC"] - The color of the fill
|
|
14
|
+
* @param {number} [options.opacity=1] - The opacity of the fill
|
|
15
|
+
* @param {Object} [options.pattern=null] - The pattern of the fill
|
|
16
|
+
*/
|
|
17
|
+
constructor(options?: {
|
|
18
|
+
color?: string;
|
|
19
|
+
opacity?: number;
|
|
20
|
+
pattern?: any;
|
|
21
|
+
});
|
|
22
|
+
color: string;
|
|
23
|
+
opacity: number;
|
|
24
|
+
pattern: any;
|
|
5
25
|
_getRgba: (...arg0: any[]) => string;
|
|
26
|
+
/**
|
|
27
|
+
* @function getRgba
|
|
28
|
+
* @description Returns the fill color as an RGBA string
|
|
29
|
+
* @returns {string} The fill color with opacity applied
|
|
30
|
+
*/
|
|
6
31
|
getRgba(): string;
|
|
32
|
+
drawInContext(context: any, scale: any): void;
|
|
7
33
|
}
|
|
@@ -1,14 +1,36 @@
|
|
|
1
1
|
import { memoise } from "../util/memoise.js";
|
|
2
2
|
import { toRgba } from "../util/toRgba.js";
|
|
3
3
|
class Fill {
|
|
4
|
+
/**
|
|
5
|
+
* @constructor
|
|
6
|
+
* @description Creates a new instance of the Fill class
|
|
7
|
+
* @param {Object} [options] - The options for the fill
|
|
8
|
+
* @param {string} [options.color="#CCC"] - The color of the fill
|
|
9
|
+
* @param {number} [options.opacity=1] - The opacity of the fill
|
|
10
|
+
* @param {Object} [options.pattern=null] - The pattern of the fill
|
|
11
|
+
*/
|
|
4
12
|
constructor(options) {
|
|
5
13
|
this.color = (options == null ? void 0 : options.color) || "#CCC";
|
|
6
14
|
this.opacity = (options == null ? void 0 : options.opacity) || 1;
|
|
15
|
+
this.pattern = (options == null ? void 0 : options.pattern) || null;
|
|
7
16
|
this._getRgba = memoise(toRgba);
|
|
8
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* @function getRgba
|
|
20
|
+
* @description Returns the fill color as an RGBA string
|
|
21
|
+
* @returns {string} The fill color with opacity applied
|
|
22
|
+
*/
|
|
9
23
|
getRgba() {
|
|
10
24
|
return this._getRgba(this.color, this.opacity);
|
|
11
25
|
}
|
|
26
|
+
drawInContext(context, scale) {
|
|
27
|
+
if (this.pattern) {
|
|
28
|
+
context.fillStyle = this.pattern.createPatternInContext(context, scale);
|
|
29
|
+
} else {
|
|
30
|
+
context.fillStyle = this.getRgba();
|
|
31
|
+
}
|
|
32
|
+
context.fill();
|
|
33
|
+
}
|
|
12
34
|
}
|
|
13
35
|
export {
|
|
14
36
|
Fill
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HashPattern creates a diagonally-striped pattern used to fill a canvas element.
|
|
3
|
+
*
|
|
4
|
+
* Use `createPatternInContext` to create a pattern that can be used as a `fillStyle`.
|
|
5
|
+
*/
|
|
6
|
+
export class HashPattern {
|
|
7
|
+
/**
|
|
8
|
+
* @param {Object} options
|
|
9
|
+
* @param {number} [options.stripeWidth=4] - width of the pattern's stripes
|
|
10
|
+
* @param {number} [options.gapWidth =10] - width of the gap between the stripes
|
|
11
|
+
* @param {string} [options.stripeColor="#eee"]
|
|
12
|
+
* @param {string} [options.gapColor="#777"]
|
|
13
|
+
*/
|
|
14
|
+
constructor({ stripeWidth, gapWidth, gapColor, stripeColor, }: {
|
|
15
|
+
stripeWidth?: number;
|
|
16
|
+
gapWidth?: number;
|
|
17
|
+
stripeColor?: string;
|
|
18
|
+
gapColor?: string;
|
|
19
|
+
});
|
|
20
|
+
stripeWidth: number;
|
|
21
|
+
stripeColor: string;
|
|
22
|
+
gapwidth: number;
|
|
23
|
+
gapColor: string;
|
|
24
|
+
tileSize: number;
|
|
25
|
+
offscreenCanvas: HTMLCanvasElement;
|
|
26
|
+
lastDrawnScale: any;
|
|
27
|
+
lastDrawnPattern: CanvasPattern;
|
|
28
|
+
/**
|
|
29
|
+
* @param {CanvasRenderingContext2D} ctx
|
|
30
|
+
* @param {number} scale
|
|
31
|
+
*/
|
|
32
|
+
createPatternInContext(ctx: CanvasRenderingContext2D, scale: number): CanvasPattern;
|
|
33
|
+
/**
|
|
34
|
+
* @param {CanvasRenderingContext2D} ctx
|
|
35
|
+
* @param {number} scale
|
|
36
|
+
*/
|
|
37
|
+
_createPattern(ctx: CanvasRenderingContext2D, scale: number): CanvasPattern;
|
|
38
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
class HashPattern {
|
|
2
|
+
/**
|
|
3
|
+
* @param {Object} options
|
|
4
|
+
* @param {number} [options.stripeWidth=4] - width of the pattern's stripes
|
|
5
|
+
* @param {number} [options.gapWidth =10] - width of the gap between the stripes
|
|
6
|
+
* @param {string} [options.stripeColor="#eee"]
|
|
7
|
+
* @param {string} [options.gapColor="#777"]
|
|
8
|
+
*/
|
|
9
|
+
constructor({
|
|
10
|
+
stripeWidth = 4,
|
|
11
|
+
gapWidth = 8,
|
|
12
|
+
gapColor = "#eee",
|
|
13
|
+
stripeColor = "#777"
|
|
14
|
+
}) {
|
|
15
|
+
this.stripeWidth = stripeWidth;
|
|
16
|
+
this.stripeColor = stripeColor;
|
|
17
|
+
this.gapwidth = gapWidth;
|
|
18
|
+
this.gapColor = gapColor;
|
|
19
|
+
const hypot = stripeWidth * 2 + gapWidth * 2;
|
|
20
|
+
this.tileSize = Math.round(hypot / Math.SQRT2);
|
|
21
|
+
this.offscreenCanvas = document.createElement("canvas");
|
|
22
|
+
this.lastDrawnScale = null;
|
|
23
|
+
this.lastDrawnPattern = null;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* @param {CanvasRenderingContext2D} ctx
|
|
27
|
+
* @param {number} scale
|
|
28
|
+
*/
|
|
29
|
+
createPatternInContext(ctx, scale) {
|
|
30
|
+
const roundedScale = Math.round(scale);
|
|
31
|
+
if (roundedScale === this.lastDrawnScale) {
|
|
32
|
+
return this.lastDrawnPattern;
|
|
33
|
+
}
|
|
34
|
+
this.lastDrawnPattern = this._createPattern(ctx, roundedScale);
|
|
35
|
+
this.lastDrawnScale = roundedScale;
|
|
36
|
+
return this.lastDrawnPattern;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* @param {CanvasRenderingContext2D} ctx
|
|
40
|
+
* @param {number} scale
|
|
41
|
+
*/
|
|
42
|
+
_createPattern(ctx, scale) {
|
|
43
|
+
const size = this.tileSize * scale;
|
|
44
|
+
this.offscreenCanvas.width = size;
|
|
45
|
+
this.offscreenCanvas.height = size;
|
|
46
|
+
const offCtx = this.offscreenCanvas.getContext("2d");
|
|
47
|
+
if (this.gapColor) {
|
|
48
|
+
offCtx.fillStyle = this.gapColor;
|
|
49
|
+
offCtx.fillRect(0, 0, size, size);
|
|
50
|
+
}
|
|
51
|
+
const lineWidth = this.stripeWidth * scale;
|
|
52
|
+
offCtx.strokeStyle = this.stripeColor;
|
|
53
|
+
offCtx.lineWidth = lineWidth;
|
|
54
|
+
offCtx.lineCap = "square";
|
|
55
|
+
offCtx.beginPath();
|
|
56
|
+
offCtx.moveTo(-1, 1);
|
|
57
|
+
offCtx.lineTo(1, -1);
|
|
58
|
+
offCtx.moveTo(0, size);
|
|
59
|
+
offCtx.lineTo(size, 0);
|
|
60
|
+
offCtx.moveTo(size - 1, size + 1);
|
|
61
|
+
offCtx.lineTo(size + 1, size - 1);
|
|
62
|
+
offCtx.stroke();
|
|
63
|
+
const pattern = ctx.createPattern(this.offscreenCanvas, "repeat");
|
|
64
|
+
pattern.setTransform(
|
|
65
|
+
new DOMMatrix().scale(1 / Math.round(scale), 1 / Math.round(scale))
|
|
66
|
+
);
|
|
67
|
+
return pattern;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
export {
|
|
71
|
+
HashPattern
|
|
72
|
+
};
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* @class
|
|
9
9
|
* @property {Object} properties - The properties of the style
|
|
10
10
|
* @property {Object} properties.stroke - The stroke color of the style
|
|
11
|
-
* @property {
|
|
11
|
+
* @property {Fill} properties.fill - The fill color of the style
|
|
12
12
|
* @property {Object} properties.text - The text color of the style
|
|
13
13
|
*/
|
|
14
14
|
export class Style {
|
package/dist/index.js
CHANGED
|
@@ -42,10 +42,11 @@ import { MapEvent } from "./components/molecules/canvas-map/lib/events/MapEvent.
|
|
|
42
42
|
import { Projection } from "./components/molecules/canvas-map/lib/projection/index.js";
|
|
43
43
|
import { TextLayer } from "./components/molecules/canvas-map/lib/layers/TextLayer.js";
|
|
44
44
|
import { VectorLayer } from "./components/molecules/canvas-map/lib/layers/VectorLayer.js";
|
|
45
|
-
import { Style } from "./components/molecules/canvas-map/lib/styles/Style.js";
|
|
46
45
|
import { Stroke, StrokePosition } from "./components/molecules/canvas-map/lib/styles/Stroke.js";
|
|
46
|
+
import { HashPattern } from "./components/molecules/canvas-map/lib/styles/HashPattern.js";
|
|
47
47
|
import { Fill } from "./components/molecules/canvas-map/lib/styles/Fill.js";
|
|
48
48
|
import { Text } from "./components/molecules/canvas-map/lib/styles/Text.js";
|
|
49
|
+
import { Style } from "./components/molecules/canvas-map/lib/styles/Style.js";
|
|
49
50
|
import { GeoCoordinate } from "./components/molecules/canvas-map/lib/util/coordinate.js";
|
|
50
51
|
import { GeoBounds } from "./components/molecules/canvas-map/lib/util/bounds.js";
|
|
51
52
|
import { Extent, combineExtents, containsCoordinate, containsXY, extentForCoordinates } from "./components/molecules/canvas-map/lib/util/extent.js";
|
|
@@ -89,6 +90,7 @@ export {
|
|
|
89
90
|
GeoJSON,
|
|
90
91
|
GradientIcon,
|
|
91
92
|
GridType,
|
|
93
|
+
HashPattern,
|
|
92
94
|
InfoButton,
|
|
93
95
|
LabelOverlapConfig,
|
|
94
96
|
LabelType,
|