class CustomIcon {
constructor(
props = {
symbol: null,
fillColor: null,
strokeColor: null,
strokeWidth: null,
borderColor: null,
borderWidth: null,
borderStyle: null,
label: null,
texture: null,
flip: null,
flag: null,
size: null,
customSize:null,
}
) {
this.size = props.size ? props.size : 120;
this.flag = props.flag ? props.flag : { show: false };
this.flip = props.flip ? props.flip : false;
this.texture = props.texture ? props.texture : { show: false };
this.symbol = props.symbol ? props.symbol : null;
this.borderStyle = props.borderStyle ? props.borderStyle : "solid";
this.fillColor = props.fillColor
? props.fillColor
: {
face: "red",
hair: "green",
shirt: "blue",
background: "purple",
border: "none"
};
this.strokeColor = props.strokeColor
? props.strokeColor
: {
face: "red",
hair: "green",
shirt: "blue",
background: "purple",
border: "black"
};
this.strokeWidth = props.strokeWidth
? props.strokeWidth
: {
face: "0px",
hair: "0px",
shirt: "0px",
background: "0px",
border: "0px"
};
this.borderColor = props.borderColor ? props.borderColor : "none";
this.borderWidth = props.borderWidth ? props.borderWidth : "none";
this.label = props.label ? props.label : "label";
}
get render() {
let uid = Math.random().toString(36).substr(2, 5);
let label = this.label;
let fillColor = this.fillColor;
let strokeColor = this.strokeColor;
let strokeWidth = this.strokeWidth;
let borderWidth = this.borderWidth;
let borderStyle = this.borderStyle;
let borderColor = this.borderColor;
let texture = this.texture;
let flip = this.flip;
let flag = this.flag;
let size = this.size;
let customSize = this.customSize
let iso = "";
if (flag.show) {
iso = flag.iso;
}
let symbol = this.symbol;
let svg;
let dy = 0;
var stringToHTML = function (str) {
var dom = document.createElementNS("http://www.w3.org/2000/svg", "svg");
dom.innerHTML = str;
return dom;
};
symbol = d3.select(stringToHTML(symbol)).attr("id", `symbol-${uid}`);
svg = DOM.svg(size, size);
let g;
if (flip) {
g = d3
.select(svg)
.style("box-sizing", "content-box")
.style("transform", "scaleX(-1)")
//.style("border", `${borderWidth} ${borderStyle} ${borderColor}`)
.attr("id", uid);
} else {
g = d3
.select(svg)
.style("box-sizing", "content-box")
//.style("border", `${borderWidth} ${borderStyle} ${borderColor}`)
.attr("id", uid);
}
g.append("svg")
.attr("class", `target-${uid}`)
.attr("width", customSize)
.attr("height", size)
.attr("viewBox", `0 0 ${size} ${size}`)
.html(symbol.node().outerHTML);
if (texture.show) {
for (let k of Object.keys(texture)) {
if (
k !== "show" &&
["background", "hair", "shirt", "face"].indexOf(k) > -1
) {
g.call(texture[k]);
g.select(`#texture-${k}`).style("fill", texture[k].url());
}
}
}
if (flag != null) {
if (flip) {
g.select("#icon")
.insert("image", "#texture-background")
.style("opacity", flag.opacity ? flag.opacity : "1")
.style("transform", "scaleX(-1)")
.style("transform-origin", "bottom")
.attr("href", flagLink[flag.iso])
.attr("width", size)
.attr("height", size);
} else {
g.select("#icon")
.insert("image", "#texture-background")
.style("opacity", flag.opacity ? flag.opacity : "1")
.attr("href", flagLink[flag.iso])
.attr("width", size)
.attr("height", size);
}
}
g.select("#face").style("fill", fillColor.face);
g.select("#hair").style("fill", fillColor.hair);
g.select("#shirt").style("fill", fillColor.shirt);
g.select("#background").style("fill", fillColor.background);
g.select("#border").style("fill", fillColor.border);
g.select("#face").style("stroke", strokeColor.face);
g.select("#hair").style("stroke", strokeColor.hair);
g.select("#shirt").style("stroke", strokeColor.shirt);
g.select("#background").style("stroke", strokeColor.background);
g.select("#border").style("stroke", strokeColor.border);
g.select("#face").style("stroke-width", strokeWidth.face);
g.select("#hair").style("stroke-width", strokeWidth.hair);
g.select("#shirt").style("stroke-width", strokeWidth.shirt);
g.select("#background").style("stroke-width", strokeWidth.background);
g.select("#border").style("stroke-width", strokeWidth.border);
return g.node();
}
}