planetarray
. var width = 840, height = 840, center = [width/2, height/2], planetNames = [ "Selene", "Mimas", "Ares", "Enceladus", "Tethys", "Dione", "Zeus", "Rhea", "Titan", "Janus", "Hyperion", "Iapetus" ], planetarray = [];
app
object (container for layers) from a div and 3 layers for the background + sun, orbits, planets and tooltips. // <div id="solarsystem"></div> var app = Graphics2D.app('#solarsystem', width, height), // background = app.layer(0), orbits = app.layer(1), planets = app.layer(2);
background.image('images/sky.png', 0, 0); background.image('images/sun.png', center[0]-50, center[1]-50); // 50,50 --
planetarray
. function Planet(options){ // this.radius = options.radius; this.rotatePerMs = 360 / 100 / options.time; this.time = options.time; this.name = options.name; // this.createOrbit(options); this.createPlanet(options); planetarray.push(this); }
for(var i = 0; i < 12; i++){ new Planet({ image: i, // ( - ) radius: 90 + i * 26, // LibCanvas :) time: 40 + i * 20, name: planetNames[i] }); }
createOrbit
and createPlanet
:) Next.orbits
layer (it is under the layer with the planets). For each orbit, in addition to itself, we draw the outline of the planet (it appears when hovering and highlights the planet itself, and not the orbit). Stroke will rotate with the planet, appearing only when hovering (I agree, not very economically, but not difficult). And yes, it will be on a layer with planets (). Planet.prototype.createOrbit = function(options){ var orbit = orbits.circle({ cx: center[0], cy: center[1], radius: this.radius, stroke: '1px rgba(0,192,255,0.5)' }); var stroke = planets.circle({ cx: center[0] + this.radius, // cy: center[1], radius: 15, fill: 'black', // ( - orbit.clip(stroke)). stroke: '3px rgba(0,192,255,1)', visible: false // }); // -, Planet orbit.mouseover(this.overOrbit.bind(this)).mouseout(this.out.bind(this)); this.orbit = orbit; this.stroke = stroke; // , ( ) this.startAngle = rand(360); // ( canvas-) stroke.rotate(this.startAngle, center); }
function rand(num){ return Math.floor(Math.random() * num); }
orbit.isPointIn = function(x, y){ x -= center[0]; y -= center[1]; return (x*x + y*y) <= Math.pow(this._radius + 20, 2) && ((x*x + y*y) > Math.pow(this._radius - 20, 2)); }
Planet.prototype.createPlanet = function(options){ // , // x var sprite = planets.sprite('images/planets.png', center[0] - 13 + options.radius, center[1] - 13); // 13,13 - sprite.autoslice(26, 26); // sprite.frame(options.image); // // sprite.mouseover(this.overPlanet.bind(this)).mouseout(this.out.bind(this)); sprite.click(this.click.bind(this)); sprite.cursor('pointer'); this.sprite = sprite; // sprite.rotate(this.startAngle, center);}}
Planet.prototype.overOrbit = function(e){ this.stroke.show(); // this.orbit.stroke('3px rgba(0,192,255,1)'); // } Planet.prototype.overPlanet = function(e){ this.stroke.show(); this.orbit.stroke('3px rgba(0,192,255,1)'); if(this.rect){ // - mouseover this.rect.remove(); this.text.remove(); } this.rect = planets.rect(e.contextX, e.contextY, 70, 25, 'rgb(0,56,100)', '1px rgb(0,30,50)'); this.text = planets.text({ text: this.name, // font: 'Arial 11pt', x: e.contextX + 35, // 35,12 -- , .. y: e.contextY + 12, align: 'center', baseline: 'middle', fill: "rgba(0,192,255,1)" }); } Planet.prototype.out = function(){ this.stroke.hide(); this.orbit.stroke('1px rgba(0,192,255,0.5)'); if(this.text){ this.text.remove(); this.rect.remove(); } } Planet.prototype.click = function(){ if(this.rotatePerMs){ this.rotatePerMs = 0; // - , .. . } else { this.rotatePerMs = 360 / 100 / this.time; } }
Planet.prototype.update = function(){ this.sprite.rotate(this.rotatePerMs, center); this.stroke.rotate(this.rotatePerMs, center); }
window.setInterval(function(){ for(var i = 0; i < 12; i++){ planetarray[i].update(); } }, 1);
Source: https://habr.com/ru/post/243457/
All Articles