I want to draw a rectangle over a static image (OpenLayers jpg, not a map). My Main issue is with Stimulus.
How do I set up the drawing which is probably a click and drag, but could be a click-click.
Rails Stimulus HTML
<% doc_image_width= @year.doc.image.metadata['width'] %>
<% [email protected]['height'] %>
<% docUrl = rails_blob_url(@year.doc.image) %>
<div id="snmap"
data-controller="snippet-draw"
data-snippet-draw-target="snmap"
data-snippet-draw-url-value="<%= docUrl %>"
data-snippet-draw-width-value="<%= doc_image_width %>"
data-snippet-draw-height-value="<%= doc_image_height %>"
data-action="click->snippet-draw#getSnippetCoordinates"
style="width: <%= doc_image_height %>px; height: <%= doc_image_height %>px">
</div>
app/javascript/controllers/snippet_draw_controller.js.
I don’t know how to approach this. Shouldn’t be repeating things in two methods. When I click on the image, the image is selected instead of drawing. Maybe point me to some posts or blogs about the actions
import { Controller } from "@hotwired/stimulus"
import Map from 'ol/Map';
<import other ol (openlayers)>
export default class extends Controller {
static targets = [ "snmap", "snippetCoords"]
static values = {
url: String,
width: Number,
height: Number,
}
connect() {
let source = new VectorSource();
const docURL = this.urlValue;
const docWidth = this.widthValue;
const docHeight = this.heightValue;
const extent = [0, 0, docWidth, docHeight];
let projection = new Projection({ units: 'pixels', extent: extent});
let docSource = new Static({ url: docURL, projection: projection, imageExtent: extent });
let docLayer = new ImageLayer({ source: docSource, extent: extent});
let map = new Map({
layers: [docLayer],
target: snmap,
view: staticView,
extent: extent,
});
}
getSnippetCoordinates() {
<repeat of everything in connect() up to map>
let map = new Map({
layers: [docLayer, drawLayer],
target: snmap,
view: staticView,
extent: extent,
});
let drawLayer = new VectorLayer({
source: source
});
draw.on('drawend', function(event) {
let boxCoords = event.feature.getGeometry().getCoordinates();
let boxCoordsStringified = JSON.stringify(boxCoords);
let boxCoordsStringifiedParsed = JSON.parse(boxCoordsStringified);
return this.snippetCoordsTarget.boxCoordsStringified
});
}