// Point.js function Point(x, y) { this.x; this.y; this.set(x, y); // }; Point.prototype = { set: function(x, y) { this.x = x || 0; this.y = y || 0; }, getDis: function(other) { return Math.sqrt(Math.pow(other.x - this.x, 2) + Math.pow(other.y - this.y, 2)); }, clone: function() { return new Point(this.x, this.y); } }; // Orbit.js function Orbit(center, radius) { this.center = center; this.radius = radius; this.planet = null; // this.ctx = null; this.mouse = null; }; // Planet.js function Planet(orbit, radius, time) { this.pos = new Point(0, 0); this.orbit = orbit; this.radius = radius; this.speed = Math.PI*2 / (time * 1000); // this.angle = ~~(Math.random() * 360); // this.animate = true; this.name; this.tile; this.ctx; this.orbit.setProperty({'planet': this}); // }; // Tile.js function Tile(ctx, img, x, y, w, h) { this.ctx = ctx; // this.img = img; // - this.x = x; this.y = y; this.width = w; this.height = h; }; Tile.prototype = { draw: function(x, y) { this.ctx.drawImage(this.img, this.x, this.y, this.width, this.height, x, y, this.width, this.height); } }; /** * @param (object) property * @param (bool) add property, */ Object.prototype.setProperty = function(property, add) { if (add !== true) add = false; for (var key in property) { if (property.hasOwnProperty(key)) { if (typeof this[key] !== 'undefined' || add) { this[key] = property[key]; } } } return this; }
var IM = { // Images Manager store: {}, // imagesAdded: 0, // imagesLoaded: 0, // add: function(url, name) { // var self = this; var img = new Image(); img.onload = function() { self.imagesLoaded++; if (self.imagesAdded == self.imagesLoaded) { self.afterRun(); // , } } img.src = url; this.store[name] = img; this.imagesAdded++; }, afterRun: function() { // render(new Date() * 1); // } }; IM.add('img/sun.png', 'sun'); // IM.add('img/planets.png', 'planets'); //
var planets = []; // var mouse = {}; // var globalCenter = new Point(canvas.width / 2, canvas.height / 2); // // globalCenter var orbit = new Orbit(globalCenter.clone(), 0).setProperty({ ctx: ctx, // mouse: mouse // , }, true); // 50 1. . var planet = new Planet(orbit, 50, 1).setProperty({ tile: new Tile(this.ctx, this._resources['sun'], 0, 0, 100, 100), name: 'Sun', ctx: ctx }, true); planets.push(planet); // var names = ['Moon', 'Phobos', 'Deimos', 'Dactyl', 'Linus', 'Io', 'Europa', 'Ganymede', 'Callisto', 'Amalthea', 'Himalia', 'Elara', 'Pasiphae', 'Taurus', 'Sinope', 'Lysithea', 'Carme', 'Ananke', 'Leda', 'Thebe', 'Adrastea', 'Metis', 'Callirrhoe', 'Themisto', '1975', '2000', 'Megaclite', 'Taygete', 'Chaldene', 'Harpalyke']; var tiles = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]; // var time = 40; shuffle(names); // shuffle(tiles); for (var i = 0; i < 12; ++i) { // 90 , 26 orbit = new Orbit(globalCenter.clone(), 90+i*26).setProperty({ ctx: this.ctx, mouse: this.mouse }, true); planet = new Planet(orbit, 13, time).setProperty({ tile: new Tile(this.ctx, this._resources['planets'], tiles[i]*26, 0, 26, 26), name: names[i], ctx: this.ctx }, true); this.planets.push(planet); time += 20; }
function render(lastTime) { var curTime = new Date(); requestAnimationFrame(function(){ render(curTime); }); ctx.clearRect(0, 0, canvas.width, canvas.height); for (var i = 0, il = planets.length; i < il; ++i) { planets[i].render(curTime - lastTime); } } Planet.prototype = { drawBorder: function() { // var ctx = this.ctx; ctx.beginPath(); ctx.arc(this.pos.x, this.pos.y, this.radius * 1.1, 0, Math.PI * 2, true); ctx.closePath(); ctx.stroke(); }, showInfo: function() { var x = this.pos.x + this.radius * 0.7; // var y = this.pos.y + this.radius * 0.9; // ox oy ctx.fillStyle = '#002244'; ctx.fillRect(x, y, 100, 24); ctx.fillStyle = '#0ff'; ctx.fillText(this.name, x + 50, y + 17); }, render: function(deltaTime) { // r(fi) = radius, r - , fi - this.pos.x = this.orbit.globalCenter.x + this.orbit.radius * Math.cos(this.angle); this.pos.y = this.orbit.globalCenter.y + this.orbit.radius * Math.sin(this.angle); this.angle += this.speed * deltaTime; // if (typeof this.tile !== 'undefined') { // this.tile.draw(this.pos.x - this.radius, this.pos.y - this.radius); } } };
Orbit.prototype = { draw: function() { var ctx = this.ctx; var hover = this.mouse && Math.abs(mouse.pos.getDis(this.center) - this.radius) < 13; // if (hover) { // ctx.lineWidth = 2; ctx.strokeStyle = 'rgb(0,192,255)'; ctx.beginPath(); // ctx.arc(this.center.x, this.center.y, this.radius, 0, Math.PI * 2, true); ctx.closePath(); ctx.stroke(); if (typeof this.planet !== null) { // // ctx.clearRect(this.planet.pos.x - this.planet.radius, this.planet.pos.y - this.planet.radius, this.planet.radius * 2, this.planet.radius * 2); // this.planet.drawBorder(); } } else { // ctx.lineWidth = 1; ctx.strokeStyle = 'rgba(0,192,255,0.5)'; ctx.beginPath(); ctx.arc(this.center.x, this.center.y, this.radius, 0, Math.PI * 2, true); ctx.closePath(); ctx.stroke(); } } function render(lastTime) { var curTime = new Date(); requestAnimationFrame(function(){ render(curTime); // }); ctx.clearRect(0, 0, canvas.width, canvas.height); // var showInfo = -1; // for (var i = 0, il = planets.length; i < il; ++i) { // planets[i].orbit.draw(); // planets[i].render(curTime - lastTime); // if (Math.abs(planets[i].pos.x-mouse.pos.x) < planets[i].radius // && Math.abs(planets[i].pos.y-mouse.pos.y) < planets[i].radius) { showInfo = i; // , //if (mouse.pressed) { // // planets[i].animate = planets[i].animate ? false : true; //} } } if (showInfo > -1) { // , planets[showInfo].showInfo(); document.body.style.cursor = 'pointer'; } else { document.body.style.cursor = 'default'; } } }; . App.
Source: https://habr.com/ru/post/164899/
All Articles