Copy /* Initialization function to run after engine load (Module.postRun) */
function init() {
Module.Start(window.innerWidth, window.innerHeight);
// Set camera location
Module.getViewCamera().setLocation(new Module.JSVector3D(129.128265, 35.171834, 1000.0));
// Create a symbol for icon management
GLOBAL.Symbol = Module.getSymbol();
// Create an output POI layer for analysis
var layerList = new Module.JSLayerList(true);
GLOBAL.POILayer = layerList.createLayer("MEASURE_POI", Module.ELT_3DPOINT);
GLOBAL.POILayer.setMaxDistance(20000.0);
GLOBAL.POILayer.setSelectable(false);
GLOBAL.WallLayer = layerList.createLayer("MEASURE_WALL", Module.ELT_POLYHEDRON);
GLOBAL.WallLayer.setMaxDistance(20000.0);
GLOBAL.WallLayer.setSelectable(false);
GLOBAL.WallLayer.setEditable(true);
initEvent(Module.canvas);
}
var GLOBAL = {
Symbol: null, // Icon management symbol object
POILayer: null, // POI storage layer
WallLayer: 0, // POI, Icon creation index
};
/* Event setup */
function initEvent(canvas) {
// Radius event setup
canvas.addEventListener("Fire_EventAddRadius", function (e) {
if (e.dTotalDistance > 0) {
// Delete POI object
clearIcon();
// Create radius POI object
createPOI(new Module.JSVector3D(e.dMidLon, e.dMidLat, e.dMidAlt), "rgba(255, 204, 198, 0.8)", e.dTotalDistance, true);
}
});
}
/* Change mouse state */
function setMouseState(_option) {
// Set mouse mode
Module.XDSetMouseState(_option);
}
/* Create POI for output analysis */
function createPOI(_position, _color, _value, _balloonType) {
// Create a canvas to draw the POI icon image
var drawCanvas = document.createElement("canvas");
drawCanvas.width = 100;
drawCanvas.height = 100;
// Return icon image data
var imageData = drawIcon(drawCanvas, _color, _value, _balloonType);
// Register icon image in symbol
if (GLOBAL.Symbol.insertIcon("Icon", imageData, drawCanvas.width, drawCanvas.height)) {
// Return registered icon object
var icon = GLOBAL.Symbol.getIcon("Icon");
// Create JSPoint object
var poi = Module.createPoint("POI");
poi.setPosition(_position); // Set location
poi.setIcon(icon); // Set icon
// Add object to layer
GLOBAL.POILayer.addObject(poi, 0);
}
}
/* Return icon image data */
function drawIcon(_canvas, _color, _value, _balloonType) {
// Return context and clear background
var ctx = _canvas.getContext("2d"),
width = _canvas.width,
height = _canvas.height;
ctx.clearRect(0, 0, width, height);
// Set background Draw Path then draw text
if (_balloonType) {
drawBalloon(ctx, height * 0.5, width, height, 5, height * 0.25, _color);
setText(ctx, width * 0.5, height * 0.2, _value);
} else {
drawRoundRect(ctx, 0, height * 0.3, width, height * 0.25, 5, _color);
setText(ctx, width * 0.5, height * 0.5, _value);
}
return ctx.getImageData(0, 0, _canvas.width, _canvas.height).data;
}
/* Draw balloon background */
function drawBalloon(ctx, marginBottom, width, height, barWidth, barHeight, color) {
var wCenter = width * 0.5,
hCenter = height * 0.5;
// Set the shape of the balloon Draw Path
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(0, height - barHeight - marginBottom);
ctx.lineTo(wCenter - barWidth, height - barHeight - marginBottom);
ctx.lineTo(wCenter, height - marginBottom);
ctx.lineTo(wCenter + barWidth, height - barHeight - marginBottom);
ctx.lineTo(width, height - barHeight - marginBottom);
ctx.lineTo(width, 0);
ctx.closePath();
// Draw the balloon
ctx.fillStyle = color;
ctx.fill();
}
/* Draw rounded rectangle background */
function drawRoundRect(ctx, x, y, width, height, radius, color) {
if (width < 2 * radius) radius = width * 0.5;
if (height < 2 * radius) radius = height * 0.5;
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.arcTo(x + width, y, x + width, y + height, radius);
ctx.arcTo(x + width, y + height, x, y + height, radius);
ctx.arcTo(x, y + height, x, y, radius);
ctx.arcTo(x, y, x + width, y, radius);
ctx.closePath();
// Draw the rectangle
ctx.fillStyle = color;
ctx.fill();
return ctx;
}
/* Draw text */
function setText(_ctx, _posX, _posY, _value) {
var strText = "";
// Set text string
if (typeof _value == "number") {
strText = setKilloUnit(_value, 0.001, 0);
} else {
strText = _value;
}
// Set text style
_ctx.font = "bold 16px sans-serif";
_ctx.textAlign = "center";
_ctx.fillStyle = "rgb(0, 0, 0)";
// Draw text
_ctx.fillText(strText, _posX, _posY);
}
/* Convert to m/km text */
function setKilloUnit(_text, _meterToKilloRate, _decimalSize) {
if (_decimalSize < 0) {
_decimalSize = 0;
}
if (typeof _text == "number") {
if (_text < 1.0 / (_meterToKilloRate * Math.pow(10, _decimalSize))) {
_text = _text.toFixed(1).toString() + "m";
} else {
_text = (_text * _meterToKilloRate).toFixed(2).toString() + "㎞";
}
}
return _text;
}
/* Clear analysis content */
function clearAnalysis() {
// Clear ongoing analysis content
Module.XDClearCircleMeasurement();
if (GLOBAL.WallLayer == null) {
return;
}
// Delete icon
clearIcon();
// Delete POI object
GLOBAL.WallLayer.removeAll();
}
/* Clear icon */
function clearIcon() {
if (GLOBAL.POILayer == null) {
return;
}
// Delete registered icon list
var icon, poi;
poi = GLOBAL.POILayer.keyAtObject("POI");
if (poi == null) {
return;
}
icon = poi.getIcon();
// Delete POI referencing icon
GLOBAL.POILayer.removeAtKey("POI");
// Delete icon from symbol
GLOBAL.Symbol.deleteIcon(icon.getId());
}