Skip to content

Instantly share code, notes, and snippets.

@JoeCrash
Last active December 27, 2020 22:43
Show Gist options
  • Save JoeCrash/cf3f6912cf26bc041b1f0d519d1a0d27 to your computer and use it in GitHub Desktop.
Save JoeCrash/cf3f6912cf26bc041b1f0d519d1a0d27 to your computer and use it in GitHub Desktop.
PIXI.Graphics.prototype.drawDashLine = function(toX, toY, dashPct = 7, gapPct = 3) {
const lastPosition = this.currentPath.points;
let ax = lastPosition[lastPosition.length - 2] || 0;
let ay = lastPosition[lastPosition.length - 1] || 0;
let pctAt = dashPct;
while(pctAt < 101){
let p = getCoordsAlongPath([ay,ax,toY,toX], pctAt);
let pd = getCoordsAlongPath([ay,ax,toY,toX], pctAt + gapPct);
this.lineTo(p.lng, p.lat);
this.moveTo(pd.lng, pd.lat);
pctAt += dash + gap;
}
if(pctAt >= 100){
let d = getCoordsAlongPath([ay,ax,toY,toX], 95);
this.moveTo(d.lng, d.lat);
this.lineTo(toX, toY);
}
let getCoordsAlongPath = function(pathRef, pct) {
const latLngs = pathRef._latlngs || pathRef;
const _pct = pct * 0.01;
let ax,ay,bx,by;
if(!pathRef._latlngs && Array.isArray(pathRef)){
ax = latLngs[1]; ay = latLngs[0]; bx = latLngs[3]; by = latLngs[2];
}else{
ax = latLngs[0].lng; ay = latLngs[0].lat; bx = latLngs[1].lng; by = latLngs[1].lat;
}
const xDist = ax > bx ? ax - bx : bx - ax;
const yDist = ay > by ? ay - by : by - ay;
const xFinal = ax > bx ? ax - (xDist * _pct) : ax + (xDist * _pct);
const yFinal = ay > by ? ay - (yDist * _pct) : ay + (yDist * _pct);
return xy(xFinal, yFinal);
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment