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
| import Feature from 'ol/Feature.js'; import Map from 'ol/Map.js'; import Point from 'ol/geom/Point.js'; import VectorLayer from 'ol/layer/Vector.js'; import VectorSource from 'ol/source/Vector.js'; import View from 'ol/View.js'; import {Fill, RegularShape, Stroke, Style} from 'ol/style.js';
const stroke = new Stroke({color: 'black', width: 2}); const fill = new Fill({color: 'red'});
const styles = { 'square': new Style({ image: new RegularShape({ fill: fill, stroke: stroke, points: 4, radius: 10, angle: Math.PI / 4, }), }), 'rectangle': new Style({ image: new RegularShape({ fill: fill, stroke: stroke, radius: 10 / Math.SQRT2, radius2: 10, points: 4, angle: 0, scale: [1, 0.5], }), }), 'triangle': new Style({ image: new RegularShape({ fill: fill, stroke: stroke, points: 3, radius: 10, rotation: Math.PI / 4, angle: 0, }), }), 'star': new Style({ image: new RegularShape({ fill: fill, stroke: stroke, points: 5, radius: 10, radius2: 4, angle: 0, }), }), 'cross': new Style({ image: new RegularShape({ fill: fill, stroke: stroke, points: 4, radius: 10, radius2: 0, angle: 0, }), }), 'x': new Style({ image: new RegularShape({ fill: fill, stroke: stroke, points: 4, radius: 10, radius2: 0, angle: Math.PI / 4, }), }), 'stacked': [ new Style({ image: new RegularShape({ fill: fill, stroke: stroke, points: 4, radius: 5, angle: Math.PI / 4, displacement: [0, 10], }), }), new Style({ image: new RegularShape({ fill: fill, stroke: stroke, points: 4, radius: 10, angle: Math.PI / 4, }), }), ], };
const styleKeys = [ 'x', 'cross', 'star', 'triangle', 'square', 'rectangle', 'stacked', ]; const count = 250; const features = new Array(count); const e = 4500000; for (let i = 0; i < count; ++i) { const coordinates = [2 * e * Math.random() - e, 2 * e * Math.random() - e]; features[i] = new Feature(new Point(coordinates)); features[i].setStyle( styles[styleKeys[Math.floor(Math.random() * styleKeys.length)]] ); }
const source = new VectorSource({ features: features, });
const vectorLayer = new VectorLayer({ source: source, });
const map = new Map({ layers: [vectorLayer], target: 'map', view: new View({ center: [0, 0], zoom: 2, }), });
const colors = ['blue', 'green', 'yellow', 'aqua', 'red']; let currentColor = 0;
document.getElementById('color-changer').addEventListener('click', function () { styles.square .getImage() .setFill(new Fill({color: colors[currentColor % colors.length]})); vectorLayer.changed(); currentColor++; });
|