117 lines
3.9 KiB
JavaScript
117 lines
3.9 KiB
JavaScript
/**
|
|
* @module ol/render/canvas/LineStringBuilder
|
|
*/
|
|
import CanvasInstruction, {strokeInstruction, beginPathInstruction} from './Instruction.js';
|
|
import CanvasBuilder from './Builder.js';
|
|
|
|
class CanvasLineStringBuilder extends CanvasBuilder {
|
|
/**
|
|
* @param {number} tolerance Tolerance.
|
|
* @param {import("../../extent.js").Extent} maxExtent Maximum extent.
|
|
* @param {number} resolution Resolution.
|
|
* @param {number} pixelRatio Pixel ratio.
|
|
*/
|
|
constructor(tolerance, maxExtent, resolution, pixelRatio) {
|
|
super(tolerance, maxExtent, resolution, pixelRatio);
|
|
}
|
|
|
|
/**
|
|
* @param {Array<number>} flatCoordinates Flat coordinates.
|
|
* @param {number} offset Offset.
|
|
* @param {number} end End.
|
|
* @param {number} stride Stride.
|
|
* @private
|
|
* @return {number} end.
|
|
*/
|
|
drawFlatCoordinates_(flatCoordinates, offset, end, stride) {
|
|
const myBegin = this.coordinates.length;
|
|
const myEnd = this.appendFlatCoordinates(
|
|
flatCoordinates, offset, end, stride, false, false);
|
|
const moveToLineToInstruction = [CanvasInstruction.MOVE_TO_LINE_TO, myBegin, myEnd];
|
|
this.instructions.push(moveToLineToInstruction);
|
|
this.hitDetectionInstructions.push(moveToLineToInstruction);
|
|
return end;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
drawLineString(lineStringGeometry, feature) {
|
|
const state = this.state;
|
|
const strokeStyle = state.strokeStyle;
|
|
const lineWidth = state.lineWidth;
|
|
if (strokeStyle === undefined || lineWidth === undefined) {
|
|
return;
|
|
}
|
|
this.updateStrokeStyle(state, this.applyStroke);
|
|
this.beginGeometry(lineStringGeometry, feature);
|
|
this.hitDetectionInstructions.push([
|
|
CanvasInstruction.SET_STROKE_STYLE,
|
|
state.strokeStyle, state.lineWidth, state.lineCap, state.lineJoin,
|
|
state.miterLimit, state.lineDash, state.lineDashOffset
|
|
], beginPathInstruction);
|
|
const flatCoordinates = lineStringGeometry.getFlatCoordinates();
|
|
const stride = lineStringGeometry.getStride();
|
|
this.drawFlatCoordinates_(flatCoordinates, 0, flatCoordinates.length, stride);
|
|
this.hitDetectionInstructions.push(strokeInstruction);
|
|
this.endGeometry(feature);
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
drawMultiLineString(multiLineStringGeometry, feature) {
|
|
const state = this.state;
|
|
const strokeStyle = state.strokeStyle;
|
|
const lineWidth = state.lineWidth;
|
|
if (strokeStyle === undefined || lineWidth === undefined) {
|
|
return;
|
|
}
|
|
this.updateStrokeStyle(state, this.applyStroke);
|
|
this.beginGeometry(multiLineStringGeometry, feature);
|
|
this.hitDetectionInstructions.push([
|
|
CanvasInstruction.SET_STROKE_STYLE,
|
|
state.strokeStyle, state.lineWidth, state.lineCap, state.lineJoin,
|
|
state.miterLimit, state.lineDash, state.lineDashOffset
|
|
], beginPathInstruction);
|
|
const ends = multiLineStringGeometry.getEnds();
|
|
const flatCoordinates = multiLineStringGeometry.getFlatCoordinates();
|
|
const stride = multiLineStringGeometry.getStride();
|
|
let offset = 0;
|
|
for (let i = 0, ii = ends.length; i < ii; ++i) {
|
|
offset = this.drawFlatCoordinates_(flatCoordinates, offset, ends[i], stride);
|
|
}
|
|
this.hitDetectionInstructions.push(strokeInstruction);
|
|
this.endGeometry(feature);
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
finish() {
|
|
const state = this.state;
|
|
if (state.lastStroke != undefined && state.lastStroke != this.coordinates.length) {
|
|
this.instructions.push(strokeInstruction);
|
|
}
|
|
this.reverseHitDetectionInstructions();
|
|
this.state = null;
|
|
return super.finish();
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc.
|
|
*/
|
|
applyStroke(state) {
|
|
if (state.lastStroke != undefined && state.lastStroke != this.coordinates.length) {
|
|
this.instructions.push(strokeInstruction);
|
|
state.lastStroke = this.coordinates.length;
|
|
}
|
|
state.lastStroke = 0;
|
|
super.applyStroke(state);
|
|
this.instructions.push(beginPathInstruction);
|
|
}
|
|
}
|
|
|
|
|
|
export default CanvasLineStringBuilder;
|