魔性的舞蹈

 

魔性的舞蹈

 

this.p0 = p0;

this.p1 = p1;

this.distance = dist;

this.size = size;

this.light = light || 1.0;

this.force = force || 0.5;

this.image = stroke("hsl(" + parent.color + " ,30%, " + parent.light * this.light + "%)", true);

this.shadow = stroke("rgba(0,0,0,0.5)");

};

 

var Point = function Point(id, x, y, fn, w) {

_classCallCheck(this, Point);

 

this.id = id;

this.x = x;

this.y = y;

this.w = w || 0.5;

this.fn = fn || null;

this.px = x;

this.py = y;

this.vx = 0;

this.vy = 0;

};

 

var Canvas = function () {

function Canvas() {

var _this = this;

 

_classCallCheck(this, Canvas);

 

this.elem = document.createElement('canvas');

this.ctx = this.elem.getContext('2d');

document.body.appendChild(this.elem);

this.resize();

window.addEventListener('resize', function () {

return _this.resize();

}, false);

}

 

Canvas.prototype.resize = function resize() {

 

this.width = this.elem.width = this.elem.offsetWidth;

this.height = this.elem.height = this.elem.offsetHeight;

ground = this.height > 500 ? 0.85 : 1.0;

};

 

return Canvas;

}();

 

var Pointer = function () {

function Pointer(canvas) {

var _this2 = this;

 

_classCallCheck(this, Pointer);

 

this.x = 0;

this.y = 0;

this.canvas = canvas;

 

window.addEventListener('mousemove', function (e) {

return _this2.move(e);

}, false);

canvas.elem.addEventListener('touchmove', function (e) {

return _this2.move(e);

}, false);

window.addEventListener('mousedown', function (e) {

return _this2.down(e);

}, false);

window.addEventListener('touchstart', function (e) {

return _this2.down(e);

}, false);

window.addEventListener('mouseup', function (e) {

return _this2.up(e);

}, false);

window.addEventListener('touchend', function (e) {

return _this2.up(e);

}, false);

}

 

Pointer.prototype.down = function down(e) {

 

this.move(e);

 

for (var _iterator7 = dancers, _isArray7 = Array.isArray(_iterator7), _i7 = 0, _iterator7 = _isArray7 ? _iterator7 : _iterator7[Symbol.iterator]();;) {

var _ref7;

 

if (_isArray7) {

if (_i7 >= _iterator7.length) break;

_ref7 = _iterator7[_i7++];

} else {

_i7 = _iterator7.next();

if (_i7.done) break;

_ref7 = _i7.value;

}

 

var dancer = _ref7;

 

for (var _iterator8 = dancer.points, _isArray8 = Array.isArray(_iterator8), _i8 = 0, _iterator8 = _isArray8 ? _iterator8 : _iterator8[Symbol.iterator]();;) {

var _ref8;

 

if (_isArray8) {

if (_i8 >= _iterator8.length) break;

_ref8 = _iterator8[_i8++];

} else {

_i8 = _iterator8.next();

if (_i8.done) break;

_ref8 = _i8.value;

}

 

var point = _ref8;

 

var dx = pointer.x - point.x;

var dy = pointer.y - point.y;

var d = Math.sqrt(dx * dx + dy * dy);

if (d < 60) {

dancerDrag = dancer;

pointDrag = point;

dancer.frame = 0;

}

}

}

};

 

Pointer.prototype.up = function up(e) {

dancerDrag = null;

};

 

Pointer.prototype.move = function move(e) {

 

var touchMode = e.targetTouches,

    pointer = undefined;

if (touchMode) {

e.preventDefault();

pointer = touchMode[0];

} else pointer = e;

this.x = pointer.clientX;

this.y = pointer.clientY;

};

 

return Pointer;

}();

 

// ---- init ----

 

var ground = 1.0;

var canvas = new Canvas();

var ctx = canvas.ctx;

var pointer = new Pointer(canvas);

var dancerDrag = null;

var pointDrag = null;

 

// ---- main loop ----

 

function run() {

 

requestAnimationFrame(run);

ctx.clearRect(0, 0, canvas.width, canvas.height);

ctx.fillStyle = "#222";

ctx.fillRect(0, 0, canvas.width, canvas.height * 0.15);

ctx.fillRect(0, canvas.height * 0.85, canvas.width, canvas.height * 0.15);

 

for (var _iterator9 = dancers, _isArray9 = Array.isArray(_iterator9), _i9 = 0, _iterator9 = _isArray9 ? _iterator9 : _iterator9[Symbol.iterator]();;) {

var _ref9;

 

if (_isArray9) {

if (_i9 >= _iterator9.length) break;

_ref9 = _iterator9[_i9++];

} else {

_i9 = _iterator9.next();

if (_i9.done) break;

_ref9 = _i9.value;

}

 

var dancer = _ref9;

 

dancer.update();

dancer.draw();

}

}

 

// ---- robot structure ----

 

var struct = {

 

points: [[0, -4, function (s, d) {

this.y -= 0.01 * s;

}], [0, -16, function (s, d) {

this.y -= 0.02 * s * d;

}], [0, 12, function (s, d) {

this.y += 0.02 * s * d;

}], [-12, 0], [12, 0], [-3, 34, function (s, d) {

if (d > 0) {

this.x += 0.01 * s;

this.y -= 0.015 * s;

} else {

this.y += 0.02 * s;

}

}], [3, 34, function (s, d) {

if (d > 0) {

this.y += 0.02 * s;

} else {

this.x -= 0.01 * s;

this.y -= 0.015 * s;

}

}], [-28, 0, function (s, d) {

this.x += this.vx * 0.035;

this.y -= 0.001 * s;

}], [28, 0, function (s, d) {

this.x += this.vx * 0.035;

this.y -= 0.001 * s;

}], [-3, 64, function (s, d) {

this.y += 0.02 * s;

if (d > 0) {

this.y -= 0.01 * s;

} else {

this.y += 0.05 * s;

}

}], [3, 64, function (s, d) {

this.y += 0.02 * s;

if (d > 0) {

this.y += 0.05 * s;

} else {

this.y -= 0.01 * s;

}

}], [0, -4.1]],

 

links: [[3, 7, 12, 0.5], [1, 3, 24, 0.5], [1, 0, 18, 0.5], [0, 11, 60, 0.8], [5, 9, 16, 0.5], [2, 5, 32, 0.5], [1, 2, 50, 1], [6, 10, 16, 1.5], [2, 6, 32, 1.5], [4, 8, 12, 1.5], [1, 4, 24, 1.5]]

};

 

// ---- instanciate robots ----

var dancers = [];

 

for (var i = 0; i < 6; i++) {

dancers.push(new Robot(i * 360 / 7, 80, 4, (i + 2) * canvas.width / 9, canvas.height * ground - 295, struct));

}

 

run();

    </script>

 

</body>

</html>

 

 WEB前端学习交流群21 598399936

 

posted @ 2017-11-05 16:41  噜噜修  阅读(181)  评论(0编辑  收藏  举报