|
|
|
/* -------------------------- */ |
|
/* planetsPlugin */ |
|
/* -------------------------- */ |
|
(function (global, factory) { |
|
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : |
|
typeof define === 'function' && define.amd ? define(['exports'], factory) : |
|
(factory((global.planetsPlugin = global.planetsPlugin || {}))); |
|
}(this, function (exports) { 'use strict'; |
|
|
|
var planetsPlugin = function planetsPlugin(__mapper = {}) { |
|
let config = {} |
|
config.width = 600 |
|
config.height = 400 |
|
config.startX = 125 |
|
config.startY = 175 |
|
config.step = 10 |
|
config.rotrx = 40 // x rotation radius |
|
config.rotry = 20 // y rotation radius |
|
|
|
let planet = {} |
|
planet.count = 3000 |
|
planet.LineWidth = 1 |
|
planet.fill = "black" |
|
planet.stroke = "black" |
|
planet.radius = 2 |
|
planet.dotrace = 1 |
|
planet.trace = [] |
|
|
|
planet.items = d3.range(planet.count).map(function(d, i) { |
|
let x0 = config.startX + config.step * Math.random() |
|
let y0 = config.startY + config.step * Math.random() |
|
let periodtime = 20000 |
|
let toffset = Math.random() * periodtime |
|
|
|
let solarItems = __mapper("solars").items() |
|
let solarid = null // i % solarItems.length |
|
let solarbit = null // Math.floor(i / solarItems.length) |
|
|
|
let xoffset = null // Math.random() * 10 // solarItems[solarid].x // phase |
|
let yoffset = null // Math.random() * 10 // solarItems[solarid].y // offset |
|
|
|
let rotrx = config.rotrx // x rotation radius |
|
let rotry = config.rotry // y rotation radius |
|
|
|
let formparams = { |
|
"m1":-144,"m2":4,"n1":2,"n2":2,"n3":2,"a":1,"b":1, |
|
"tx":300, |
|
"ty":200, |
|
"rot":-0, |
|
"rad":1, |
|
"segs":3, |
|
"pta": 0, |
|
"ptb": -1, |
|
"cf":522, "cs":222, |
|
"v0":0,"v1":-1, |
|
} |
|
|
|
return { |
|
radius: formparams.rad, |
|
x0: x0, |
|
y0: y0, |
|
x: x0, |
|
y: y0, |
|
vx: 1, // * (0.5 - Math.random()), // 0.1, |
|
vy: 1 * (0.5 - Math.random()), // 0.5, |
|
trace:[], |
|
pslinks: [], // ref to solars |
|
type: 'planet', |
|
color: planet.fill, |
|
idx: i, |
|
key: i, |
|
id: i, |
|
rotrx: rotrx, |
|
rotry: rotry, |
|
rtype: "ellipse", |
|
solarid: solarid, |
|
solarbit: solarbit, |
|
xoffset: xoffset, |
|
yoffset: yoffset, |
|
toffset: toffset, |
|
periodtime: periodtime, |
|
starttime: undefined, |
|
elapsed: 0, |
|
formparams: formparams, |
|
}}) |
|
|
|
|
|
var planetsPlugin = function planetsPlugin() {} |
|
|
|
planetsPlugin.items = function items(_) { |
|
return planet.items |
|
} |
|
|
|
planetsPlugin.updcoords = function coords(i=0, _elapsed=0) { |
|
|
|
let p = planet.items[i] |
|
let solarItems = __mapper("solars").items() |
|
|
|
if (p.solarid !== null) { |
|
let s = solarItems[p.solarid] |
|
|
|
let x_solar = solarItems[p.solarid].x |
|
let y_solar = solarItems[p.solarid].y |
|
|
|
let t_xoffset = p.xoffset |
|
let t_yoffset = p.yoffset |
|
|
|
let rotation_radius_x = p.rotrx * (1 + p.solarbit ) |
|
let rotation_radius_y = p.rotry * (1 + p.solarbit) |
|
|
|
if (p.starttime == undefined) p.starttime = _elapsed - p.toffset |
|
|
|
p.elapsed = _elapsed - p.starttime |
|
|
|
let t_angle = (2 * Math.PI) * p.elapsed / p.periodtime // ang = 2Pi * t / T |
|
|
|
let t_x = rotation_radius_x * Math.cos(t_angle) |
|
let t_y = rotation_radius_y * Math.sin(t_angle) |
|
|
|
let x = x_solar + t_x |
|
let y = y_solar + t_y |
|
|
|
x = s.points[p.angidx][0] |
|
y = s.points[p.angidx][1] |
|
|
|
p.trace[p.trace.length] = [x,y] |
|
|
|
p.x = x // upd planet coords |
|
p.y = y |
|
|
|
return [x, y] |
|
|
|
} |
|
} |
|
|
|
planetsPlugin.render = function render(_) { |
|
|
|
var color = d3.interpolatePlasma |
|
__mapper({'xsf': xsfPlugin.xsfPlugin(__mapper)}) |
|
|
|
var itemsIds = (d, i) => d.idx |
|
|
|
|
|
var planetItems = __mapper("planets").items() |
|
var planetsLayer = __mapper("svg")({cls: 'planets', item: 'path.planetPath', data: planetItems, idfn: itemsIds}) |
|
|
|
planetsLayer.m |
|
.attr("d", function(d) { |
|
let p = Object.assign(d.formparams, { |
|
"tx":d.x, |
|
"ty":d.y |
|
}) |
|
let pts = (__mapper('xsf')(p).tfpoints()) |
|
return d3.line()(pts) |
|
}) |
|
.style("fill", planet.fill) |
|
.style("fill-opacity", 1) |
|
.style("stroke", planet.stroke) |
|
.style("stroke-width", 0.01) |
|
|
|
planetsLayer.x |
|
.remove() |
|
} |
|
|
|
|
|
return planetsPlugin |
|
|
|
} |
|
|
|
exports.planetsPlugin = planetsPlugin |
|
|
|
})); |