Ver código fonte

first commit

tags/v0.2.0^2
big bad waffle 6 anos atrás
pai
commit
ca7ef5f328
3 arquivos alterados com 128 adições e 0 exclusões
  1. +51
    -0
      helpers/passives/generator.js
  2. +13
    -0
      helpers/passives/index.html
  3. +64
    -0
      helpers/passives/renderer.js

+ 51
- 0
helpers/passives/generator.js Ver arquivo

@@ -0,0 +1,51 @@
var generator = {
nodes: [],

init: function () {
this.addNode({
x: 100,
y: 100
});

renderer.center(this.nodes[0]);
},

addNode: function (options) {
var nodes = this.nodes;
nodes.push(tplNode.build({
id: nodes.length,
x: options.x,
y: options.y
}));

renderer.makeDirty();
}
};

var tplNode = {
id: 0,
parents: [],
children: [],
pos: {
x: 0,
y: 0
},

build: function (options) {
var res = $.extend(true, {}, this, {
id: options.id,
pos: {
x: options.x,
y: options.y
}
});
delete res.build;

return res;
}
};

$(function () {
renderer.init();
generator.init();
})

+ 13
- 0
helpers/passives/index.html Ver arquivo

@@ -0,0 +1,13 @@
<!doctype html>
<html>
<head>
<title>test</title>
<link rel="stylesheet" href="styles.css">
<script src="../../src/client/plugins/jquery.min.js"></script>
<script src="renderer.js"></script>
<script src="generator.js"></script>
</head>
<body>
<canvas></canvas>
</body>
</html>

+ 64
- 0
helpers/passives/renderer.js Ver arquivo

@@ -0,0 +1,64 @@
var renderer = {
canvas: null,
ctx: null,

screen: {
width: 0,
height: 0
},

pos: {
x: 0,
y: 0
},

dirty: false,

constants: {
lineWidth: 5,
blockSize: 40
},

init: function () {
this.canvas = $('canvas')[0];
this.screen.width = this.canvas.width = $('body').width();
this.screen.height = this.canvas.height = $('body').height();
this.ctx = this.canvas.getContext('2d');

this.update();
},

center: function (node) {
this.pos.x = node.pos.x + (this.constants.blockSize / 2) - (this.screen.width / 2);
this.pos.y = node.pos.y + (this.constants.blockSize / 2) - (this.screen.height / 2);

this.ctx.translate(-this.pos.x, -this.pos.y);
this.makeDirty();
},

makeDirty: function () {
this.dirty = true;
},

render: function () {
var nodes = generator.nodes;

nodes.forEach(n => this.renderers.node.call(this, n));
},

update: function () {
if (this.dirty) {
this.dirty = false;
this.render();
}

window.requestAnimationFrame(this.update.bind(this));
},

renderers: {
node: function (node) {
this.ctx.fillStyle = '#c0c3cf';
this.ctx.fillRect(node.pos.x, node.pos.y, this.constants.blockSize, this.constants.blockSize)
}
}
};

Carregando…
Cancelar
Salvar