UNPKG

thematic-earth

Version:

HTML-based, whole-Earth thematic maps using locally hosted data layers

2 lines 3.66 kB
/* Copyright (c) 2023 Read Write Tools. Legal use subject to the Thematic Earth Software License Agreement. */ import ProxyBase from'../labels/proxy-base.class.js';import LabelRect from'../labels/label-rect.class.js';import*as LabelGraphics from'../graphics/label-graphics.js';import aver from'../dev/aver.js';import expect from'../dev/expect.js';export default class ProxyPolygon extends ProxyBase{constructor(s,t,i){super(s,t,i),this.possibleSlots=[],this.slotAlignments=[],this.chosenSlot=null,this.chosenAlignment=null,Object.seal(this)}disqualificationCriteria(){aver(null!=this.labelBoundingBox),aver(null!=this.featureBoundingBox),0!=this.featureBoundingBox.isValid()?this.labelBoundingBox.width>this.featureBoundingBox.width&&(this.isDisqualified=!0):this.isDisqualified=!0}possiblePlacements(s){expect(s,'HTMLCanvasElement'),aver(null!=this.feature),aver(null!=this.featureBoundingBox),aver(null!=this.labelBoundingBox);var t=this.featureBoundingBox.top,i=this.featureBoundingBox.height,e=this.labelBoundingBox.height;if(this.possibleSlots=this.feature.getPossibleLabelSlots(s.width,s.height,t,i,e),this.possibleSlots=this.possibleSlots.filter((s=>s.width>this.labelBoundingBox.width)),0!=this.possibleSlots.length){for(let s=0;s<this.possibleSlots.length;s++)this.possibleSlots[s].deltaFromBoundingBox(this.featureBoundingBox,this.labelBoundingBox.width,this.labelBoundingBox.height);for(let s=0;s<this.possibleSlots.length;s++)this.slotAlignments.push(`${s}/left`),this.slotAlignments.push(`${s}/center`),this.slotAlignments.push(`${s}/right`);this.slotAlignments.sort(((s,t)=>{var[i,e]=s.split('/'),[l,o]=t.split('/');return this.possibleSlots[i].getDelta(e)-this.possibleSlots[l].getDelta(o)}))}else this.isDisqualified=!0}collisionAdjustment(s,t){expect(s,'HTMLCanvasElement'),expect(t,'CollisionMatrix');for(let s=0;s<this.slotAlignments.length;s++){var[i,e]=this.slotAlignments[s].split('/'),l=this.possibleSlots[i],o=this.labelBoundingBox.width,a=this.labelBoundingBox.height,n=l.getLabelPosition(o,a,e),h=new LabelRect(n.canvasX,n.canvasY,o,a);if(1==t.reserveLabelLocation(h))return this.chosenSlot=this.possibleSlots[i],void(this.chosenAlignment=e)}this.isDisqualified=!0}drawBoundingBox(s){if('show'==(this.canvasParams['label-debug-alt-slots']??'hide')){aver(null!=this.labelBoundingBox);var t=s.getContext('2d');t.strokeWidth=.5;const o=['#f00','#0f0','#00f','#ff0','#0ff','#f0f'];for(let s=0;s<this.possibleSlots.length;s++){var i=this.possibleSlots[s];t.strokeStyle=o[s%6];var e=this.labelBoundingBox.height,l=i.getFullSlotRect(e);t.strokeRect(l.x,l.y,l.width,l.height)}}}drawLabel(s){if(expect(s,'HTMLCanvasElement'),''!=this.labelText){var t='left',i='top';if('show'==(this.canvasParams['label-debug-alt-slots']??'hide'))for(let n=0;n<this.possibleSlots.length;n++){var e=this.labelBoundingBox.top,l=this.possibleSlots[n].getLabelPosition(this.labelBoundingBox.width,this.labelBoundingBox.height,'left');LabelGraphics.draw(s,this.canvasParams,l.canvasX,l.canvasY+e,this.labelText,t,i);var o=this.possibleSlots[n].getLabelPosition(this.labelBoundingBox.width,this.labelBoundingBox.height,'center');LabelGraphics.draw(s,this.canvasParams,o.canvasX,o.canvasY+e,this.labelText,t,i);var a=this.possibleSlots[n].getLabelPosition(this.labelBoundingBox.width,this.labelBoundingBox.height,'right');LabelGraphics.draw(s,this.canvasParams,a.canvasX,a.canvasY+e,this.labelText,t,i)}else if(null!=this.chosenSlot){var n=this.chosenSlot.getLabelPosition(this.labelBoundingBox.width,this.labelBoundingBox.height,this.chosenAlignment);e=this.labelBoundingBox.top;LabelGraphics.draw(s,this.canvasParams,n.canvasX,n.canvasY+e,this.labelText,t,i)}}}}